since readLine() of DataInputStream is depreacted, now we can use BufferReader to do this job.
public static void main(String args[]){
try {
File file = new File("d://myfile.txt");
FileInputStream fis = new FileInputStream(file );
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = null;
while (( line = br.readLine()) != null){
System.out.println(line);
}
fis.close();
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String args[]){
try {
File file = new File("d://myfile.txt");
FileInputStream fis = new FileInputStream(file );
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = null;
while (( line = br.readLine()) != null){
System.out.println(line);
}
fis.close();
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
本文介绍了一种使用BufferedReader替代DataInputStream中的sincereadLine()方法来读取文件内容的方法。通过示例代码展示了如何打开指定路径的文件,并逐行读取文件内容直至结束。同时处理了可能发生的FileNotFoundException和IOException。
422

被折叠的 条评论
为什么被折叠?



