“This is the 15th day of my participation in the August More Text Challenge. For details, see: August More Text Challenge.”

The vast sea of thousands of thousands, thank you for this second you see here. Hope my article is helpful to you!

Wish you in the future, keep love, go to the mountains and seas!

Senior stream I/O

Yesterday we learned how to operate basic data streams in advanced streams, and in the larger system of IO streams, there are some advanced streams waiting for us to unlock.

So without further discussion, today we’re going to look at one of these advanced streams — random access streams.

Random access flow

In our previous study of streams, when you read or write a file, you start at the beginning of the file, and you can’t specify a location to start reading. Now we can solve this problem with RandomAccessFile.

RandomAccessFile is a special stream in the IO stream system. You can read and write to the file, and unlike other input and output streams, you can jump directly to any location in the file to read and write data. So if we want to access only part of the file, we can use the RandomAccessFile class.

Because the RandomAccessFile class contains a record pointer that identifies the location of the current read/write, when the program creates a new RandomAccessFile object, the file record pointer for that object is in the header (i.e., at zero). After n bytes of read/write, The file record pointer will be moved back n bytes. In addition, RandomAccessFile can move the record pointer freely, either forward or backward. RandomAccessFile contains the following two methods to manipulate the record pointer to the file.

  • long getFilePointer(); Returns the current position of the file record pointer
  • void seek(long pos); Position the file record pointer toposlocation

The RandomAccessFile class is not a stream and is a subclass of Object. But it combines the functions of InputStream and OutputStream. Supports random access to read and write files.

A constructor

  • public RandomAccessFile(File file, String mode): creates a random flowFileParameters.
  • public RandomAccessFile(String name, String mode): Creates a random stream and passes it inStringParameter to specify the file name.

Both constructors need to pass a specified mode argument that specifies the mode of access for RandomAccessFile.

Explain each:

  • R: Opens the specified folder in read-only mode.
  • Rw: Reads and writes the specified file.
  • RWS: The contents of the file to be written and its metadata need to be updated.

  • RWD: Only the contents of the files to be written to storage need to be updated.

    One of the most common is the RW mode, which can write data as well as read data.

Read and write the test

The read-write method is basically the same as the previous byte I/O stream. Let’s test the read-read method by specifying the position:

public class RandomAccessFileDemo { public static void main(String[] args) throws IOException { RandomAccessFile raf = new RandomAccessFile("e:\demo\raf.txt", "r"); Write (); Int ch = raf.read(); System.out.println((char) ch); Println (" the current file pointer is: "+ raf.getFilePointer())); // The file pointer can be read from system.out.println (" the current file pointer is:" + raf.getFilePointer()); Ch = raf.read(); System.out.println((char) ch); Println (" the current file pointer is: "+ raf.getFilePointer())); // The file pointer can be read from system.out.println (" the current file pointer is:" + raf.getFilePointer()); // What if I want to read the last d directly? // Set it by the seek method. raf.seek(9); ch = raf.read(); System.out.println((char) ch); Println (" the current file pointer is: "+ raf.getFilePointer())); // The file pointer can be read from system.out.println (" the current file pointer is:" + raf.getFilePointer()); raf.close(); } private static void write() throws IOException {// Create RandomAccessFile Raf = new RandomAccessFile("e:\demo\raf.txt", "rw"); raf.write("helloworld".getBytes()); raf.close(); }} Program execution result: h The position of the pointer to the current file is: 1 e The position of the pointer to the current file is: 2 d the position of the pointer to the current file is: 10Copy the code

Information stored in a file:

conclusion

I believe you have a certain understanding of the random access flow class in the advanced stream IO flow, looking forward to the next chapter of advanced stream – attribute set teaching!

Of course, there are many streams waiting to watch together next time! Welcome to the next chapter!

So far, the world is closed for today, good night! Although this article is over, BUT I still, never finished. I will try to keep writing articles. The coming days are long, and the horse is slow!

Thank you for seeing this! May you be young and have no regrets!

Note: If there are any mistakes and suggestions, please leave a message! If this article is also helpful to you, I hope you give a lovely and kind attention, thank you very much!