java(IO)读写文件乱码转换UTF-8问题

java(IO)读写文件乱码转换UTF-8问题

     读取文件

        String Content = ""; // 文件很长的话建议使用StringBuffer
     try {
         FileInputStream fs=new FileInputStream("文件录取");
         InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
         
         BufferedReader br = new BufferedReader(isr);
          String line = null;
         while ((line = br.readLine()) != null) {
             Content += line;
             Content += "\r\n"; // 补上换行符
         }catch (Exception e) {         
         e.printStackTrace();
                  }  

      写入文件
         
        StringBuffer sb=new StringBuffer();//文件内容
     try {
        FileOutputStream fos=new FileOutputStream("文件路径");
    OutputStreamWriter osw=newOutputStreamWriter(fos,"utf-8");
               osw.write(sb);
              osw.close();

         }catch (Exception e) {         
        e.printStackTrace();
                }  




                                

猜你喜欢

转载自www.cnblogs.com/weibanggang/p/9347778.html