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

How java writes data from memory array streams to file streams

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the relevant knowledge of "how java writes data from memory array streams into file streams". 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!

In the java byte stream introduction (file stream), we introduced two ways of writing files: FileOutputStream (FOS) and RandomAccessFile (RAF). So, when we use ByteArrayOutputStream (BAOS) to maintain data in memory, how to use FOS and RAF to write files? this article introduces four methods.

Preparatory work:

Private static final Path path = Paths.get ("src", "main", "resources", "test.myfile")

Private static final File file = path.toFile ()

Private static int size = 1024,1024,800

Private static byte [] b1 = new byte [size]

Private static ByteArrayOutputStream out = new ByteArrayOutputStream ()

And write b1 to out

Out.write (b1)

WriteTo writes to FOS

First, BAOS has a method called writeTo (), which writes data from BAOS directly to another byte output stream. More accurately, the data in BAOS is written out using the write () method of another byte output stream. Here BAOS is equivalent to a byte array.

/ * *

* Writes the complete contents of this byte array output stream to

* the specified output stream argument, as if by calling the output

* stream's write method using out.write (buf, 0, count).

*

* @ param out the output stream to which to write the data.

* @ exception IOException if an Imax O error occurs.

, /

Public synchronized void writeTo (OutputStream out) throws IOException {

Out.write (buf, 0, count)

}

Because FOS itself is an OutputStream, the data in BAOS can be written directly to FOS through writeTo ().

/ / write data from BAOS to FileOutputStream

Private static void writeToFOS () throws IOException {

If (file.exists ())

File.delete ()

/ / write the data cached by ByteArrayOutputStream to FileOutputStream, that is, to the file

FileOutputStream fileOutputStream = new FileOutputStream (file, false)

Long time = System.currentTimeMillis ()

Out.writeTo (fileOutputStream)

FileOutputStream.close ()

Time = System.currentTimeMillis ()-time

System.out.println (it takes time to write "+ size +" bytes to FOS: "+ time +" ms ")

File.delete ()

}

WriteTo writes to RAF

Because RandomAccessFile is not a standard OutputStream, it cannot be implemented directly with the writeTo () method. So how do you write the data in BAOS to RandomAccessFile?

The solution is to wrap the RandomAccessFile as an OutputStream. We implement a custom OutputStream, inherit OutputStream, and overwrite the original writing method of OutputStream with three writing methods of RAF.

Class MyRandomAccessFileOutputStream extends OutputStream {

Private RandomAccessFile raf

Public MyRandomAccessFileOutputStream (RandomAccessFile raf) {

This.raf = raf

}

@ Override

Public void write (int b) throws IOException {

Raf.write (b)

}

@ Override

Public void write (byte b []) throws IOException {

Raf.write (b)

}

@ Override

Public void write (byte b [], int off, int len) throws IOException {

Raf.write (b, off, len)

}

Public void seek (long pos) throws IOException {

Raf.seek (pos)

}

Public long length () throws IOException {

Return raf.length ()

}

@ Override

Public void close () throws IOException {

Raf.close ()

}

}

Next, you can happily use RandomAccessFile as an OutputStream.

/ / write data from BAOS to MyRandomAccessFileOutputStream

Private static void writeToMyRaf () throws IOException {

If (file.exists ())

File.delete ()

RandomAccessFile raf = new RandomAccessFile (file, "rw")

MyRandomAccessFileOutputStream myraf = new MyRandomAccessFileOutputStream (raf)

Long time = System.currentTimeMillis ()

Out.writeTo (myraf)

Myraf.close ()

Time = System.currentTimeMillis ()-time

System.out.println (it takes time to write "+ size +" bytes to MyRaf: "+ time +" ms ")

File.delete ()

}

Copy writes to FOS

Do you remember that BAOS also has a toByteArray () method that returns the contents to an byte array. Its implementation calls the Arrays.copyOf () method, which is recorded here as copy.

In retrospect, there is actually a write (byte []) method for all kinds of streams, so you know what I want to do? well, it's a rough way to implement it. Code it.

/ / write the data copy in BAOS to FileOutputStream

Private static void copyToFOS () throws IOException {

If (file.exists ())

File.delete ()

/ / write the data cached by ByteArrayOutputStream to FileOutputStream, that is, to the file

FileOutputStream fileOutputStream = new FileOutputStream (file, false)

Long time = System.currentTimeMillis ()

FileOutputStream.write (out.toByteArray ())

FileOutputStream.close ()

Time = System.currentTimeMillis ()-time

System.out.println ("write" + size + "byte copy to FOS:" + time + "ms")

File.delete ()

}

Copy writes to RAF

Similarly, RandomAccessFile also has a write (byte []) method, so. Continue with the code:

/ / write the data copy in BAOS to RandomAccessFile

Private static void copyToRaf () throws IOException {

If (file.exists ())

File.delete ()

RandomAccessFile raf = new RandomAccessFile (file, "rw")

Long time = System.currentTimeMillis ()

Raf.write (out.toByteArray ())

Raf.close ()

Time = System.currentTimeMillis ()-time

System.out.println ("write" + size + "byte copy to Raf:" + time + "ms")

File.delete ()

}

Next, let's compare the speed of these four ways:

Experimental comparison

Write 800m data. Packaging RAF as OutputStream is about as efficient as FileOutputStream. For both file stream writing methods, writeTo is always faster than copy. After all, copy has an extra copy and takes up extra memory.

So no matter what kind of file stream, using BAOS's writeTo () is the best.

It takes time to write 838860800 bytes to FOS: 1413ms

Writing 838860800 bytes of copy to FOS takes time: 2092ms

It takes time to write 838860800 bytes to MyRaf: 1452ms

Writing 838860800 bytes of copy to Raf takes time: 2203ms "how java writes data from an array of memory streams to a file stream" ends here. 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

Internet Technology

Wechat

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

12
Report