Java 按行读写文件(解决中文乱码)

import java.io.*;


public class EmployeeTest{
	EmployeeTest(){
		
	}
	
	// 按行读取文件
	public void readFile01(){
		try {
			FileReader fr=new FileReader("C:\\Users\\40622\\Desktop\\abccba.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]);
				System.out.println(line);
			}
			br.close();
			fr.close();
		}catch(IOException e){
			//System.out.println(e);
			e.printStackTrace();
		}
		
    }
	
	// 按行读取文件,解决读出中文字符时出现乱码
	public void readFile02(){
		try {
	        FileInputStream fis=new FileInputStream("C:\\Users\\40622\\Desktop\\abccba.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]);
				System.out.println(line);
	        }
	        br.close();
	        isr.close();
	        fis.close();
		}catch(IOException e){
			//System.out.println(e);
			e.printStackTrace();
		}
	}
	
	// 按行写入文件
	public void writeFile01(){
		try {
	        String[] arrs={
	            "zhangsan,23,FuJian",
	            "lisi,30,ShangHai",
	            "wangwu,43,BeiJing",
	            "laolin,21,ChongQing",
	            "ximenqing,67,GuiZhou",
	            "我们我们我们"
	        };
	        FileWriter fw=new FileWriter(new File("C:\\Users\\40622\\Desktop\\abccba.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();
	    }catch(IOException e){
			//System.out.println(e);
			e.printStackTrace();
		}
		
    }
	
	// 按行写,解决写入中文字符时出现乱码,且追加模式
	public void writeFile02(){
		try {
	        String[] arrs={
	                "zhangsan,23,福建",
	                "lisi,30,上海",
	                "wangwu,43,北京",
	                "laolin,21,重庆",
	                "ximenqing,67,贵州"
	        };
	        //写入中文字符时解决中文乱码问题
	        FileOutputStream fos=new FileOutputStream(new File("C:\\Users\\40622\\Desktop\\abccba.txt"), true);
	        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();
		}catch(IOException e){
			//System.out.println(e);
			e.printStackTrace();
		}
    }
	
   public static void main(String[] args){
	   EmployeeTest abc = new EmployeeTest();
	   abc.readFile01();
	   abc.readFile02();
	   abc.writeFile01();
	   abc.writeFile02();
   }
}

 

你可能感兴趣的:(Java)