Beginners Section 39 IO-Byte Stream

announcement notice

This official account is purely a personal public welfare official account! I just want to help all java beginners. The official account has a series of free resources for the majority of java beginners to learn by themselves! There is also a WeChat exchange group for everyone to learn and discuss! ! ! You can pay more attention! ! ! You can also leave a lot of comments on our articles and leave your valuable comments! ! !

FileInputStream

The stream is used to read data from the file, and its objects can be created with the keyword new. There are various constructors that can be used to create objects, mostly reading bytes.

You can create an input stream object to read a file using a filename of type string.

InputStream f = new FileInputStream("C:/java/1.txt");

You can also use a file object to create an input stream object to read from a file. We first have to use the File() method to create a file object

File f = new File("C:/java/1.txt");

FileInputStream out = new FileInputStream(f);

all methods



Several common methods are described below.

public void read() throws IOException{}

public int read(int r)throws IOException{}

read(byte[] b, int off, int len) throws IOException{}

The operation method is very similar to that of FileReader, so I will demonstrate one.



The native method worth mentioning
  Why is the implementation of the above core methods simple, because the workload is all implemented in the native method, that is, in the JVM. There are quite a few natives, let’s list them one by one:

    native void open(String name) // Open the file, in order to read the file content in the next step

    native int read0() // read a byte from the file input stream

    native int readBytes(byte b[], int off, int len) // Read from the file input stream, len bytes from the off handle, and store into the b byte array.

    native void close0() // Close the file input stream and related resources. For example, if the FileChannel pair of the file input stream is obtained, the FileChannel needs to be closed.

Others worth mentioning are that in jdk1.4, the NIO package (which will be described in the subsequent advanced stages of NIO) has been added to optimize the speed of some IO processing, so FileChannel has been added to FileInputStream and FileOutputStream. getChannel() method. That is, get the java.nio.channels.FileChannel object related to the file input stream.


2

FileOutputStream

This class is used to create a file and write data to the file.

If the destination file does not exist before the stream opens the file for output, the stream creates the file.

There are two constructors that can be used to create FileOutputStream objects.

Create an output stream object with a filename of type string:

OutputStream f = new FileOutputStream("C:/java/1.txt ")

You can also use a file object to create an output stream to write to the file. We first have to use the File() method to create a file object:

File f = new File("C:/java/hello");

OutputStream f = new FileOutputStream(f);



Introduce several commonly used methods

public void read() throws IOException{}

public int read(int r)throws IOException{}

read(byte[] b, int off, int len) throws IOException{}

The operation method is very similar to FileWriter, so I will demonstrate one.



There will be other integration methods above, everyone can test it by themselves, and I will not demonstrate them one by one here.

There are other stream objects that are basically used in the same way, so I won't explain them one by one. They basically appear in pairs. For example: InputStream, OutputStream

 

Exercise: Use a program to create a text and write the data, then read the data. What methods and constructors to use, everyone is free.

Enter "FileOutputStream job" in the official account

    Scan the code to follow ∣ there are troublemakers to detour


Long press, identify the QR code, and follow

Note: This official account is purely a personal public welfare official account!

There is no recruitment information for any training institutions! !


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324811351&siteId=291194637