文件操作

169 阅读1分钟
好记性不如烂笔头,此文为了记录下文件的基本操作,需要的时候,方便查阅,下面进行实例:
  • 按字节读取文件
    /**
     * 按字节读取文件,一般用于读取图片、声音、影像等二进制文件
     *
     * @param fileName 文件全名
     * @param bytes    单次读取字节长度
     * @throws IOException
     */
    public static void readFileByBytes(String fileName, byte[] bytes) throws IOException {
        File file = new File(fileName);
        FileInputStream inputStream = null;
        if (file.isFile() && file.exists()) {
            try {
                inputStream = new FileInputStream(file);
                int result;
                while ((result = inputStream.read(bytes)) != -1) {
                    System.out.write(bytes, 0, result);
                }
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
            }
        }
    }
  • 按字符读取文件
    /**
     * 按字符读取文件,一般用于读取文本文件,建议单字符读取
     *
     * @param fileName 文件全名
     * @param chars    单次读取字符长度
     * @throws IOException
     */
    public static void readFileByChar(String fileName, char[] chars) throws IOException {
        File file = new File(fileName);
        Reader reader = null;
        if (file.isFile() && file.exists()) {
            try {
                reader = new InputStreamReader(new FileInputStream(file));
                int result;
                while ((result = reader.read(chars)) != -1) {
                    System.out.print(chars);
                }
            } finally {
                if (reader != null) {
                    reader.close();
                }
            }
        }
    }
  • 按行读取文件
    /**
     * 按行读取文件
     *
     * @param fileName 文件全名
     * @throws IOException
     */
    public static void readFileByLine(String fileName) throws IOException {
        File file = new File(fileName);
        BufferedReader reader = null;
        if (file.isFile() && file.exists()) {
            try {
                reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
                String result;
                while (StringUtils.hasText((result = reader.readLine()))) {
                    System.out.println(result);
                }
            } finally {
                if (reader != null) {
                    reader.close();
                }
            }
        }
    }

更多文章: CSDN博客 简书博客 公众号:代码小搬运

代码小搬运.jpg