/*作业一
* 读取[银行存款信息.txt]文件,解析该文件,封装到一个java对象,
* 把这些java对象依次放到List中;
*/
public class ReadBankIt {
public static void main(String[] args) {
FileReader fr = null;
BufferedReader bufr = null;
try {
// 创建一个文件读取流对象,和指定文件名的文件相关联
fr = new FileReader("C:\\Users\\yizhi\\Desktop\\银行存款信息.txt");
bufr = new BufferedReader(fr);
// 将读取数据依次放到List中
List<String> list = new java.util.ArrayList<String>();
String line = null;
while ((line = bufr.readLine()) != null) {
list.add(line);
}
// 使用foreach遍历List
for (String str : list) {
System.out.println(str);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 关闭资源
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (bufr != null) {
try {
bufr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
作业一:解析文件,封装到一个java对象,依次放到List中
最新推荐文章于 2021-02-27 02:05:07 发布