我正在做一个关于阅读文件的样本。我将一个 txt 文件放入项目文件夹并编写了此代码,但出现异常FileNotFound
,并且当我尝试关闭dataInputStream
时,我收到编译错误(注释掉的行)。我想我把一切都搞砸了
String str=null;
try {
FileInputStream fileInputStream=new FileInputStream("myfile.txt");
DataInputStream dataInputStream=new DataInputStream(fileInputStream);
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(dataInputStream));
str=bufferedReader.readLine();
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println(str);
//dataInputStream.close();