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

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

天津卓眾教育      2022-04-10 12:49:01     20

java文件讀取到byte數(shù)組的三種方法(總結(jié)),  下面天津卓眾教育java學(xué)院小編就為大家?guī)硪黄猨ava文件讀取到byte數(shù)組的三種方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,希

課程價(jià)格 請(qǐng)咨詢

上課時(shí)段: 授課校區(qū):

詳細(xì)介紹

  


下面天津卓眾教育java學(xué)院小編就為大家?guī)硪黄猨ava文件讀取到byte數(shù)組的三種方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,希望能夠通過此文幫助到大家。那一起跟隨小編過來看看吧。


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


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;

        }

        // 確保所有數(shù)據(jù)均被讀取

        if (offset != buffer.length) {

        throw new IOException("Could not completely read file "

                    + file.getName());

        }

        fi.close();

        return buffer;

    }

 

   

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

        }

    }

 

   

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

            }

        }

    }

 

   

    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學(xué)院小編介紹的“java文件讀取到byte數(shù)組的三種方法”的內(nèi)容,希望能夠幫助到大家,如有任何疑問,請(qǐng)?jiān)诰€咨詢,有專業(yè)老師隨時(shí)為你服務(wù)。



培訓(xùn)啦提醒您:交易時(shí)請(qǐng)核實(shí)對(duì)方資質(zhì),對(duì)于過大宣傳或承諾需謹(jǐn)慎!任何要求預(yù)付定金、匯款等方式均存在風(fēng)險(xiǎn),謹(jǐn)防上當(dāng)。