类图1:
彻底攻克java流

IO分两种流

字节流 InputStream OutputStream

字符流 Reader  Writer

他们都是抽象类

具体实现
字节流 FileInputStream  FileOutputStream
字符流 FileReader    FileWriter


字节流转换成字符流可以用 InputSteamReader  OutputStreamWriter

转换成BufferdReader  BufferedWriter 他们具有缓冲区

例如:读取文件 从字节流输入到字符流输入
定义一个字节流:
Java代码  彻底攻克java流
  1. FileInputStream fileInputStream = new FileInputStream("d:/text.txt");  // 定义一个指向D:/TEXT.TXT 的字节流  
  2.   
  3. InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);  
  4. //字节流转换成InputStreamReader  
  5.   
  6. BufferedReader bufferedReader = new BufferedReader(inputSteamReader);  
  7. //InputStreamReader 转换成带缓存的bufferedReader  


可以把读出来的内容赋值给字符
Java代码  彻底攻克java流
  1. String ss = new String();  
  2. String s;  
  3. while((s = bufferedReader.readLine())!=null){  
  4.     ss += s;  
  5. }  

例如:写入文件  从字节流输出到字符流输出
Java代码  彻底攻克java流
  1. FileOutputStream fileOutputStream = new FileOutputStream("d:/text.txt"); //定义一个指向D:/TEXT.TXT文件   
  2.   
  3. OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);  
  4.   
  5. BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);  
  6.   
  7. bufferedWriter.write(s);  
  8.   
  9. bufferedWriter.close();  
  10. outputStreamWriter.close();  
  11. fileOutputStream.close();  



彻底攻克java流


彻底攻克java流


彻底攻克java流


彻底攻克java流

上面这个图向我们传达了这个信息:链接流链接流对象接收一个原始流对象或者另外一个链接流对象作为流源;另一方面他们对流源的内部工作方法做了相应的改变,这种改变是装饰模式所要达到的目的。比如:

  BufferedInputStream“装饰”了InputStream的内部工作方式,使得流的读入操作使用了缓冲机制。在使用了缓冲机制后,不会对每一次的流读入操作都产生一个物理的读盘动作,从而提高了程序的效率,在汲及到物理流的读入时,都应当使用这个装饰流类。

  LineNumberInputStream和PushbackInputStream也同样“装饰”了InputStream的内部工作方式,前者使得程序能够按照行号读入数据;后者能够使程序读入的过程中,退后一个字符。

  DataInputStream子类读入各种不同的原始数据类型以及String类型的数据,这一点可以从它提供的各种read方法看出来,如:readByte(),readInt(),readFloat()等。

使用基本原则:

根接口是InputStream/OutputStream充当数据源的IO类有FileInputStream/FileOutputStream,ByteArrayInputStream  / ByteArrayOutputStream  等,充当装饰功能的IO类有BufferedInputStream  /   BufferedOutputStream,DataInputStream   /   DataOutputStream等, 它们都是继承装饰接口FilterInputStream/FilterOutputStream。使用IO时,首先创建一个数据源IO,然后根据需要的功能创建装饰类IO,其构造函数的参数为已创建的数据源IO 常用类举例说明:
FileInputStream和FileOutputStream
    简单略

ObjectOutputStream和ObjectInputStream
    这个流的作用是,直接将一个对象转换为字节流,对象必须implements Serializable
Java代码  彻底攻克java流
  1. package demo;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.io.ObjectInputStream;  
  8. import java.io.ObjectOutputStream;  
  9. import java.io.Serializable;  
  10.   
  11. public class ObjectStreamTest  
  12. {  
  13.     public static void main(String[] args)  
  14.     {  
  15.         T t = new T();  
  16.         t.k = 15;  
  17.   
  18.         try  
  19.         {  
  20.             FileOutputStream fos = new FileOutputStream("d:/333.txt");  
  21.             ObjectOutputStream oos = new ObjectOutputStream(fos);  
  22.   
  23.             oos.writeObject(t);  
  24.   
  25.             fos.close();  
  26.             oos.close();  
  27.         }  
  28.         catch (IOException e)  
  29.         {  
  30.             e.printStackTrace();  
  31.         }  
  32.   
  33.         try  
  34.         {  
  35.             FileInputStream fis = new FileInputStream("d:/333.txt");  
  36.             ObjectInputStream ois = new ObjectInputStream(fis);  
  37.   
  38.             T t2 = (T) ois.readObject();  
  39.   
  40.             System.out.println(t2.i);  
  41.             System.out.println(t2.k);  
  42.             System.out.println(t2.s);  
  43.             System.out.println(t2.j);  
  44.         }  
  45.         catch (FileNotFoundException e)  
  46.         {  
  47.             e.printStackTrace();  
  48.         }  
  49.         catch (IOException e)  
  50.         {  
  51.             e.printStackTrace();  
  52.         }  
  53.         catch (ClassNotFoundException e)  
  54.         {  
  55.             e.printStackTrace();  
  56.         }  
  57.     }  
  58.   
  59. }  
  60.   
  61. @SuppressWarnings("all")  
  62. class T implements Serializable  
  63. {  
  64.     int i = 20;  
  65.     short j = 10;  
  66.     String s = "hello";  
  67.     int k = 100;  
  68. }  


DataInputStream和DataOutputStream
    这个是数据流.给我们直接处理基本数据类型的接口,举个最基本的例子,如果我们要将一个float类型的数据写入文件,我们需要先转换成String类型,然后转换成字节数组,这样才能存入..现在我们可以直接用DataInputStream和DataOutputStream来解决这个问题
Java代码  彻底攻克java流
  1. package demo;  
  2.   
  3. import java.io.ByteArrayInputStream;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.DataInputStream;  
  6. import java.io.DataOutputStream;  
  7. import java.io.IOException;  
  8.   
  9. public class DataStreamTest  
  10. {  
  11.     public static void main(String[] args)  
  12.     {  
  13.         //ByteArrayOutputStream可以临时缓存一个对象的字节数组  
  14.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  15.         DataOutputStream dos = new DataOutputStream(baos);  
  16.   
  17.         try  
  18.         {  
  19.             dos.writeChar('d');  
  20.             dos.writeInt(10);  
  21.             dos.writeShort(50);  
  22.         }  
  23.         catch (IOException e)  
  24.         {  
  25.             e.printStackTrace();  
  26.         }  
  27.   
  28.         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());//ByteArrayOutputStream的用法  
  29.         DataInputStream dis = new DataInputStream(bais);  
  30.   
  31.         try  
  32.         {  
  33.             System.out.println(dis.available());  
  34.             System.out.println(dis.readChar());  
  35.             System.out.println(dis.readInt());  
  36.             System.out.println(dis.readShort());  
  37.         }  
  38.         catch (IOException e)  
  39.         {  
  40.             e.printStackTrace();  
  41.         }  
  42.   
  43.         try  
  44.         {  
  45.             dos.close();  
  46.             dis.close();  
  47.         }  
  48.         catch (IOException e)  
  49.         {  
  50.             e.printStackTrace();  
  51.         }  
  52.     }  
  53. }  


BufferedInputStream和BufferedOutputStream
    BufferedInputStream当使用read()方法时,实际上是先读取buf中的数据,而不是直接对数据来源作读取。当buf中的数据不足时,BufferedInputStream才会再实现给定的InputStream对象的read()方法,从指定的装置中提取数据。
    BufferedOutputStream当使用write()方法写入数据时实际上会先将数据写到buf中,当buf已满时才会实现给定的OutputStream对象的write()方法,将buf数据写到目的地,而不是每次都对目的地作写入的动作

Java代码  彻底攻克java流
  1. package demo;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedOutputStream;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileOutputStream;  
  8. import java.io.IOException;  
  9.   
  10. public class BufferedStreamDemo  
  11. {  
  12.     public static final void copy(File srcFile, File distFile) throws IOException  
  13.     {  
  14.         FileInputStream fin = null;  
  15.         BufferedInputStream bis = null;  
  16.         FileOutputStream fout = null;  
  17.         BufferedOutputStream bos = null;  
  18.   
  19.         try  
  20.         {  
  21.             if (distFile.exists())  
  22.             {  
  23.                 distFile.delete();  
  24.             }  
  25.   
  26.             byte[] data = new byte[1024];  
  27.   
  28.             fin = new FileInputStream(srcFile);  
  29.             bis = new BufferedInputStream(fin);  
  30.             fout = new FileOutputStream(distFile);  
  31.             bos = new BufferedOutputStream(fout);  
  32.   
  33.             int readCount = 0;  
  34.             while ((readCount = bis.read(data)) != -1)  
  35.             {  
  36.                 bos.write(data, 0, readCount);  
  37.             }  
  38.   
  39.             // 将缓冲区中的数据全部写出  
  40.             bos.flush();  
  41.         }  
  42.         finally  
  43.         {  
  44.             try  
  45.             {  
  46.                 if (bos != null)  
  47.                 {  
  48.                     bos.close();  
  49.                 }  
  50.             }  
  51.             catch (IOException e)  
  52.             {  
  53.             }  
  54.             try  
  55.             {  
  56.                 if (bis != null)  
  57.                 {  
  58.                     bis.close();  
  59.                 }  
  60.             }  
  61.             catch (IOException e)  
  62.             {  
  63.             }  
  64.             try  
  65.             {  
  66.                 if (fin != null)  
  67.                 {  
  68.                     fin.close();  
  69.                 }  
  70.             }  
  71.             catch (IOException e)  
  72.             {  
  73.             }  
  74.             try  
  75.             {  
  76.                 if (fout != null)  
  77.                 {  
  78.                     fout.close();  
  79.                 }  
  80.             }  
  81.             catch (IOException e)  
  82.             {  
  83.             }  
  84.         }  
  85.     }  
  86.       
  87.     public static void main(String[] args)  
  88.     {  
  89.         try  
  90.         {  
  91.             BufferedStreamDemo.copy(new File("c:\\test1.abs"), new File("c:\\test2.abs"));  
  92.         }  
  93.         catch (IOException e)  
  94.         {  
  95.             e.printStackTrace();  
  96.         }  
  97.     }  
  98. }  


InputStreamReader和OutputStreamWriter:字符流和字节流之间的转换
    Reader 输入流对应于不同的数据源:
    FileReader 用于从文件输入;
    CharArrayReader 用于从程序中的字符数组输入;
    StringReader 用于从程序中的字符串输入;

   InputStreamReader从一个数据源读取字节,并自动将其转换成Unicode字符
    OutputStreamWriter将字符的Unicode编码写到字节输出流

Java代码  彻底攻克java流
  1. package demo;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.BufferedWriter;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.io.InputStreamReader;  
  9. import java.io.OutputStreamWriter;  
  10.   
  11. public class InputStreamReaderTest  
  12. {  
  13.   
  14.     public static void main(String[] args)  
  15.     {  
  16.         try  
  17.         {  
  18.             FileInputStream fis = new FileInputStream("d:/222.txt");  
  19.             InputStreamReader isr = new InputStreamReader(fis);  
  20.             BufferedReader br = new BufferedReader(isr);// 这里又用BufferedReader封装起来,其实就是为了可以使用它的ReadLine方法  
  21.   
  22.             FileOutputStream fos = new FileOutputStream("d:/555.txt");  
  23.             OutputStreamWriter osw = new OutputStreamWriter(fos);  
  24.             BufferedWriter bw = new BufferedWriter(osw);// 这里同样的用BufferedWriter封装起来,就是为了可以使用它的writer(String )方法..可以看API,其他的都只能write Int类型的数据或者byte类型的  
  25.   
  26.             String s2 = null;  
  27.   
  28.             while ((s2 = br.readLine()) != null)  
  29.             {  
  30.                 bw.write(s2);  
  31.                 bw.newLine();  
  32.             }  
  33.   
  34.             bw.close();  
  35.             br.close();  
  36.         }  
  37.         catch (IOException e)  
  38.         {  
  39.             e.printStackTrace();  
  40.         }  
  41.     }  
  42. }  


疑问:
1、ByteArrayOutputStream和BufferedOutputStream区别
    本来这2个没法比较的,非得比较可以为:
(1)、构造方法不一样;
(2)、byte[]转换为InputStream流:InputStream sbs = new ByteArrayInputStream(byte[] buf);
(3)、InputStream转换为byte[]数组
Java代码  彻底攻克java流
  1. ByteArrayOutputStream swapStream = new ByteArrayOutputStream();  
  2. byte[] buff = new byte[100]; //buff用于存放循环读取的临时数据  
  3. int rc = 0;  
  4. while ((rc = inStream.read(buff, 0100)) > 0) {  
  5.     swapStream.write(buff, 0, rc);  
  6. }  
  7. byte[] in_b = swapStream.toByteArray(); //in_b为转换之后的结果  


2、ArrayList一个基础问题,关于 transient

ArrayList 里面定义:
    private transient Object[] elementData;  
    这里是ArrayList.elementData定义的地方,而elementData是用来存储实际内容的数组, 而transient字是表明域不能序列化,而我在这里能打印出 a b c,这个对transient怎么理解?

Java代码  彻底攻克java流
  1. package demo;  
  2.   
  3. import java.io.ByteArrayInputStream;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.ObjectInputStream;  
  7. import java.io.ObjectOutputStream;  
  8. import java.util.ArrayList;  
  9.   
  10. public class WriteObject  
  11. {  
  12.     public static void main(String[] args)  
  13.     {  
  14.         ArrayList<String> al = new java.util.ArrayList<String>();  
  15.         al.add("a");  
  16.         al.add("b");  
  17.         al.add("c");  
  18.         try  
  19.         {  
  20.             ByteArrayOutputStream stream = new ByteArrayOutputStream();  
  21.             ObjectOutputStream out = new ObjectOutputStream(stream);  
  22.             out.writeObject(al);  
  23.             byte[] alBytes = stream.toByteArray();  
  24.   
  25.             ArrayList<String> all = (ArrayList<String>) new ObjectInputStream(new ByteArrayInputStream(alBytes)).readObject();  
  26.             for (String s : all)  
  27.             {  
  28.                 System.out.println(s);  
  29.             }  
  30.         }  
  31.         catch (IOException e)  
  32.         {  
  33.             e.printStackTrace();  
  34.         }  
  35.         catch (ClassNotFoundException e)  
  36.         {  
  37.             // TODO Auto-generated catch block  
  38.             e.printStackTrace();  
  39.         }  
  40.     }  
  41. }  


序列化有2种方式:

A、只是实现了Serializable接口。

  序列化时,调用java.io.ObjectOutputStream的defaultWriteObject方法,将对象序列化。

注意:此时transient修饰的字段,不会被序列化。

B、实现了Serializable接口,同时提供了writeObject方法。

  序列化时,会调用该类的writeObject方法。而不是java.io.ObjectOutputStream的defaultWriteObject方法。

注意:此时transient修饰的字段,是否会被序列化,取决于writeObject。

分类:

技术点:

相关文章: