zoukankan      html  css  js  c++  java
  • InputStreamReader & OutputStreamWriter

    1. code

    package test;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.UnsupportedEncodingException;
    
    public class IOTest {
    
        public static void main(String[] args) {
    
    //        writeInfo();
            readInfo();
            
    
        }
        
        public static void readInfo(){
            String file = "d:\a.txt";
            String charset = "utf-8";
            InputStreamReader reader = null;
            try {
                FileInputStream fis = new FileInputStream(file);
                reader = new InputStreamReader(fis, charset);
                StringBuffer buffer = new StringBuffer();
                char[] buf = new char[64];
                int count = 0;
                while((count = reader.read(buf))!= -1){
                    buffer.append(buf, 0, count);
                }
                System.out.println(buffer.toString());
                System.out.println();
    //            System.getProperties().list(System.out);
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        
        public static void writeInfo(){
            String file = "d:\a.txt";
            String charset = "utf-8";
            OutputStreamWriter writer = null;
            try {
                FileOutputStream fos = new FileOutputStream(file);
                writer = new OutputStreamWriter(fos, charset);
                writer.write("李四到此一游.....");
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                try {
                    writer.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    
    }

    2. 字节到字符 转换的桥梁 InputStreamReader & OutputStreamWriter

        避免乱码的方案就是 编解码规范一致

  • 相关阅读:
    windows命令行下导入excel数据到SQLite数据库
    Android Studio如何提示函数用法
    在不root手机的情况上读取Data目录上的文件
    OSI七层模型
    设计模式之代理模式
    Android中Javascript中的调用
    cf #205 B Codeforces Round #205 (Div. 2) B. Two Heaps
    uva 10600 次小生成树
    防2B && 图论模板 && 7788
    最大匹配 && 最佳完美匹配 模板
  • 原文地址:https://www.cnblogs.com/rocky-fang/p/6566090.html
Copyright © 2011-2022 走看看