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); } }
上一篇: java基础之基本数据类型对象包装类,StringBuffer
下一篇: 多线程读取文件