本例以Windows, JDK1.7 为基础,这里使用BufferedReader这个类来,对文件一行一行的读取。
FileReadBufferedReader.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| package com.devnp.io;
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader;
public class FileReadBufferedReader {
public static void main(String[] args) {
File file = new File("D:\\Person-Test\\1.txt"); InputStreamReader isr = null; BufferedReader bis = null;
StringBuilder stringBuilder = new StringBuilder(); try { isr = new InputStreamReader(new FileInputStream(file));
bis = new BufferedReader(isr); String str ; while ((str = bis.readLine()) != null) { stringBuilder.append(str); }
} catch (IOException e) { e.printStackTrace(); } finally { try { isr.close(); bis.close(); } catch (IOException ex) { ex.printStackTrace(); } } System.out.println(stringBuilder); }
}
|
Author:
Darren Du
License:
Copyright (c) 2019 MIT LICENSE