1. 程式人生 > >Java按一行一行進行檔案的讀取或寫入

Java按一行一行進行檔案的讀取或寫入

/**
     * 一行一行讀取檔案,適合字元讀取,若讀取中文字元時會出現亂碼
     * 
     * 流的關閉順序:先開啟的後關,後開啟的先關,
     *       否則有可能出現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();
    }