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

專注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數組的三種方法”的內容,希望能夠幫助到大家,如有任何疑問,請在線咨詢,有專業老師隨時為你服務。



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

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 午夜影片 | 欧美中文一区 | 亚洲国产va | 处videossex第一次hd | 好吊色视频在线观看 | 制服丝袜 自拍偷拍 | 天天槽夜夜槽槽不停 | 久久精品亚洲综合一品 | 日韩欧美在线综合网高清 | sea8国产精品视频 | 婷婷亚洲久悠悠色悠在线播放 | 日韩欧美精品中文字幕 | 日韩一级影院 | 日本精品高清一区二区不卡 | 免费看羞羞视频网站 | 欧美午夜网站 | 国产精品一区久久精品 | 天天爽天天操 | 黄色一级视频免费观看 | 天天做天天添婷婷我也去 | 无遮挡h黄漫动漫在线观看 无遮挡h纯内动漫在线观看 | 顶级欧美做受xxx000 | 最近免费中文字幕大全高清片 | 狠狠夜色午夜久久综合热91 | 狠狠操天天爱 | 亚洲三级理论 | 天天爱天天舔 | 日本高清视频色wwwwww色 | 久久精品国产无限资源 | 欧美日韩一区二区视频免费看 | 一区在线免费 | 亚洲一区二区福利视频 | 日本妇人成熟免费观看18 | 国产激情久久久久影 | 一区二区三区在线 | 欧 | 亚洲无砖砖区免费 | 日韩精品区 | 美女黄网站免费 | 国产一区二区三区在线观看精品 | 日韩精品福利片午夜免费 | 午夜亚洲国产成人不卡在线 |