Java 读文件的几种常用方式

166 阅读1分钟

最近同事run了,要接手他的Java项目,哎,心累,不得已临时学个Java。就把最近所学做个简单整理,算是以后的工具吧。

Java的文件读,很丰富,分字节流、字符流、带缓冲的读等,以下总结几种会用到的读文件的方式,各位看官轻喷。

代码展示:


package learnjava.fileIO;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

public class ReadFile {
    public static void main(String[] args) {
        String path = new String("D:\software\intelli D\myDemo\src\learnjava\fileIO\file1.txt");
        fileInputStream(path); // 输入流读
        fileBufferedStream(path); // 带缓冲的输入流读
        fileReader(path); // 字符读
        fileBufferedReader(path); // 带缓冲的字符读
        fileFiles(path); // 使用Files读
    }

    // read byte one by one, suit for binary file read, like picture/video/music and so on.
    public static void fileInputStream(String filePath) {
        System.out.println("Read file by FileInputStream class...");
        try { // may use try (resource) to avoid to code xx.close()
            FileInputStream fis = new FileInputStream(filePath);

            int data;
            while ((data = fis.read()) != -1) {
                System.out.println((char) data);
            }

            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // read with, use FileInputStream
    public static void fileBufferedStream(String filePath) {
        System.out.println("Read file by BufferedStream class...");

        try {
            FileInputStream fis = new FileInputStream(filePath);
            BufferedInputStream bis = new BufferedInputStream(fis);

            int data;
            while ((data = bis.read()) != -1) {
                System.out.println((char) data);
            }
            fis.close();
            bis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // read char one by one, suit for text file.
    public static void fileReader(String filePath) {
        System.out.println("Read file by FileReader class...");

        try {
            FileReader fr = new FileReader(filePath);

            int data;
            while ((data = fr.read()) != -1) {
                System.out.println((char) data);
            }
            fr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // read with buffer, use FileReader as reader
    public static void fileBufferedReader(String filePath) {
        System.out.println("Read file by BufferedReader class...");

        try {
            FileReader fr = new FileReader(filePath);
            BufferedReader br = new BufferedReader(fr);

            int data;
            while ((data = br.read()) != -1) { // may also use readLine(), return String
                System.out.println((char) data);
            }
            fr.close();
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void fileFiles(String filePath) {
        System.out.println("Read file by Files class...");

        try {
            List<String> lines;
            lines = Files.readAllLines(Paths.get(filePath));

            System.out.println(lines.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}