- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- public class T {
- public static void main(String[] args) {
- String fileName = "data.txt";
- BufferedReader fromFile = null;
- try {
- FileReader fr = new FileReader(fileName);
- fromFile = new BufferedReader(fr);
- System.out.println("The file " + fileName+ " contains the following lines:");
- String line = fromFile.readLine();
- while (line != null) {
- System.out.println(line);
- line = fromFile.readLine(); // throw IOException
- }
- } catch (FileNotFoundException e) {
- System.out.println("Error opening the file " + fileName);
- System.exit(0);
- } catch (IOException e) {
- System.out.println("Error reading the file " + fileName);
- }
- finally {
- try {
- if (fromFile != null)
- fromFile.close(); // throw IOException
- } catch (IOException e) {
- System.out.println("Error closing the file " + fileName);
- System.exit(0);
- }
- }
- }
- }
BufferedReader读取文本文件
最新推荐文章于 2025-05-28 23:47:11 发布