I/O byte stream in Java, java byte
I/O is the abbreviation of input/output, that is, the input/output system.
The I/O operation reads data from the data source and writes the data to the data destination. There are many types of reading sources (such as files, keyboards, and networks) and writing destinations (such as files, screens, and networks.
The data flow is a reference object of a java program. The input stream is used when the data enters the program, and the output stream is used when the data goes out of the program.
"Stream": A pipe established between the data source and the Program for data flow. The data flows in/out a little bit through the pipe.
I/O classification:
Input stream/output stream, byte stream/worker stream, node stream/Process Stream
Core class of byte stream in I/O
The InputStream class and the OutputStream class are the parent classes of all byte stream classes, both of which are abstract classes. FileInputStream is a common subclass of InputStream. It is used to read data from hard disks. FileOutputStream is a common subclass of OutputStream. It is used to write data to the hard disk.
Core methods:
InputStream:
int read(byte [] b, int off, int len)
The read method is used to read data. The first parameter is a byte array used to store the read data. The second parameter is the offset, which specifies that the read data is stored from the specified position of the array. The third parameter indicates the length of the array, that is, the amount of data that can be read by read each time. The return value of this method is the number of bytes of the data read.
OutputStream:
void write(byte [] b, int off, int len)
The write method is used to write data. The first parameter is the data to be written, and the second parameter is the offset, which specifies that the data is written from the specified position of the array, the third parameter indicates the amount of data to be written to the hard disk.
Example: read and write files
Import java. io. *; // import class Test {public static void main (String args []) {FileInputStream FS = null in the I/O package; // declare that the input stream references FileOutputStream fos = null; // declare that the output stream references try {// generate the input stream object, and locate the file "FS = new FileInputStream" ("F:/Android/Java4Android/32/src/a.txt "); // generate a byte array to access data byte [] B = new byte [15]; // call the read method to read data and return the data size int bLen = fiis. read (B, 0, B. length); // generate the output stream object and generate the file fos = new FileOutputStream ("F:/Android/Java4Android/32/src/B .txt "); // if the file does not exist, the system will automatically generate the file // call the write method to write data to fos. write (B, 0, bLen);} catch (Exception e) {System. out. println (e );}}}
// Print the content of the byte array for (int I = 0; I <B. length; I ++) {System. out. println (B [I]);}
// Restore to String s = new String (B); s = s. trim (); // remove the spaces at the beginning and end of the String and the empty character System. out. println (s );
1 what is the difference between the forward stream and the byte stream in Java I/O?
The byte stream itself does not use a buffer (memory) during operations, but directly operates on the file itself. The swap stream uses a buffer during operations and operates on the file through the buffer.
Two examples can be used to describe:
1: use byte stream not to close the execution
Public static void main (String [] args) throws Exception {// thrown Exception, not processed
// Step 2: use the File class to find an object
File f = new File ("d:" + File. separator + "test.txt"); // declare the File object
// Step 2: instantiate the parent class object through subclass
OutputStream out = null; // prepare an output object.
Out = new FileOutputStream (f); // instantiated by object Polymorphism
// Step 4: Perform the write operation
String str = "Hello World !!! "; // Prepare a string
Byte B [] = str. getBytes (); // string to byte array
Out. write (B); // output the content
// Step 4: Close the output stream
// Out. close (); // not closed
} // You will see the generated file contains "Hello World !!! "
2: Do not close the execution when using the livestream
Public static void main (String [] args) throws Exception {// thrown Exception, not processed
// Step 2: use the File class to find an object
File f = new File ("d:" + File. separator + "test.txt"); // declare the File object
// Step 2: instantiate the parent class object through subclass
Writer out = null; // prepare an output object.
Out = new FileWriter (f); // instantiate through object Polymorphism
// Step 4: Perform the write operation
String str = "Hello World !!! "; // A string
Out. write (str); // output the content
// Step 4: Close the output stream
// Out... the remaining full text>
What is the difference between the byte stream and the byte stream in java?
The performance in the memory doesn't matter whether he knows how much he reads at a time!
Character: two bytes
Byte: one byte
For example, if you want to read a Chinese character (in two bytes, that is, one character ".
1. Read with a forward stream: read two bytes at a time, then you can read it completely and store it in the desired place.
2. throttling by words: If you read a byte at a time, you have to read this Chinese character twice. Otherwise, we will read half of the data (one byte in two Chinese characters). What do you mean by this half (one byte? I don't think that's what you want.
Another useful thing is that you can read a row (readLine () at a time in the pipeline stream. This is a good method for us.