zoukankan      html  css  js  c++  java
  • java(IO)读写文件乱码转换UTF-8问题

    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();
            }
        }
    
    
    
    
    
  • 相关阅读:
    Docker学习笔记
    Linux学习笔记
    C#
    30分钟掌握 C#7
    30分钟掌握 C#6
    Redmine部署到Windows Azure
    关于企业管理系统集成那些事
    变量内存分配知多少
    流行Java IDE工具大比拼[转]
    pgpool 流复制主从安装与配置(高可用、读写分离)[转]
  • 原文地址:https://www.cnblogs.com/weibanggang/p/9347778.html
Copyright © 2011-2022 走看看