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

     读取文件
public static String readHtml(String fileName){ 
        FileInputStream fis = null; 
        StringBuffer sb = new StringBuffer(); 
        try { 
            fis = new FileInputStream(fileName); 
            InputStreamReader isr = new InputStreamReader(fis); 
            BufferedReader br = new BufferedReader(isr); 
            String line = null; 
            while ((line = br.readLine()) != null) { 
                sb.append(line); 
            } 
        } catch (FileNotFoundException e) { 
            e.printStackTrace(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } finally { 
            try { 
                fis.close(); 
            } catch (IOException e1) { 
                e1.printStackTrace(); 
            } 
        } 
        return sb.toString(); 
    }
 
 

 

 
 
      写入文件
   public static void outFile(String sb) { 
        try { 
            File file = new File("G:\\liuhecai.txt"); 
            if (file.exists()) { 
                file.delete(); 
            } 
            file.createNewFile(); 
            FileOutputStream fos = new FileOutputStream(file); 
            OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8"); 
            osw.write(sb); 
        } catch (UnsupportedEncodingException e) { 
            e.printStackTrace(); 
        } catch (FileNotFoundException e) { 
            e.printStackTrace(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
    }
 
 

 

 

 

发布评论

分享到:

IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

Hbuilder与夜神连接讲解
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。