Home >Java >javaTutorial >Example analysis of Java file read and write operations
Java programs allow data transmission with input and output devices through streams. Streams in Java are all in the java.io package and are called IO (input and output) streams. IO streams can be divided into byte streams and character streams according to different operating data. According to the different data transmission directions, they can be divided into input streams and output streams. The program reads data from the input stream and writes to the output stream. Data, in the IO package, the input and output of the byte stream are represented by java.InputStream and java.io.OutputStream respectively, and the input and output of the character stream are represented by java.io.Reader and java.io.Writer respectively.
In the computer, whether it is text, pictures, audio or video, all files exist in binary form. Byte stream is the most commonly used in programs. Streams are divided into byte input streams and byte output streams according to the direction of data transmission. In the JDK, two abstract classes InputStream and OutputStream are provided. They are the top parent classes of byte streams. All byte input streams are Inherit OutputStream.
int read()//Read an eight-bit byte from the input stream and convert it to 0~255 integer between and return this integer
int read(byte[]b)//Read several bytes from the input stream and save them to the byte array specified by parameter b. The returned integer table 4 Number of bytes read
int read(byte[]b,int off,int len)//Read a number of bytes from the input stream and save them to the byte array specified by parameter b, off specifies the word The section array starts to save the starting index of the data, len represents the number of bytes read
void close()//Close this input stream and release all system resources associated with the stream
void write(int b)//Write a byte to the output stream
void write(byte[]b)//Put the byte specified by parameter b Write all bytes of the array to the output stream
void write(byte[]b,int off,int len)//Write len bytes starting from offset off in the specified byte array to the output stream
void flush()//Refresh this output stream and force handwriting of all buffered output bytes
void close()//Close this output stream and release all system resources related to this stream
The flush() method is used to force the data in the current output stream buffer (usually a byte array) to be written to the target device. This process is called flushing. Although the two classes InputStream and OutputStream provide some methods related to reading and writing data, these two classes are abstract classes and cannot be instantiated.
Since the data in the computer is basically stored in the files on the hard disk, when operating the file, the data is read from the file and written to the file. FileInputStream and File Output Stream are two classes for reading and writing operations. Since reading data from a file is a repeated operation, loop statements are required to achieve continuous reading of data.
package IO; import java.io.FileInputStream; public class Li01 { public static void main(String[] args) throws Exception{ FileInputStream in=new FileInputStream("test.txt"); int b=0; while(true){ b=in.read(); if(b==-1){ break; } System.out.print(b+" "); } in.close(); } }
Demonstration results
#The reason why the numbers are output is because the files on the hard disk exist in the form of bytes. When reading the file data , you must ensure that the file exists in the corresponding directory and is readable.
package IO; import java.io.FileOutputStream; import java.io.OutputStream; import java.nio.charset.StandardCharsets; public class Li02 { public static void main(String[] args) throws Exception{ OutputStream out=new FileOutputStream("example1.txt"); String str="好好学习,天天向上!"; byte[]b=str.getBytes(StandardCharsets.UTF_8); for(int i=0;i<b.length;i++){ out.write(b[i]); } out.close(); } }
When writing data through FileOutputStream, the file example1.txt is automatically created and the data is written to the file , if you write content to an existing file, first the content of the file will be cleared, and then new data will be written. If you want to append content to an existing file, you can add a boolean value after the file name and Set to true.
package IO; import java.io.FileOutputStream; import java.io.OutputStream; import java.nio.charset.StandardCharsets; public class Li02 { public static void main(String[] args) throws Exception{ OutputStream out=new FileOutputStream("example1.txt",true); String str="好好学习,天天向上!"; byte[]b=str.getBytes(StandardCharsets.UTF_8); for(int i=0;i<b.length;i++){ out.write(b[i]); } out.close(); } }
The above is the detailed content of Example analysis of Java file read and write operations. For more information, please follow other related articles on the PHP Chinese website!