java一行一行寫入或讀取資料

來源:互聯網
上載者:User

假如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();
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.