Okay, let's learn about the data flow in Java.
concept
* IO stream is used to process data transmission between devices* Java operates on data through streaming
* Java classes for operation flow are in the IO package
* Flow is divided into two kinds according to flow direction: input flow and output flow.
* Flows are divided into two types according to the type of operation:
* Byte streams: Byte streams can manipulate any data, because any data in a computer is stored in bytes.
* Character stream: Character stream can only operate pure character data, which is more convenient.
First, let's talk about the inheritance system of IO streams:
Abstract parent class of 1 byte stream
InputStream
OutputStream
2-character stream Abstract parent class
Reader
Writer
Okay, let's start with FileInputStream and FileOutputStream.
In fact, we only need to know its construction method and read and write method, anyway, I am so simple to ask myself.
First let's look at how the book constructs FileInputStream:
Okay, let's look at his read method:FileInputStream fileInputStream = new FileInputStream(new File("text1.text"));//The parameter is File object FileInputStream fileInputStream = new FileInputStream("text1.text");//The parameter is String
int read() Read a data byte from the input stream. // The return value is the code table value, if read to the end return - 1; int read(byte[] b) From this input stream, the data of up to b.length bytes is read into an array of bytes. // The return value returns the number of bytes read, and - 1 if the end of the file is read.
Let's take a look at FileOutput Stream:
First, how to create the object:
FileOutputStream(File file) Creates a file output stream that writes data to a file represented by the specified File object. // Note that when there is no file, the file is automatically created, and if the file exists, the contents of the file are emptied and re-entered. FileOutputStream(File file, boolean append) Creates a file output stream that writes data to a file represented by the specified File object. // It can be input on demand in the original document content. FileOutputStream(String name) Creates a file output stream that writes data to a file represented by the specified File object. // Same as above FileOutputStream(String name, boolean append) Creates a file output stream that writes data to a file represented by the specified File object.
void write(byte[] b) Writes b.length bytes from the specified byte array to the file output stream. void write(byte[] b, int off, int len) Writes len bytes from offset off in the specified byte array to the file output stream. void write(int b) Writes the specified byte to the file output stream.
Needless to say, then close ()
Oh, let's take a look at some of his small applications.Copy Mode I of Documents
This method can achieve the copy of files, but if the file is too large, the copy process will be very slow. We know that we access memory very quickly, but access to the hard disk is very silent. Every read we perform is equivalent to a read of the hard disk, and every write is equivalent to a write of the hard disk. That's too inefficient!public static void main(String[] args) throws IOException { FileInputStream fileI = new FileInputStream("text1.text"); FileOutputStream fileO = new FileOutputStream("text2.text"); int i; while( (i = fileI.read()) != -1) { fileO.write(i); } fileO.close(); fileI.close(); }
Copy Mode II of Documents
You can see that our successful entrance exam is indeed very efficient, but fortunately this is a small document, if the file reaches tens of GB, you should know that our definition of byte [station is memory, we have dozens of GB of memory, anyway, I am not.public static void main(String[] args) throws IOException { FileInputStream fileI = new FileInputStream("text1.text"); FileOutputStream fileO = new FileOutputStream("text2.text"); //If we know the number of bytes in a file and then define a byte array equal to the number of bytes, it only needs to read and write once, is it very effective? int length = fileI.available(); byte[] b = new byte[length]; fileI.read(b); fileO.write(b); fileO.close(); fileI.close(); }
Copy Mode 3 of Documents
public static void main(String[] args) throws IOException { FileInputStream fileI = new FileInputStream("Xu Wei.mp3"); FileOutputStream fileO = new FileOutputStream("Xu Wei 2.mp3"); //Well, since defining a larger array is not a good idea, let's define a smaller array. Anyway, our goal is to reduce the operation of reading hard disks. byte[] b = new byte[1024 * 8];//Note that our definition is essentially an integer multiple of 1024. int length; while((length = fileI.read(b)) != -1) { fileO.write(b, 0, length);//To prevent the last read-in of unrelated elements } fileO.close(); fileI.close(); }
There's a fourth way to copy documents. Before we talk about the fourth way, let's first look at these two categories:
Buffered Input Stream and Buffer Output Stream
* A: Buffering Thought* Byte streams can read and write an array at a time much faster than they can read and write an array at a time.
* This is the addition of buffer effects such as arrays, when java itself is designed,
* Considering this design idea (explained later in the decorative design pattern), a byte buffer stream is provided.
* B.BufferedInputStream
* BufferedInputStream has a built-in buffer (array)
* When reading a byte from Buffered Input Stream
* BufferedInputStream reads 8192 files at one time, exits a buffer, and returns one to the program.
* When the program reads again, it does not need to look for the file, but gets it directly from the buffer.
* It was not until all of the buffers were used that 8192 files were re-read from the file.
* C.BufferedOutputStream
* Buffered Output Stream also has a built-in buffer (array)
* When a program writes bytes to a stream, it does not write directly to a file, but first to a buffer.
* Buffered Output Stream does not write data in the buffer to a file at once until the buffer is full
Fourth Way to Copy Final Documents
public static void main(String[] args) throws IOException { FileInputStream fileI = new FileInputStream("Xu Wei.mp3"); FileOutputStream fileO = new FileOutputStream("Xu Wei 2.mp3"); BufferedInputStream bufferedInputStream = new BufferedInputStream(fileI);//Encapsulation of InputStream Objects BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileO);//Encapsulation of OutputStream Objects int i; while((i = bufferedInputStream.read()) != -1) {//In fact, Buffered Input Stream has read 8192 bytes into memory. We read one byte in memory, which is very fast. bufferedOutputStream.write(i);//We actually wrote it into an array of bytes in BufferedOutputStream, which is full of flush () methods that are called to brush into the hard disk. } bufferedInputStream.close(); bufferedOutputStream.close(); }
Oh, that's the copy of the file, and so is the transmission.
Okay, let's take a look at the standard exception handling code for streams. There are two versions.
Old version:
FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream("aaa.txt"); fos = new FileOutputStream("bbb.txt"); int b; while((b = fis.read()) != -1) { fos.write(b); } } finally { try { if(fis != null) fis.close(); fis = null; }finally { if(fos != null) fos.close(); fos = null; } }
New version:
try(FileInputStream fis = new FileInputStream("aaa.txt");
FileOutputStream fos = new FileOutputStream("bbb.txt");){
int b;
while((b = fis.read()) != -1) {
fos.write(b);
}}
What's going on here? Should we not shut down resources?The reason is that the stream object created in try() must implement the AutoCloseable interface. If it is implemented, it will be called automatically after {} (read and write code) is executed, and the close method of the stream object will turn off the flow.
Okay, let's look at an application.
Picture encryption
public static void main(String[] args) throws IOException { jiami(); jiemi(); } public static void jiemi() throws FileNotFoundException, IOException { FileInputStream fileI = new FileInputStream("nvsheng.jpg"); FileOutputStream fileO = new FileOutputStream("nvsheng2.jpg"); BufferedInputStream bufferedInputStream = new BufferedInputStream(fileI); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileO); int i; while((i = bufferedInputStream.read()) != -1) { bufferedOutputStream.write(i^123);//We do it XOR and get its original value. } bufferedInputStream.close(); bufferedOutputStream.close(); } public static void jiami() throws FileNotFoundException, IOException { FileInputStream fileI = new FileInputStream("btmf.jpg"); FileOutputStream fileO = new FileOutputStream("nvsheng.jpg"); BufferedInputStream bufferedInputStream = new BufferedInputStream(fileI); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileO); int i; while((i = bufferedInputStream.read()) != -1) { bufferedOutputStream.write(i^123);//Our secret key is to encrypt its XOR 123. } bufferedInputStream.close(); bufferedOutputStream.close(); }
Not finished yet!