轉自 Ruthless
鏈接:http://www.cnblogs.com/linjiqin/archive/2011/03/23/1992250.html
假如E:/phsftp/evdokey目錄下有個evdokey_201103221556.txt文件,
現在對evdokey_201103221556.txt文件進行寫入或讀取操作,並解決寫入或讀取出現的亂碼問題。
文件內容如:
zhangsan,23,福建
lisi,30,上海
wangwu,43,北京
laolin,21,重慶
ximenqing,67,貴州
代碼如下:
/**
* 一行一行讀取文件,適合字符讀取,若讀取中文字符時會出現亂碼
*
* 流的關閉順序:先打開的后關,后打開的先關,
* 否則有可能出現java.io.IOException: Stream closed異常
*
* @throws IOException
*/
@Test
public void readFile01() throws IOException {
FileReader fr=new FileReader("E:/phsftp/evdokey/evdokey_201103221556.txt");
BufferedReader br=new BufferedReader(fr);
String line="";
String[] arrs=null;
while ((line=br.readLine())!=null) {
arrs=line.split(",");
System.out.println(arrs[0] + " : " + arrs[1] + " : " + arrs[2]);
}
br.close();
fr.close();
}
/**
* 一行一行讀取文件,解決讀取中文字符時出現亂碼
*
* 流的關閉順序:先打開的后關,后打開的先關,
* 否則有可能出現java.io.IOException: Stream closed異常
*
* @throws IOException
*/
@Test
public void readFile02() throws IOException {
FileInputStream fis=new FileInputStream("E:/phsftp/evdokey/evdokey_201103221556.txt");
InputStreamReader isr=new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(isr);
//簡寫如下
//BufferedReader br = new BufferedReader(new InputStreamReader(
// new FileInputStream("E:/phsftp/evdokey/evdokey_201103221556.txt"), "UTF-8"));
String line="";
String[] arrs=null;
while ((line=br.readLine())!=null) {
arrs=line.split(",");
System.out.println(arrs[0] + " : " + arrs[1] + " : " + arrs[2]);
}
br.close();
isr.close();
fis.close();
}
/**
* 一行一行寫入文件,適合字符寫入,若寫入中文字符時會出現亂碼
*
* 流的關閉順序:先打開的后關,后打開的先關,
* 否則有可能出現java.io.IOException: Stream closed異常
*
* @throws IOException
*/
@Test
public void writeFile01() throws IOException {
String[] arrs={
"zhangsan,23,FuJian",
"lisi,30,ShangHai",
"wangwu,43,BeiJing",
"laolin,21,ChongQing",
"ximenqing,67,GuiZhou"
};
FileWriter fw=new FileWriter(new File("E:/phsftp/evdokey/evdokey_201103221556.txt"));
//寫入中文字符時會出現亂碼
BufferedWriter bw=new BufferedWriter(fw);
//BufferedWriter bw=new BufferedWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("E:/phsftp/evdokey/evdokey_201103221556.txt")), "UTF-8")));
for(String arr:arrs){
bw.write(arr+"\t\n");
}
bw.close();
fw.close();
}
/**
* 一行一行寫入文件,解決寫入中文字符時出現亂碼
*
* 流的關閉順序:先打開的后關,后打開的先關,
* 否則有可能出現java.io.IOException: Stream closed異常
*
* @throws IOException
*/
@Test
public void writeFile02() throws IOException {
String[] arrs={
"zhangsan,23,福建",
"lisi,30,上海",
"wangwu,43,北京",
"laolin,21,重慶",
"ximenqing,67,貴州"
};
//寫入中文字符時解決中文亂碼問題
FileOutputStream fos=new FileOutputStream(new File("E:/phsftp/evdokey/evdokey_201103221556.txt"));
OutputStreamWriter osw=new OutputStreamWriter(fos, "UTF-8");
BufferedWriter bw=new BufferedWriter(osw);
//簡寫如下:
//BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
// new FileOutputStream(new File("E:/phsftp/evdokey/evdokey_201103221556.txt")), "UTF-8"));
for(String arr:arrs){
bw.write(arr+"\t\n");
}
//注意關閉的先后順序,先打開的后關閉,后打開的先關閉
bw.close();
osw.close();
fos.close();
}
注意:下面三行代碼
FileOutputStream fos=new FileOutputStream(new File("E:/phsftp/evdokey/evdokey_201103221556.txt")); OutputStreamWriter osw=new OutputStreamWriter(fos, "UTF-8"); BufferedWriter bw=new BufferedWriter(osw);
解釋:http://bbs.csdn.net/topics/330165652
FileOutputStream 是字節流,它一個字節一個字節的向外邊送數據
OutputStreamWrite是字符流,它一個字符一個字符的向外邊送數據
它們有什么區別么?
因為計算機是洋鬼子發明的,它們的英文字符占一個字節,而我們的中文是一個字符,占倆字節。
如果用stream,你讀出來的英語再倒也罷了,讀出來的中文可就是亂碼或者一個個“????”。
如果你用WRITER,就不會有亂碼了。
BufferedWriter Buffer是一個緩沖區,為什么要用BUFFER呢?
如果你直接用stream或者writer,你的硬盤可能就是一個字符或者一個字節 讀寫硬盤一次,
可是你用了Buffer,你的硬盤就是讀了一堆數據之后,讀寫一下硬盤。這樣對你硬盤有好處。
