大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

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

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

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

  


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



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

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 特级aa一级欧美毛片 | 午夜看毛片 | 两性影院| 一区二区三区四区免费视频 | 亚洲天堂777 | 四虎影剧院 | 99热最新网址获取 | 91精品国产闺蜜国产在线 | 中文字幕人成乱码第一页 | 国产精品中文 | 99在线热视频 | 久久久久久综合一区中文字幕 | 欧美在线一区二区三区 | 精品久久网站 | 污宅男666在线永久免费观看 | 福利视频网页 | 亚洲国产精品不卡毛片a在线 | 人人揉人人爽五月天视频 | 亚洲美女亚洲精品久久久久 | 狠狠色丁香久久婷婷 | 伊人一区二区三区 | 精品国产91在线网 | a毛片在线播放 | 伊人a.v在线 | 99久久综合狠狠综合久久aⅴ | 在线观看年轻的母亲 | 久热精品香蕉在线视频 | 天天色天天拍 | 91综合久久婷婷久久 | 99热这里只有精品国产免费 | 99视频国产在线 | 尹人香蕉99久久综合网站 | 四虎在线永久免费观看 | 亚洲热在线视频 | 久久桃花| 亚洲精品视频免费在线观看 | 高清国产一区二区 | 9999久久| 久久网免费 | 大陆一级毛片免费视频观看i | 欧美成人四级剧情在线播放 |