java如何將內(nèi)存數(shù)組流的數(shù)據(jù)寫入文件流中

本篇內(nèi)容介紹了“java如何將內(nèi)存數(shù)組流的數(shù)據(jù)寫入文件流中”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

成都創(chuàng)新互聯(lián)專注于企業(yè)營銷型網(wǎng)站建設(shè)、網(wǎng)站重做改版、愛輝網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5建站、商城網(wǎng)站制作、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)公司、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為愛輝等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

在 java 字節(jié)流入門(文件流)中,我們介紹了 FileOutputStream(FOS) 和 RandomAccessFile(RAF) 兩種寫文件的方式。那么,當(dāng)我們?cè)趦?nèi)存中使用 ByteArrayOutputStream(BAOS) 維護(hù)數(shù)據(jù)時(shí),如何利用 FOS 和 RAF 寫文件呢,本文介紹四種方法。

準(zhǔn)備工作:

   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();

并將 b1 寫入 out 中

out.write(b1);

writeTo寫入FOS

首先,BAOS 有一個(gè)方法叫 writeTo(),這個(gè)方法可以將 BAOS 中的數(shù)據(jù)直接寫入另一個(gè)字節(jié)輸出流中。更準(zhǔn)確的說法是,使用另一個(gè)字節(jié)輸出流的 write() 方法將 BAOS 中的數(shù)據(jù)寫出去。這里 BAOS 就和一個(gè)字節(jié)數(shù)組是等價(jià)的。

   /**
    * 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 <code>out.write(buf, 0, count)</code>.
    *
    * @param      out   the output stream to which to write the data.
    * @exception  IOException  if an I/O error occurs.
    */
   public synchronized void writeTo(OutputStream out) throws IOException {
       out.write(buf, 0, count);
   }

因?yàn)?FOS 本身就是 OutputStream,所以可以直接將 BAOS 中的數(shù)據(jù)通過 writeTo() 寫入 FOS 中。

// 將 BAOS 中的數(shù)據(jù)寫入 FileOutputStream
   private static void writeToFOS() throws IOException {
       if(file.exists())
           file.delete();
       // 將 ByteArrayOutputStream 緩存的數(shù)據(jù)寫入 FileOutputStream 中,即寫入文件中
       FileOutputStream fileOutputStream = new FileOutputStream(file, false);

       long time = System.currentTimeMillis();
       out.writeTo(fileOutputStream);
       fileOutputStream.close();
       time = System.currentTimeMillis() - time;
       System.out.println("將 "+ size + " 個(gè)字節(jié)寫入 FOS 耗時(shí):" + time + "ms");
       file.delete();
   }

writeTo寫入RAF

由于 RandomAccessFile 不是標(biāo)準(zhǔn)的 OutputStream,所以沒法直接用 writeTo() 方法實(shí)現(xiàn)。那如何將 BAOS 中的數(shù)據(jù)寫入 RandomAccessFile 呢?

解決方案是:把 RandomAccessFile 包裝成一個(gè) OutputStream。我們實(shí)現(xiàn)一個(gè) 自定義的 OutputStream,繼承 OutputStream,并用 RAF 的三種寫方法覆蓋 OutputStream 的原有寫方法。

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();
   }

}

接下來,就可以開心的把 RandomAccessFile 當(dāng) OutputStream 用了。

// 將 BAOS 中的數(shù)據(jù)寫入 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("將 "+ size + " 個(gè)字節(jié)寫入 MyRaf 耗時(shí):" + time + "ms");
       file.delete();
   }

Copy寫入FOS

大家記不記得 BAOS 還有個(gè) toByteArray() 方法可以將其中的內(nèi)容返回一個(gè) byte 數(shù)組。其實(shí)現(xiàn)調(diào)用了 Arrays.copyOf() 方法,這里記做 copy 。

然后回想,其實(shí)各種流都有一個(gè) write(byte[]) 方法,所以你們知道我想干嘛了嗎,嗯,很粗暴的實(shí)現(xiàn)方式,上代碼。

// 將 BAOS 中的數(shù)據(jù) copy 寫入 FileOutputStream
   private static void copyToFOS() throws IOException {
       if(file.exists())
           file.delete();
       // 將 ByteArrayOutputStream 緩存的數(shù)據(jù)寫入 FileOutputStream 中,即寫入文件中
       FileOutputStream fileOutputStream = new FileOutputStream(file, false);

       long time = System.currentTimeMillis();
       fileOutputStream.write(out.toByteArray());
       fileOutputStream.close();
       time = System.currentTimeMillis() - time;
       System.out.println("將 "+ size + " 個(gè)字節(jié) copy 寫入 FOS 耗時(shí):" + time + "ms");
       file.delete();
   }

Copy寫入RAF

同樣的,RandomAccessFile 也有 write(byte[]) 方法,所以。。。繼續(xù)上代碼:

 

// 將 BAOS 中的數(shù)據(jù) copy 寫入 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("將 "+ size + " 個(gè)字節(jié) copy 寫入 Raf 耗時(shí):" + time + "ms");
       file.delete();
   }

接下來我們比較一下這四種方式的速度:

實(shí)驗(yàn)對(duì)比

寫 800M數(shù)據(jù)。將 RAF 包裝成 OutputStream 和 FileOutputStream 的效率差不多。對(duì)于兩種文件流的寫入方法,writeTo 總是比 copy 寫入要快。畢竟 copy 多了一步拷貝,而且會(huì)占用額外內(nèi)存。

所以不管哪種文件流,用 BAOS 的 writeTo() 都是最好的。

將 838860800 個(gè)字節(jié)寫入 FOS 耗時(shí):1413ms
將 838860800 個(gè)字節(jié) copy 寫入 FOS 耗時(shí):2092ms
將 838860800 個(gè)字節(jié)寫入 MyRaf 耗時(shí):1452ms
將 838860800 個(gè)字節(jié) copy 寫入 Raf 耗時(shí):2203ms

“java如何將內(nèi)存數(shù)組流的數(shù)據(jù)寫入文件流中”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

當(dāng)前名稱:java如何將內(nèi)存數(shù)組流的數(shù)據(jù)寫入文件流中
當(dāng)前地址:http://bm7419.com/article10/igecdo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、Google、做網(wǎng)站網(wǎng)站導(dǎo)航、ChatGPT網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)