黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 學習攻略 java文件讀取到byte數組的三種方法(總結)

java文件讀取到byte數組的三種方法(總結)

更新時間:2019-08-28 14:00:49 來源:動力節點 瀏覽3809次

  


下面動力節點java學院小編就為大家帶來一篇java文件讀取到byte數組的三種方法。小編覺得挺不錯的,現在就分享給大家,希望能夠通過此文幫助到大家。那一起跟隨小編過來看看吧。


java文件讀取到byte數組的三種方法


package zs;

 

import java.io.BufferedInputStream;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.RandomAccessFile;

import java.nio.ByteBuffer;

import java.nio.MappedByteBuffer;

import java.nio.channels.FileChannel;

import java.nio.channels.FileChannel.MapMode;

 

public class FileUtils {

    public byte[] getContent(String filePath) throws IOException {

        File file = new File(filePath);

        long fileSize = file.length();

        if (fileSize > Integer.MAX_VALUE) {

            System.out.println("file too big...");

            return null;

        }

        FileInputStream fi = new FileInputStream(file);

        byte[] buffer = new byte[(int) fileSize];

        int offset = 0;

        int numRead = 0;

        while (offset < buffer.length

        && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {

            offset += numRead;

        }

        // 確保所有數據均被讀取

        if (offset != buffer.length) {

        throw new IOException("Could not completely read file "

                    + file.getName());

        }

        fi.close();

        return buffer;

    }

 

    /**

     * the traditional io way

     * 

     * @param filename

     * @return

     * @throws IOException

     */

    public static byte[] toByteArray(String filename) throws IOException {

 

        File f = new File(filename);

        if (!f.exists()) {

            throw new FileNotFoundException(filename);

        }

 

        ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length());

        BufferedInputStream in = null;

        try {

            in = new BufferedInputStream(new FileInputStream(f));

            int buf_size = 1024;

            byte[] buffer = new byte[buf_size];

            int len = 0;

            while (-1 != (len = in.read(buffer, 0, buf_size))) {

                bos.write(buffer, 0, len);

            }

            return bos.toByteArray();

        } catch (IOException e) {

            e.printStackTrace();

            throw e;

        } finally {

            try {

                in.close();

            } catch (IOException e) {

                e.printStackTrace();

            }

            bos.close();

        }

    }

 

    /**

     * NIO way

     * 

     * @param filename

     * @return

     * @throws IOException

     */

    public static byte[] toByteArray2(String filename) throws IOException {

 

        File f = new File(filename);

        if (!f.exists()) {

            throw new FileNotFoundException(filename);

        }

 

        FileChannel channel = null;

        FileInputStream fs = null;

        try {

            fs = new FileInputStream(f);

            channel = fs.getChannel();

            ByteBuffer byteBuffer = ByteBuffer.allocate((int) channel.size());

            while ((channel.read(byteBuffer)) > 0) {

                // do nothing

                // System.out.println("reading");

            }

            return byteBuffer.array();

        } catch (IOException e) {

            e.printStackTrace();

            throw e;

        } finally {

            try {

                channel.close();

            } catch (IOException e) {

                e.printStackTrace();

            }

            try {

                fs.close();

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

    }

 

    /**

     * Mapped File way MappedByteBuffer 可以在處理大文件時,提升性能

     * 

     * @param filename

     * @return

     * @throws IOException

     */

    public static byte[] toByteArray3(String filename) throws IOException {

 

        FileChannel fc = null;

        try {

            fc = new RandomAccessFile(filename, "r").getChannel();

            MappedByteBuffer byteBuffer = fc.map(MapMode.READ_ONLY, 0,

                    fc.size()).load();

            System.out.println(byteBuffer.isLoaded());

            byte[] result = new byte[(int) fc.size()];

            if (byteBuffer.remaining() > 0) {

                // System.out.println("remain");

                byteBuffer.get(result, 0, byteBuffer.remaining());

            }

            return result;

        } catch (IOException e) {

            e.printStackTrace();

            throw e;

        } finally {

            try {

                fc.close();

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

    }

}


以上就是動力節點java學院小編介紹的“java文件讀取到byte數組的三種方法”的內容,希望能夠幫助到大家,如有任何疑問,請在線咨詢,有專業老師隨時為你服務。



提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 日本福利片午夜免费观着 | 丁香综合在线 | 国产玖玖 | 日韩三级黄色 | 日本三级一区二区三区 | 天天爽夜夜爽人人爽免费 | 亚洲区精品 | 狠狠色婷婷狠狠狠亚洲综合 | 亚洲国产精品91 | 黄网站视频在线观看 | 色视频在线观看视频 | 一级黄色片在线播放 | 欧美精品在线一区二区三区 | 欧美一级精品高清在线观看 | 日本在线看小视频网址 | 成人在线一区二区三区 | 久久99国产亚洲精品观看 | 成人www视频网站免费观看 | 国产一级做a爱免费观看 | 国产成人精品在视频 | 亚洲欧美精品伊人久久 | 亚洲第一天堂无码专区 | 成人网18免费网站 | 婷婷亚洲综合五月天小说 | 亚洲麻豆国产精品 | 中文精品久久久久国产网站 | 精品在线视频一区 | 欧美一区二区三区不卡 | 影音先锋最新资源网 | 老子影院午夜伦不卡亚洲 | 国产在线视频欧美亚综合 | 欧美日韩无 | 欧美午夜在线 | 国产成人啪精品 | 69午夜视频| 蜜桃视频成人免费版在线 | 717影院理论午夜伦不卡久久 | 国产成人综合久久精品亚洲 | 国产亚洲综合色就色 | 伦理剧在线播放 | 7m视频在线观看高清国产7m |