「Java开发工具」Java 读取文件或二进制文件内容

164 阅读2分钟

废话不多说系列,开整~

美女2.png

读取文件内容工具类

  1. 读取非二进制文件;
  2. 读取二进制文件,然后以二进制字符返回出来;
一、源码
import java.io.*;

public class FileUtil {

    /**
     * 读取文件内容,作为字符串串返回(常见纯文本类型的文件即可适用次方法)
     *
     * @param filePath 文件所在地址
     * @return 文件内容
     * @throws IOException 输出异常
     */
    public static String readFileAsString(String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.exists()) {
            throw new FileNotFoundException(filePath);
        }

        if (file.length() > 1024 * 1024 * 1024) {
            throw new IOException("File is too large");
        }

        StringBuilder sb = new StringBuilder((int) (file.length()));
        // 创建字节输入流
        FileInputStream fis = new FileInputStream(filePath);
        // 创建一个长度为10240的Buffer
        byte[] bbuf = new byte[10240];
        // 用于保存实际读取的字节数
        int hasRead = 0;
        while ((hasRead = fis.read(bbuf)) > 0) {
            sb.append(new String(bbuf, 0, hasRead));
        }
        fis.close();
        return sb.toString();
    }

    /**
     * 根据文件路径读取byte[] 数组(一般用于读取图片,视频等二进制文件才用到的方法,注意:doc文件中含有图片就可以适用此方法)
     *
     * @param filePath 文件所在路径
     * @return 返回内容以字节数组形式输出
     * @throws IOException
     */
    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));
                short bufSize = 1024;
                byte[] buffer = new byte[bufSize];
                int len1;
                while (-1 != (len1 = in.read(buffer, 0, bufSize))) {
                    bos.write(buffer, 0, len1);
                }

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

                bos.close();
            }
        }
    }

    public static void main(String[] args) {
        testReadFileByBytesOut();
    }


    public static void testReadFileAsStringOut() {
        String filePath = "D:\\config\\fastdfsclient.properties";
        try {
            String content = readFileAsString(filePath);
            System.out.println(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void testReadFileByBytesOut() {
        String filePath = "D:\\config\\fastdfsclient.properties";
        try {
            byte[] content = readFileByBytes(filePath);
            for (int i = 0; i < content.length; i++) {
                System.out.println(content[i]);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

二、结果展示:

① 以字符串的方式读取文件内容(读取纯文本内容)

image.png

② 以字节码的方式读取文件内容(一般用于读取二进制文件)

image.png


至此,谢谢阅读🙏~

美女2.png