1. buffered stream
1. Definitions
Buffered stream, the stream is high, is an enhancement to the four basic FileXxx stream, so the flow is four
- Byte stream buffer:
BufferedInputStream
,BufferedOutputStream
- Character buffer stream:
BufferedReader
,BufferedWriter
The basic principle of the buffer stream, it is to create objects, creates a built-in default size of the buffer array, reading and writing through the buffer zone to reduce the number of system io
2. byte stream buffer
Construction method
public BufferedInputStream(InputStream in)
: Create a new buffered stream objectpublic BufferedOutputStream(OutputStream out)
: Create a new buffered stream object
For example construction
// 创建字节缓冲输入流
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("a.txt")); // 创建字节缓冲输出流 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("b.txt"));
Use case realization stream buffer
public class BufferedDemo{
public static void main(String[] args){ // 记录开始时间 long start = System.currentTimeMillis(); // 创建流对象 try( BufferedInputStream bis = new BufferedInputStream(new FileInputStream("jdk.exe")); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.exe")); ){ //读写数据 int b; while((b=bis.read())!=-1){ bos.write(b); }catch(IOException e){ e.printStackTrace(); } // 记录结束的时间 long end = System.currentTImeMillis(); System.out.println("索要时间为"+(end-start)+"毫秒"); } } }
Using arrays, speed
public class BufferedDemo{
public static void main(String[] args){ // 记录开始时间 long start = System.currentTimeMillis(); // 创建流对象 try( BufferedInputStream bis = new BufferedInputStream(new FileInputStream("jdk.exe")); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.exe")); ){ //读写数据 int len; byte[] bytes = new byte[8*1024]; while((len=bis.read())!=-1){ bos.write(bytes, 0, len); }catch(IOException e){ e.printStackTrace(); } // 记录结束的时间 long end = System.currentTImeMillis(); System.out.println("索要时间为"+(end-start)+"毫秒"); } } }
3. character buffer flow
Construction method
public BufferedReader(Reader in)
: Create a new buffered input streampublic BufferedWriter(Writer out)
: Creates a new buffered output stream
For example construction
// 创建字符缓冲输入流
BufferedReader br = new BufferedReader(new FileReader("br.txt")); // 创建字符缓冲输出流 BufferedWriter bw = new BufferedWriter(new FileWriter("bw.txt"));
Unique method
The basic method is consistent character buffer stream and the general stream of characters is called
BufferedReader
:public String readLine()
: Read a line of textBuffferedWriter
:public void newLine()
: Writing a line line separator, there are system-defined symbol
readLine
Way to demonstrate
public class BufferedReaderDemo{
public static void main(String[] args) throws IOException{ //创建流对象 BufferedReader br = new BufferedReader(new FileReader("in.txt")); //定义字符串,保存读取的一行文字 String line = null; //循环读取,读到最后返回null while((line=br.read())!=null){ System.out.println(line); System.out.println("-----"); } //释放资源 br.close(); } }
newLine
Way to demonstrate
public class BufferedWriterDemo{
public static void main(String[] args) throws IOException{ // 创建流对象 BufferedWriter bw = new BufferedWriter(new FileWriter("bw.txt")); //写出数据 bw.write("黑马"); bw.newLine(); bw.write("程序"); bw.newLine(); //释放资源 bw.close(); } }
2. Conversion flow
1. IuputStreamReader
class
Commutations java.io/InputStreamReader
is Reader
a child class of bytes to read, using the specified character set and decodes it into a character.
Construction method
InputStreamReader(InputStream in)
: Create one using the default character set of character streamInputStreamReader(InputStream in, String charsetName)
: Create a character set specified character stream
For example construction
InputStreamReader isr = new InputStreamReader(new FileInputStream("in.txt"));
InputStreamReader isr = new InputStreamReader(new FileInputStream("in.txt"), "GBK");
Specifies the code reading
public class ReaderDemo{
public static void main(String[] args) throws IOException{ // 定义文件位置, 文件为gbk编码 String FileName = "E:\\file_txt"; // 创建流对象指定为GBK InputStreamReader isr = new InputStreamReader(new FileInputStream(FileName), "GBK"); //定义变量, 保存字符 int read; //循环读取 while((read=isr.read())!=-1){ System.out.println((char) read); } //关闭资源 isr.close(); } }
2. OutputStreamWrite
class
It is Write
subclass
Construction method
OutputStreamWriter(OutputStream in)
: Create one using the default character set of character streamOutputStreamWriter(OutputStream in, String chasetName)
: Create a character set specified character stream
Configuration examples
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("a.txt"));
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("a.txt"), "GBK");
Specify the encoding write
public class OutputDemo{
public static void main(String[] args) throws IOException{ //定义文件路径 String FileName = "E:\\out.txt"; //创建流对象 OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(FileName), "GBK"); osw.write("你好"); osw.close(); } }
3. Serialization
Provides for a Java object serialization mechanisms, an object can be represented by a sequence of bytes, the byte containing the sequence , and other information. After a sequence of bytes written to the file, the file corresponding to the information stored in a persistent object. 对象的数据
对象的 类型
对象中存储的属性
Conversely, the byte sequence can also be read from a file back, rearranged, it is deserialized. 对象的数据
, 对象的类型
And the 对象中 存储的数据
information can be used to create objects in memory.
1.ObjectOutputStream类
Construction method
public ObjectOutputStream(OutputStream out)
: A wear member designated the OutputStream ObjectOutputStream
Configuration examples
FileOutputStream FileOut = new FileOutputStream("a.txt");
ObjectOutputStream out = new ObjectOutputStream(FileOut);
Sequence of Operation
1. An object serialization order, two conditions must be met
- This class is the
java.io.Serializable
interfaceSerializable
is a marker interface does not implement this interface will not use any state serialization and de-serialization throwsNotSerializableException
- All the attributes of the class must be serialized, if there is a property not to be serialized, then change the property must be marked transient, using the
transient
keyword modifications
public class Employle implements java.io.Serializable{ public String name; public String address; public transient int age; //tanisent瞬态修饰成员, 不会被序列化 }
2. Write Object Methods
public final void writeObject(Object obj)
: The development of object write
public class Serializable{
public static void main(String[] args){ Employee e = new Employee(); e.name = "张三"; e.address = "北大"; e.age = 20; try{ //创建序列化流对象 ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("a.txt")); //写出对象 out.writeObject(e); //释放资源 out.close(); Fileout.close(); System.out.println("Serializable 打他 is saved") }catch(IOException i){ i.printStackTrace(); } } }
2. ObjectInputStream
class
Construction method
public ObjectInputStream(InputStream in)
: A wear member designated the InputStream ObjectInputStream.
1 deserialization
If you can find a class file object, we can deserialize
public final Object readObject()
: Reads an object
public class DeserialixeDemo{
public static void main(String[] args){ Employee e = null; try{ //创建反序列化流 FileInputStream fileIn = new FileInputStream("a.txt"); ObjectInputStream in = new ObjectInputStream(FileIn); //读取一个对象 e = (Employee) in .readObject(); //释放资源 in.close(); fileIn.close(); }catch(IOException i){ //捕获异常 i.printStackTrace(); return; }catch(ClassNotFoundException c){ // 捕获找不到异常 System.out.println("not found"); c.printStrackTrace(); return; } // 无异常,直接打印输出 System.out.println(e.name); System.out.println(e.address); System.out.println(e.age); } }
Deserialize 2
In addition, when the JVM deserialize objects to find class files, but the class files have changed after a serialized object, then the deserialization operation
As also fails, throw a InvalidClassException exception. This exception occurs for the following reasons:
- Such serial version number of the version number of the class descriptor read from the stream does not match
- This class contains unknown data type
- No parameters such constructions are not accessible method
Serializable interface to require serialization class that provides a serial version number. SerialVersionUID purpose is to verify the version number and the corresponding sequence of the target classes whether to match the version
public class Employee implements java.io.Serializable{ //加入序列版本号 private static final long serialVersionUID = 1L; public String name; public String address; //添加新的属性,重新编译 public int eid; public void adressCheck(){ System.out.println("adress check"+name+"--"+adress); } }
4. Print stream
Usually we use the console to print, print和println
from the java.io.PrintStream
class
1.PrintStream class
Construction method
public PrintStream(String FileName)
: Specified file name to create a new print stream
Configuration examples
PrintStream ps = new PrintStream("ps.txt");
Change the flow of print
System.out
It is the PrintStream
type of
public class PrintDemo{
public static void main(String[] args) throws IOException{ //调用系统的打印流 System.out.println(97); //创建打印流,指定文件的名称 PrintStream ps = new PrintStream("ps.txt"); // 设置系统的流向到ps.txt中 System.setOut(ps); //调用打印的方法, ps.txt中输出97 System.out.println(97); } }