Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods of Java file Icano

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what are the methods of Java file Icano". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

First, there are three common types of file read-in and output streams: FileInputStream/FileOutputStream,FileReader/FileWriter,RandomAccessFile. Here are some simple examples:

Fundamentals:

1.

FileRead fr = new FileReader (filename); String s; while ((s=fr.readLine ())! = null) {...} fr.close (); / / FileWriter similarly, all Reader and Writer in the write () function / / Java Imax O are output oriented to the character stream.

two。

FileInputStream fi = new FileInputStream (filename); int in; while ((in=fi.read ())! =-1) {.} fi.close (); / / FileOutputStream synonym / / Java I FileOutputStream O all Reader and Writer in Byte stream oriented output

3.

RandomAccessFile ra = new RandomAccessFile (filename, "rw"); / / the following parameter specifies how to open the file stream. "rw" refers to read-write, "r" is read-only, and Java does not provide write-only ra.seek (number); / / move the file pointer to number, where the file pointer can be understood as the location where the file starts to read ra.skipByte (number); / / skip number bytes ra.read (); ra.close () / / RandomAccessFile can be read or written, and the location can be specified using the seek () function

Here are some introductions to Baidu encyclopedia:

RandomAccessFile does not belong to the InputStream and OutputStream families. In fact, apart from implementing the DataInput and DataOutput interfaces (DataInputStream and DataOutputStream also implement these two interfaces), it has nothing to do with these two classes and doesn't even use the functionality already prepared by InputStream and OutputStream; it's a completely separate class, and all methods (most of which belong to itself) are written from scratch. This may be because RandomAccessFile can move back and forth in a file, so its behavior is fundamentally different from that of other Imax O classes. All in all, it is a separate class that directly inherits from Object.

Advanced chapters:

In nio, Java reimplements the I _ map O stream and introduces some new methods to improve the speed. I mainly introduce channel and memory mapping files.

1. Channel:

Channels and buffers are a paired concept, and an example in Thinking in Java is easy to understand: we treat the file we want to read as a coal mine, and the data is the coal we want. The channel is like the conveyor belt of the coal mine. We have no way to take the coal directly from the conveyor belt, so we have to use the truck to load the coal. The truck is the buffer. It is mainly responsible for taking the data from the channel and passing it to the program we wrote. * * the buffer that can interact with the channel is ByteBuffer. You can see that the way to parse streams supported by and channels is byte flow. So it comes with FileInputStream/FileOutputStream,RandomAccessFile.

Example:

a.

FileChannel fc = new FileOutputStream (filename). GetChannel (); fc.write (ByteBuffer.wrap ("something test" .getBytes (); / / it's easy to use ByteBuffer here. In fact, ByteBuffer can use a put () function to write byte array fc.close ().

b.

Fc= new FileOutputStream (filename). GetChannel (); ByteBuffer buff = ByteBuffer.allocate (size); / / Yes, ByteBuffer does not provide a display constructor, and if you want to create a new object, you must use the allocate () function to allocate space. Fc.read (buff); fc.close ()

Why do you want to use a channel to do Igamo? The main concern is performance. The channel plus buffer allows the program to read and write a certain amount of characters, while only using InputStream/OutputStream,Reader/Writer can read and write only one byte / character at a time. While the program needs to be handed over to the operating system to solve this part of the function (calling the system call), reducing the number of times it is handed over to the operating system can effectively reduce the time spent by IWeiO.

two。 Memory mapping file:

The main meaning of a memory-mapped file is to assume that the files are all in memory and accessed as a very large array, which is particularly efficient. Why is it better? This starts with the Java virtual machine and operating system. The main implementation method of Java iUnip O must be the use of system calls, which first transfer the files you want to use from the hard disk to the kernel's iUnip O buffer, this time it will import more files than the Java program wants (copy more content because of the program's locality principle, can get better efficiency), and then import from the kernel iUnip O buffer into the Java process's own private memory space. On the other hand, the memory-mapped file abandons the method of two copies and directly maps the virtual space of the Java process to the file object. A page fault exception occurs when the desired content is not typed in the private memory space, and then the lower-level system call is used to solve this problem (in fact, page fault exception handling is also involved in the system call of Igamo). The advantage is that it reduces the overhead of going from the kernel I _ map O buffer to the process's private address at once.

Example:

FileChannel fc = new RandomAccessFile (filename, "rw"). GetChannel (); MappedByteBuffer mb = fc.map (FileChannel.MapMode.READ_WRITE,start,length); mb.put ((byte)'x'); mb.get (); fc.close (); "what are the methods of Java file iMaple O". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report