黑马程序员 java基础之IO流(一)


使用IO流的规律:

  1、首先知道IO流的体系,分为输入和输出流,输入流:InputStream,Reader.输出流:OutputStream,Writer

      2、明确源和目的,源  需要用到InputStream或者Reader;

               目的需要用到OutputSteam或者Writer;

     3、明确具体文件类型:如果是文本文件,用字符流Reader/Writer;

               如果是非文本文件,如图片,视频等,用字节流InputStream/OutputStream;

    4、通过设备确定用哪个具体对象:源分为:内存,硬盘,键盘。

             目的分为:内存,硬盘,控制台。

    5、是否需要提高效率:BufferedReader/Writer

练习一:读取一个.java文件,打印出来

 class IOStream{

    public static void main(String [] args){
        FileReader fr = null;
        try{
            fr = new FileReader("area.java");
            char [] ch = new char[1024];
            int num = 0;
            while((num = fr.read(ch))!=-1){     
                System.out.print("area.java:" + new String(ch,0,num));
            }
        }
        catch(IOException e){
            System.out.println(e.toString());
        }
        finally{
            try{
                fr.close();
            }
            catch(IOException e){
                System.out.println(e.toString());
            }           
        }       
    }
}

 练习二:将一个c盘的文件复制到d盘

 class IOStream{

    public static void main(String [] args){
        FileWriter fw = null;
        FileReader fr = null;
        try{
            fw = new FileWriter("D:\\area.java");
            fr = new FileReader("area.java");
            char [] ch = new char[1024];
            int num = 0;
            while((num = fr.read(ch))!=-1){
                fw.write(ch,0,num);
            }
        }
        catch(IOException e){
            System.out.println(e.toString());
        }
        finally{
            if(fr == null)
                try{                    
                    fr.close();
                }
                catch(IOException e){
                    System.out.println(e.toString());
                }
            if(fw == null)
                try{
                    fw.close();                 
                }
                catch(IOException e){
                    System.out.println(e.toString());
                }
        }       
    }
}

 练习三:练习二的改进版,利用缓冲区复制文本

 class IOStream{

    public static void main(String [] args){
        BufferedReader br = null;
        BufferedWriter bw = null;
        try{            
            br = new BufferedReader(new FileReader("area.java"));
            bw = new BufferedWriter(new FileWriter("D:\\area.java"));
            String s = null;
            while((s = br.readLine())!=null){
                bw.write(s);
                bw.newLine();
                bw.flush();
            }
        }
        catch(IOException e){
            System.out.println(e.toString());
        }
        finally{
            if(bw==null)
                try{
                    bw.close();                 
                }
                catch(IOException e){
                    System.out.println(e.toString());
                }
            if(br==null)
                try{                    
                    br.close();
                }
                catch(IOException e){
                    System.out.println(e.toString());
                }   
        }
    }
}

 练习四:拷贝一张图片

 class IOStream{

    public static void main(String [] args){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try{
            fis = new FileInputStream("34.jpg");
            fos = new FileOutputStream("D:\\background.jpg");
            byte [] bt = new byte[1024];
            int num = 0;
            while((num = fis.read(bt))!=-1){
                fos.write(bt);
            }
        }
        catch(IOException e){
            System.out.println(e.toString());
        }
        finally{
            if(fis==null)
                try{
                    fis.close();
                }
                catch(IOException e){
                    System.out.println(e.toString());
                }
            if(fos==null)
                try{
                    fos.close();
                }
                catch(IOException e){
                    System.out.println(e.toString());
                }   
        }
    }
}

 

 

 

 

 

 

posted @ 2013-05-14 20:44  xiewen3410  阅读(94)  评论(0编辑  收藏  举报