import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class TestReadFile
{undefined
public static void main(String[] args)
{undefined
String FilePath = “D:/test.txt”;
FileReader fr = null;
BufferedReader br = null;
StringBuffer sBuffer = new StringBuffer();
try
{undefined
try
{undefined
fr = new FileReader(FilePath);// 建立FileReader对象,并实例化为fr
}
catch (FileNotFoundException e)
{undefined
e.printStackTrace();
}
br = new BufferedReader(fr);// 建立BufferedReader对象,并实例化为br
String Line = br.readLine();// 从文件读取一行字符串
// 判断读取到的字符串是否不为空
while (Line != null)
{undefined
sBuffer.append(Line);
sBuffer.append(“\n”);
Line = br.readLine();// 从文件中继续读取一行数据
}
}
catch (IOException e)
{undefined
e.printStackTrace();
}
finally
{undefined
try
{undefined
if (br != null)
br.close();// 关闭BufferedReader对象
if (fr != null)
fr.close();// 关闭文件
}
catch (IOException e)
{undefined
e.printStackTrace();
}
}
// 输出读取的结果
System.out.println(sBuffer);
}
}
————————————————
版权声明:本文为CSDN博主「任念辰」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_42136826/article/details/114032259