IO流知识点浅析

来源:互联网 发布:淘宝账号怎么注销? 编辑:程序博客网 时间:2024/05/16 11:10

IO流

什么是流?

流就是一组有序的,有起点和终点的字节集合,是对数据传输的总称。客观一点就是数据在两设备间的传输称为流,流的本质就是数据传输,根据数据传输特性将流抽象为各种类,方便更直观进行数据操作。

IO流的分类:

1、按字节分:

  • 字节流 - InPutStream/OutPutStream
  • 字符流 - Reader/Writer

2、按方向分:

  • 输入流 - InPutStream/Reader
  • 输出了 - OutPutStream/Writer

3、按功能分:

  • 包装流(转换流) - InPuterStreamReader/OutPutStreamWriter
  • 节点流(除包装流外都是节点流)- FileOutPutStream

更详细分类如下图:

字节流

先来看字节流的例子(InPutStream/OutPutStream):

public class IODemo1 {public static void main(String[] args) {    FileOutputStream fos=null; // OutputStream输出流    try {        fos=new FileOutputStream("ab.txt");//在名叫"ab.txt"的文件中写内容        fos.write("Hello World".getBytes());//将内容写到缓冲区        fos.flush();    //保存到硬盘    } catch (Exception e) {        e.printStackTrace();    }finally {        CloseUtils.close(fos);//调用自己写的CloseUtils方法,实现流的关闭    }  }}

将内容”Hello World”写到”ab.txt”文件中。

public class IODemo2 {public static void main(String[] args) {    FileInputStream fis=null;    StringBuilder sb=new StringBuilder();    try {        fis=new FileInputStream("ab.txt");//读"ab.txt"文件        byte[] buf=new byte[512];        int len=-1;         while((len=fis.read(buf))!=-1){  //读文件,到-1后结束            sb.append(new String(buf, 0, len));        }    } catch (Exception e) {        e.printStackTrace();    }finally {        CloseUtils.close(fis);//关闭流    }    System.out.println(sb);  }}

运行结果:

Hello World

字节流综合运用(实现文件的复制功能)

 import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import com.sxt.io.CloseUtils; public class IODemo4 {    public static void main(String[] args) {        copy("D:"\\java.mp4","j.mp4");定义一个copy()方法,将D盘的java.mp4文件复制到我当前工程下    }      private static void copy(String from, String to) {       BufferedInputStream bis=null;       BufferedOutputStream bos=null;          try {            File file = new File(from);            bis=new BufferedInputStream(new FileInputStream(file)); //输入流            bos=new BufferedOutputStream(new FileOutputStream(to)); //输出流            long total = file.length();            long part=0;// 用来记录复制了多少            byte[] buf=new byte[512];            int len=-1;            while((len=bis.read(buf))!=-1){               bos.write(buf,0,len);               part+=len;               System.out.println("当前进度:"+(part*100/total)+"%");//打印复制文件的进度百分比            }       }catch (Exception e) {           e.printStackTrace();       }finally {            CloseUtils.close(bis);//调用自己创建的工具类,实现关闭流的功能            CloseUtils.close(bos);        }      }   }

上面的代码,实现了将我D盘下的一个视频文件复制到eclipes的当前工程中,这里用到了BufferedInputStream和BufferedOutStream这两个子类,它们的父类分别是InPutStream和OutPutStream。

InputStream类常用方法:

  • int read( )
  • int read(byte[] b)
  • int read(byte[] b,int off,int len)
  • void close( )

子类FileInputStream常用的构造方法:

  • FileInputStream(File file)
  • FileInputStream(String name)

OutputStream类常用方法:

  • void write(int c)
  • void write(byte[] buf)
  • void write(byte[] b,int off,int len)
  • void close( )

子类FileOutputStream常用的构造方法:

  • FileOutputStream (File file)
  • FileOutputStream(String name)
  • FileOutputStream(String name,boolean append)

字符流

同样我们先来看两个例子:

public class IODemo15 {public static void main(String[] args) {    BufferedWriter bw=null;    try {        bw=new BufferedWriter(new FileWriter("a.txt");        bw.write("hello world");        bw.newLine();// 换行。        bw.flush();    } catch (Exception e) {        // TODO: handle exception    }finally {        CloseUtils.close(bw);    }  }}

上面这个代码的功能是在”a.txt”文件下写入”hello world”内容,我们发现字符流的格式几乎和字节流一模一样
。先构造BufferedWriter对象和FileWrite对象,然后调用write()方法写数据。流对象清空flush(), 流对象关闭close()。

  public static void main(String[] args) {    BufferedReader br=null;    try {        //给FileReader增加了缓存功能        br=new BufferedReader(new FileReader("abc2.txt"));        String line=null;        while((line=br.readLine())!=null){            System.out.println(line);        }    } catch (Exception e) {        // TODO: handle exception    }finally {        CloseUtils.close(br);     }  } }

输出结果

hello world

上面这个代码是为了读取BufferedWriter刚写入的数据。格式也仍然和字节流相类似,构造BufferedReader对象和FileReader对象,然后调用readLine()方法读取数据。

Write类常用方法:

  • write(String str)
  • write(String str,int off,int len)
  • void close();
  • void flush();

子类BufferedWriter常用的构造方法

  • BufferedReader(Writer out)

Reader()常用方法:

  • int read()
  • int read(byte[] c)
  • read(char[]c,int off,int len)
  • void close();

子类BufferedReader特有的方法:

  • readLine()
原创粉丝点击