JAVA工具类(7)-读取二进制文件

import org.apache.commons.codec.binary.StringUtils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @Author zufeichao
 * @ProjectName test-pdf
 * @Description TODO
 * @Date 2019-08-13 11:12
 * @T: BinaryFile
 **/
public class BinaryFile {

    public static byte[] read(File file) throws IOException {
        BufferedInputStream bf = new BufferedInputStream(new FileInputStream(file));
        try {
            byte[] data = new byte[bf.available()];
            bf.read(data);
            return data;
        }finally {
            bf.close();
        }
    }


    public static byte[] read(String bFile) throws IOException{
        return read(new File(bFile).getAbsoluteFile());
    }

    public static void main(String[] args) {
        try {
            byte[] bt = read("test.txt");
            System.out.println(bt);
            String s = StringUtils.newStringUtf8(bt);
            System.out.println(s);
        }catch (IOException e){
            e.printStackTrace();
        }

    }
}

你可能感兴趣的:(Java)