IO流程图

 掌握13个Io流

 

一、FileInputStream 和 FileoutputStream

FileInputStream:文件的读取

 1 public class readers
 2 {
 3     public static void main(String[] args) throws Exception
 4     {
 5         File file = new File("F:\\java\\workspace\\Fanshe\\src\\com\\cyg\\fanshe.java");//读取文件
 6         FileInputStream fi = new FileInputStream(file);//创建字节流,打开该 文件
 7         byte[] b = new byte[fi.available()];//fi.available 可以获取文件占多少字节
 8         int a = -1;
 9         while((a= fi.read(b))!= -1)//判断文件是否到达文件末尾
10         {
11             //System.out.println(new String(b));
12         }
13         System.out.println(new String(b));
14         //关闭流
15         fi.close();
16         
17     }
18 }

FileoutputStream:文件的写入

 1 public class output
 2 {
 3     public static void main(String[] args) throws Exception
 4     {
 5         File file = new File("F:\\a.txt");
 6         FileOutputStream out = new FileOutputStream(file);
 7         out.write("abmin".getBytes());
 8         out.flush();//清楚缓存
 9         out.close();//关闭流
10     }    
11 }

 

二、BufferedInputStream 和 BufferedOutputStream 

BufferedInputStream  是一个缓冲流

作用:为另一个输入流添加一些功能,即缓冲输入以及支持 mark (标记)和 reset 重置方法的能力,是通过一个内部缓冲区数组实现的。

 

 1 public class buffertream
 2 {
 3     public static void main(String[] args) throws Exception
 4     {
 5         FileInputStream in = new FileInputStream("E:\\win10 64\\vmware.log");
 6         BufferedInputStream bu = new BufferedInputStream(in);
 7         FileOutputStream out = new FileOutputStream("E:\\a.txt");
 8         BufferedOutputStream bo = new BufferedOutputStream(out);
 9         
10         byte[] b = new byte[1024];
11         int a = bu.read(b);
12         while(a != -1)
13         {
14             //System.out.println(Arrays.toString(b));
15             bo.write(b, 0, a);
16             a = bu.read(b);
17         }
18         in.close();
19         bu.close();
20         
21         bo.flush();
22         out.close();
23         bo.close();
24     }
25 }

运行结果如下

 

三、DataOutputStream 和 DataInputStream

是一个数据输入流 它继承FilterInputStream,用来装饰其他输入流

 1 public class adtastream
 2 {
 3     public static void main(String[] args) throws Exception
 4     {
 5         FileOutputStream out = new FileOutputStream("E:\\b.txt");
 6         DataOutputStream dot = new DataOutputStream(out);//过滤流必须要有一个节点流
 7         
 8         //写入数据
 9         dot.writeInt(10);
10         dot.writeBytes("admin");
11         dot.writeChar('a');
12         dot.writeBoolean(true);
13         
14         out.flush();
15         out.close();
16         dot.close();
17         
18         FileInputStream in = new FileInputStream("E:\\b.txt");
19         DataInputStream di = new DataInputStream(in);    
20         
21         //读取数据的时候要和写入的顺序一样 不然破坏结构
22         System.out.println(di.readInt());
23         System.out.println(di.readByte());
24         System.out.println(di.readChar());
25         System.out.println(di.readBoolean());
26     }
27 }

 

 

四、ByteArrayInputStream 和 ByteArrayOutputStream

ByteArrayInputStream 专门用来读取内存中的数据 

 1 public class bytestream
 2 {
 3     public static void main(String[] args) throws Exception
 4     {
 5         ByteArrayOutputStream out = new ByteArrayOutputStream();
 6         String str = "admin";
 7         out.write(str.getBytes());
 8         byte [] b = new byte[200];
 9         ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes());//ByteArrayInputStream: 专门用来从内从中读取数据的流
10         in.read(b);
11         System.out.println(Arrays.toString(b));
12     }
13 }

打印出来是damin所对应的系统底层源码

 

五、ObjectInputStream 和 ObjectoutputStream

 

六、BufferedReader 和 BufferedWriter