IO流

字节输入流:

private static void readAll(){
        InputStream  is=null;
        try {
             is=new FileInputStream("f:\\xiaoshuo.txt");
             byte[]  data=new byte[1024]; 
             int len=0;
             StringBuffer str=new StringBuffer();
             while(   (len=is.read(data))!=-1  ){
                 System.out.println("本次读了长度为:"+len+"个字节的数据");
                 str.append(  new  String(data,0,len,"UTF-8")  ); 
             }
             System.out.println("------------读完了---------------");
             System.out.println(str.toString());
        } catch (FileNotFoundException e) { 
            e.printStackTrace();
        } catch (IOException e) { 
            e.printStackTrace();
        }finally{
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) { 
                    e.printStackTrace();
                }
            }
        } 
    }

字符输入流:

private static void readAll(){
        Reader rd=null;        
        try {
            rd=new FileReader("f:\\xiaoshuo.txt");
            char[] data=new char[1024];
            int len=0;
            StringBuffer str=new StringBuffer();
            while(  (len=rd.read(data))!=-1  ){
                 System.out.println("本次读了长度为:"+len+"个字符的数据");
                 str.append(new String(data, 0, len));
            }
            System.out.println(str.toString());
        } catch (FileNotFoundException e) { 
            e.printStackTrace();
        } catch (IOException e) { 
            e.printStackTrace();
        }finally{
            if(rd!=null){
                try {
                    rd.close();
                } catch (IOException e) { 
                    e.printStackTrace();
                }
            }
        }

利用包装类解决字节流乱码问题:

private static void readAll(){
        InputStream  is=null;
        ByteArrayOutputStream baos=null;
        try {
             is=new FileInputStream("f:\\xiaoshuo.txt");
             baos=new ByteArrayOutputStream();
             byte[]  data=new byte[1024]; 
             int len=0;
             while(   (len=is.read(data))!=-1  ){
                 System.out.println("本次读了长度为:"+len+"个字节的数据");
                 baos.write(data, 0, len);
             }
             
             System.out.println("------------读完了---------------");
             System.out.println(  baos.toString("UTF-8"));
        } catch (FileNotFoundException e) { 
            e.printStackTrace();
        } catch (IOException e) { 
            e.printStackTrace();
        }finally{
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) { 
                    e.printStackTrace();
                }
            }
            if(baos!=null){
                try {
                    baos.close();
                } catch (IOException e) { 
                    e.printStackTrace();
                }
            }
        }
    }

输出流:

private static void write1(){
        OutputStream  os=null;
        try {
            os=new FileOutputStream("f:\\11.txt",true);
            String info="\r\n这是我的四个数据";
            os.write( info.getBytes("UTF-8"));
            os.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) { 
            e.printStackTrace();
        } catch (IOException e) {             
            e.printStackTrace();
        }finally{
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) { 
                    e.printStackTrace();
                }
            }
        }
    }

 

posted @ 2017-06-25 17:55  莫轩ASL  阅读(115)  评论(0编辑  收藏  举报