黑马程序员_IO流(一)

        

             ------ android培训java培训java学习型技术博客、期待与您交流! ----------       

 IO流


1  概述


   1). IO流基础知识


         [1]. IO用途:IO流是用来处理设备之间的数据传输的

         [2]. 早期的JavaIO中都是字节流(体现均为二进制字节)

         [3]. 字符流是从字节流中分离出来的,用来处理文本数据的流。

    2). IO流的抽象基类


         [1]. 字节流的抽象基类 ---- InputStreamOutputStream

         [2]. 字符流的抽象基类 ---- ReaderWriter


2   字符流

      字符流继承体系结构 (ReaderWriter)



       字符流写入

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
 
public class Demo {
    public static void main(String[] args ) {
         
        //创建要操作的文件路径和名称
        //其中,File.separator表示系统相关的分隔符,Linux下为:/  Windows下为:\\
        String path = File.separator + "home" + File.separator + "siu" + 
                      File.separator + "work" + File.separator + "demo.txt";
     
        //由于IO操作会抛出异常,因此在try语句块的外部定义FileWriter的引用
        FileWriter w = null;
        try {
            //以path为路径创建一个新的FileWriter对象
            //如果需要追加数据,而不是覆盖,则使用FileWriter(path,true)构造方法
            w = new FileWriter(path);
             
            //将字符串写入到流中,\r\n表示换行想有好的
            w.write("Nerxious is a good boy\r\n");
            //如果想马上看到写入效果,则需要调用w.flush()方法
            w.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //如果前面发生异常,那么是无法产生w对象的 
            //因此要做出判断,以免发生空指针异常
            if(w != null) {
                try {
                    //关闭流资源,需要再次捕捉异常
                    w.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

    字符流读取

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
 
public class Demo2 {
    public static void main(String[] args ) {
        String path = File.separator + "home" + File.separator + "siu" + 
                      File.separator + "work" + File.separator + "demo.txt";
 
        FileReader r = null;
        try {
            r = new FileReader(path);
             
            //方式一:读取单个字符的方式
            //每读取一次,向下移动一个字符单位
            int temp1 = r.read();
            System.out.println((char)temp1);
            int temp2 = r.read();
            System.out.println((char)temp2);
                         
            //方式二:循环读取
            //read()方法读到文件末尾会返回-1
            /*
            while (true) {
                int temp = r.read();
                if (temp == -1) {
                    break;
                }
                System.out.print((char)temp);
            }
            */
             
            //方式三:循环读取的简化操作
            //单个字符读取,当temp不等于-1的时候打印字符
            /*int temp = 0;
            while ((temp = r.read()) != -1) {
                System.out.print((char)temp);
            }
            */
             
            //方式四:读入到字符数组
            /*
            char[] buf = new char[1024];
            int temp = r.read(buf);
            //将数组转化为字符串打印,后面参数的意思是
            //如果字符数组未满,转化成字符串打印后尾部也许会出现其他字符
            //因此,读取的字符有多少个,就转化多少为字符串
            System.out.println(new String(buf,0,temp));
            */
             
            //方式五:读入到字符数组的优化
            //由于有时候文件太大,无法确定需要定义的数组大小
            //因此一般定义数组长度为1024,采用循环的方式读入
            /*
            char[] buf = new char[1024];
            int temp = 0;
            while((temp = r.read(buf)) != -1) {
                System.out.print(new String(buf,0,temp));
            }
            */
             
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(r != null) {
                try {
                    r.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

    文本文件的复制

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
 
public class Demo {
    public static void main(String[] args ) {
         
        String doc = File.separator + "home" + File.separator + "siu" + 
                      File.separator + "work" + File.separator + "demo.txt";
         
        String copy = File.separator + "home" + File.separator + "siu" + 
                     File.separator + "life" + File.separator + "lrc.txt";
 
        FileReader r = null;
        FileWriter w = null;
        try {
            r = new FileReader(doc);
            w = new FileWriter(copy);
             
            //方式一:单个字符写入
            int temp = 0;
            while((temp = r.read()) != -1) {
                w.write(temp);
            }
             
            //方式二:字符数组方式写入
            /*
            char[] buf = new char[1024];
            int temp = 0;
            while ((temp = r.read(buf)) != -1) {
                w.write(new String(buf,0,temp));
            }
            */
             
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //分别判断是否空指针引用,然后关闭流
            if(r != null) {
                try {
                    r.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(w != null) {
                try {
                    w.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

   字符串缓冲流对文本文件复制

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
 
public class Demo {
    public static void main(String[] args ) {
         
        String doc = File.separator + "home" + File.separator + "siu" + 
                      File.separator + "work" + File.separator + "demo.txt";
         
        String copy = File.separator + "home" + File.separator + "siu" + 
                     File.separator + "life" + File.separator + "lrc.txt";
 
        FileReader r = null;
        FileWriter w = null;
        //创建缓冲区的引用
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            r = new FileReader(doc);
            w = new FileWriter(copy);
            //创建缓冲区对象
            //将需要提高效率的FileReader和FileWriter对象放入其构造函数内
            //当然,也可以使用匿名对象的方式 br = new BufferedReader(new FileReader(doc));
            br = new BufferedReader(r);
            bw = new BufferedWriter(w);
             
            String line = null;
            //读取行,直到返回null
            //readLine()方法只返回换行符之前的数据
            while((line = br.readLine()) != null) {
                //使用BufferWriter对象的写入方法
                bw.write(line);
                //写完文件内容之后换行
                //newLine()方法依据平台而定
                //windows下的换行是\r\n
                //Linux下则是\n
                bw.newLine();
            }       
             
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //此处不再需要捕捉FileReader和FileWriter对象的异常
            //关闭缓冲区就是关闭缓冲区中的流对象
            if(br != null) {
                try {
                    r.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

3   字节流

    字节流继承体系结构 (InputStream和OutputStream)


    字节流写入

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
public class Demo {
    public static void main(String[] args ) {
          
        String path = File.separator + "home" + File.separator + "siu" +
                      File.separator + "work" + File.separator + "demo.txt";
         
        FileOutputStream o = null;
         
        try {
            o = new FileOutputStream(path);
            String str = "Nerxious is a good boy\r\n";
            byte[] buf = str.getBytes();
            //也可以直接使用o.write("String".getBytes());
            //因为字符串就是一个对象,能直接调用方法
            o.write(buf);
             
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(o != null) {
                try {
                    o.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
    }
}

字节流读取

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
 
public class Demo {
    public static void main(String[] args ) {
          
        String path = File.separator + "home" + File.separator + "siu" +
                      File.separator + "work" + File.separator + "demo.txt";
         
        FileInputStream i = null;
         
        try {
            i = new FileInputStream(path);
             
            //方式一:单个字符读取
            //需要注意的是,此处我用英文文本测试效果良好
            //但中文就悲剧了,不过下面两个方法效果良好
            int ch = 0;
            while((ch=i.read()) != -1){
                System.out.print((char)ch);
            }
             
            //方式二:数组循环读取
            /*
            byte[] buf = new byte[1024];
            int len = 0;
            while((len = i.read(buf)) != -1) {
                System.out.println(new String(buf,0,len));
            }
            */
             
             
            //方式三:标准大小的数组读取
            /*
            //定一个一个刚好大小的数组
            //available()方法返回文件的字节数
            //但是,如果文件过大,内存溢出,那就悲剧了
            //所以,亲们要慎用!!!上面那个方法就不错
            byte[] buf = new byte[i.available()];
            i.read(buf);
            //因为数组大小刚好,所以转换为字符串时无需在构造函数中设置起始点
            System.out.println(new String(buf));
            */
             
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(i != null) {
                try {
                    i.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
    }
}


mp3格式文件用字节数组复制

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
 
public class Demo {
    public static void main(String[] args ) {
          
        String bin = File.separator + "home" + File.separator + "siu" +
                      File.separator + "work" + File.separator + "一个人生活.mp3";
         
        String copy = File.separator + "home" + File.separator + "siu" +
                      File.separator + "life" + File.separator + "一个人生活.mp3";
         
        FileInputStream i = null;
        FileOutputStream o = null;
         
        try {
            i = new FileInputStream(bin);
            o = new FileOutputStream(copy);
             
            //循环的方式读入写出文件,从而完成复制
            byte[] buf = new byte[1024];
            int temp = 0;
            while((temp = i.read(buf)) != -1) {
                o.write(buf, 0, temp);
            }
 
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(i != null) {
                try {
                    i.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(o != null) {
                try {
                    o.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

mp3格式文件用字符串缓冲区复制

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
 
public class Demo {
    public static void main(String[] args ) {
          
        String bin = File.separator + "home" + File.separator + "siu" +
                      File.separator + "work" + File.separator + "一个人生活.mp3";
         
        String copy = File.separator + "home" + File.separator + "siu" +
                      File.separator + "life" + File.separator + "一个人生活.mp3";
         
        FileInputStream i = null;
        FileOutputStream o = null;
        BufferedInputStream bi = null;
        BufferedOutputStream bo = null;
         
        try {
            i = new FileInputStream(bin);
            o = new FileOutputStream(copy);
            bi = new BufferedInputStream(i);
            bo = new BufferedOutputStream(o);
             
            byte[] buf = new byte[1024];
            int temp = 0;
            while((temp = bi.read(buf)) != -1) {
                bo.write(buf,0,temp);
            }
             
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(bi != null) {
                try {
                    i.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bo != null) {
                try {
                    o.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}






              ------ android培训java培训java学习型技术博客、期待与您交流! ----------

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章