IO stream: byte stream

1. Byte output stream OutPutStream 

java. io. outputstream: This abstract class is the superclass of all classes that represent output byte streams.
Defines some member methods common to subclasses:

  • public void close(): Close this output stream and release any system resources associated with this stream.
  • public void flush(): Flush this output stream and force any buffered output bytes to be written out.
  • public void write(byte[] b): Write b.length bytes from the specified byte array to this output stream.
  • public void write(byte[] b, int off, int len): Write Len bytes from the specified byte array, and output to this output stream from offset off.
  • public abstract void write(int b): output the specified byte to the stream.

java. io.FileOutputStream extends OutputStream
FileOutputstream: File byte output stream

Function: Write the data in memory to the file on the hard disk
Construction method:

  • FileOutputstream(String name) creates an output file stream that writes data to a file with the specified name.
  • FileOutputstream(File file) creates a file output stream that writes data to the file represented by the specified File object.

Parameters: the purpose of writing data
String name: the destination is the path of a file
File file: the destination is a file
The role of the constructor:

  • Create a FileOutputStream object
  • An empty file will be created according to the file/file path passed in the construction method
  • Will point the FiLeOutputStream object to the created file

The principle of writing data (memory-->hard disk)
java program-->JVM (javo virtual machine)-->0S (operating system)-->os call the method of writing data-->write data to a file
Steps to use byte output stream (emphasis):

  • Create a FileOutputstream object and pass the destination of the written data in the constructor
  • Call the method write in the FileOutputStream object to write the data to the file
  • Release resources (stream use will occupy a certain amount of memory, and the memory must be cleared after use to improve the efficiency of the program)

Ways to write multiple bytes at once: 

public void write(byte[] b): Write b. length bytes from the specified byte array to this output stream.

      Write multiple bytes at a time:
      If the first byte written is a positive number (0-127), then the ASCII table will be consulted when it is displayed.
      If the first byte written is a negative number. Then the first byte and the second byte, two bytes form a Chinese display, query the system default code table (GBK)

public void write(byte[] b, int off, int len): from the specified The byte array is written into len bytes and output to this output stream starting from offset off.
public void write(byte[] b, int off, int len): write a part of the byte array to the file
int off: start index of the array
int Len: write a few bytes

 Data append and write

Append write/continue write: Use the two-parameter construction method
FileOutputStream(String name, boolean append) to create an output file stream that writes data to the file with the specified name.
FileOutputStream(File file, boolean append) Creates a file output stream that writes data to the file represented by the specified File object.
Parameters:
string name, File file: the destination of the data to be written
boolean append: append write switch
true: creating an object will not overwrite the source file, continue to append data at the end of the file
false: create a new file, overwrite the source file

Write newline: write newline symbol
windows: \r\n
linux:/n
mac:/r

2. Byte input stream InPutStream

java. io. Inputstream: byte input stream
This abstract class is the superclass of all classes that represent byte input streams.
Defines the methods common to all subclasses:
int read() reads the next byte of data from the input stream.
int read(byte[] b) reads a certain number of bytes from the input stream and stores it in the buffer array b.
void close() Close this input stream and release all system resources associated with the stream.
java. io. FileInputStream extends InputStream
FileInputstream: file byte input stream
Function: read the data in the hard disk file into the memory using the
construction method:
FileInputstream(string name)
FileInputStream(File file)
Parameters : read the data source of the file
String name: the path of the
file File file:
the role of the file construction method:
1. A FileInputStream object will be created
2. The FileInputstream object will be specified in the file to be read in the construction method

Read data (read one)

The principle of reading data (hard disk-->memory)
java program-->JVM-->0S-->OS method of reading data-->reading file.Using
steps of byte input stream (emphasis):
1. Create a FileInputStream object, bind the data source to be read in the constructor
2. Use the method read in the FileInputstream object to read the file
3. Release resources

Reading the file above is an iterative process; it is recommended to use a loop to complete it. When we don't know how many bytes are in the file, use a while loop to complete it.

The while loop end condition, the loop ends when it returns -1.

Boolean expression (len = fis.read())!=-1

1.fis. read(): read a byte
2.len = fis.read(): assign the read byte to the variable len
3.(len = fis.read())!=-1: Determine whether the variable Len is not equal to -1
 

Read data (read multiple)

Byte input stream-the method of reading multiple bytes at a time:
int read(byte[] b) reads a certain number of bytes from the input stream and stores them in the buffer array b.
Two things are clear:
1. What is the role of the method parameter byte[]?

      Play a buffer role,
      the length of multiple byte arrays that are read each time is generally defined as 1024 (1kb) or an integer multiple of 1024.
2. What is the return value of the method int? The number
      of valid bytes read each time

The construction method of the String class
String(byte[] bytes): Convert the byte array to a string
String(byte[] bytes, int offset, int length) Convert a part of the byte array to a string offset: the beginning of the array Index length: the number of bytes converted

I don’t know how many bytes there are in the file, so I use the
while loop to end the while loop and read to the end of -1

Copy of files: read and write

 

Guess you like

Origin blog.csdn.net/weixin_51980491/article/details/113105325