zoukankan      html  css  js  c++  java
  • java一行一行写入或读取数据

       
       

    假如E:/phsftp/evdokey目录下有个evdokey_201103221556.txt文件,

    现在对evdokey_201103221556.txt文件进行写入或读取操作,并解决写入或读取出现的乱码问题。

    /**
         * 一行一行读取文件,适合字符读取,若读取中文字符时会出现乱码
         * 
         * 流的关闭顺序:先打开的后关,后打开的先关,
         *       否则有可能出现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+"	
    ");   
    
          }      
    
           bw.close(); 
    
           fw.close();  
    
       }
  • 相关阅读:
    每日博客
    每日博客
    今日收获
    今日收获
    今日收获
    今日收获
    今日收获
    今日收获
    今日收获
    今日收获
  • 原文地址:https://www.cnblogs.com/yy123/p/4170473.html
Copyright © 2011-2022 走看看