package 將C盤一個(gè)文本copy到D盤;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/*步驟
* 1.在D盤創(chuàng)建一個(gè)文件经瓷,用于存儲(chǔ)C盤文件中的數(shù)據(jù)
* 2.定義讀取流和c盤文件關(guān)聯(lián)
* 3.通過不斷的讀寫完成數(shù)據(jù)存儲(chǔ)
* 4. (finally中) 關(guān)閉資源?
*/
public class CopyText {
public static void main(String[] args) {
// TODO Auto-generated method stub
// copy_1();
copy_2();
}
//方式一? 從C盤讀一個(gè)字符,就往D盤寫一個(gè)字符
private static void copy_1() {
//創(chuàng)建目的地
FileWriter fw = null;
FileReader fr = null;
try {
fw = new FileWriter("CopyTextTXT.txt");
fr = new FileReader("/Users/denmeiho/Documents/workspace/將C盤一個(gè)文本copy到D盤/src/將C盤一個(gè)文本copy到D盤/CopyText.java");
int ch = 0;
while ((ch=fr.read())!=-1) {
fw.write(ch);
System.out.println((char)ch);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(fw!=null)
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(fr!=null)
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//方式2? 從C盤讀一個(gè)字符钉嘹,就往D盤寫一個(gè)字符
private static void copy_2() {
//創(chuàng)建目的地
FileWriter fw = null;
FileReader fr = null;
try {
fw = new FileWriter("CopyTextTXT.txt");
fr = new FileReader("/Users/denmeiho/Documents/workspace/將C盤一個(gè)文本copy到D盤/src/將C盤一個(gè)文本copy到D盤/CopyText.java");
char[] buf = new char[1024];
int len = 0;
while ((len=fr.read(buf))!=-1) {
System.out.println(new String(buf,0,len));
fw.write(buf, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(fw!=null)
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(fr!=null)
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//獲取系統(tǒng)啟動(dòng)時(shí)的 屬性 ?并打印到sysinfo.tex文件中