代码:
public static void readTxt(String fileName) {
FileReader fr= null;
BufferedReader br= null;
try {
String file = TableUtils.class.getResource("/"+fileName).getPath();
fr = new FileReader(file);
br = new BufferedReader(fr);
String line="";
String[] arrs=null;
while ((line=br.readLine())!=null) {
System.out.println(line);
}
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
if (br!=null){
try {
br.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (fr!=null){
try {
fr.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
测试:
-
在resources目录下新建
test.txt
文件 -
主函数
public static void main(String[] args) throws IOException {
readTxt("test.txt");
}
- 结果
111
aaa
bbb
ccc
进程已结束,退出代码0