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();
            }
        }
    
    
    
    
    
  • 相关阅读:
    第四章 Zookeeper技术内幕
    容器--TreeMap
    算法--红黑树实现介绍(二)
    算法---红黑树实现介绍(一)
    容器--WeakHashMap
    基础-WeakReference
    容器--IdentityHashMap
    容器--EnumMap
    容器--HashMap
    容器--Map和AbstractMap
  • 原文地址:https://www.cnblogs.com/weibanggang/p/9347778.html
Copyright © 2011-2022 走看看