java 將一個本地的文件讀取到内存BYTE數組裏面

public static byte[] readFileByBytes(String filePath) throws IOException {
    File file = new File(filePath);
    if (!file.exists()) {
        throw new FileNotFoundException(filePath);
    } else {
        ByteArrayOutputStream bos = new ByteArrayOutputStream((int)file.length());
        BufferedInputStream in = null;

        try {
            in = new BufferedInputStream(new FileInputStream(file));
            int bufSize = 1024;
            byte[] buffer = new byte[bufSize];
            boolean var6 = false;

            int len;
            while(-1 != (len = in.read(buffer, 0, bufSize))) {
                bos.write(buffer, 0, len);
            }

            byte[] var7 = bos.toByteArray();
            return var7;
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException var14) {
                var14.printStackTrace();
            }

            bos.close();
        }
    }

猜你喜欢

转载自blog.csdn.net/yz18931904/article/details/127594221