java如何一行一行读取文件_java文件写入「建议收藏」

java如何一行一行读取文件_java文件写入「建议收藏」假如E:/phsftp/evdokey目录下有个evdokey_201103221556.txt文件,现在对evdokey_201103221556.txt文件进行写入或读取操作,并解决写入或读取出现的乱码问题。文件内容如:zhangsan,23,福建lisi,30,上海wangwu,43,北

大家好,欢迎来到IT知识分享网。

假如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();
}

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/29168.html

(0)
上一篇 2023-09-28 10:33
下一篇 2023-09-28 13:45

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信