欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java读取txt文件

程序员文章站 2022-07-12 13:30:03
...

使用java读取txt文件import java.io.BufferedReader;

import java.io.File;
import java.io.FileReader;

public class ReadFileFromTxt {

	public void readFile(String filePath) {
		try {
			BufferedReader in = new BufferedReader(new FileReader(filePath));
			String line = null;
			while ((line = in.readLine()) != null) {
				System.out.println(line);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void readFile2(String filePath) {
        BufferedReader br;
        String read = "";
        try {
            File file = new File(filePath);
            FileReader fileread = new FileReader(file);
            br = new BufferedReader(fileread);
            while ((read = br.readLine()) != null) {
            	System.out.print(read + "\t\n");
            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }

	public static void main(String[] args) {
		String filePath = "E:\\link.txt";
//		new ReadFileFromTxt().readFile(filePath);
		new ReadFileFromTxt().readFile2(filePath);
	}
}