java按行读入文件并打印的示例程序
import java.io.*;
public class Main {
public static void main(String[] args) {
try{
// 需要读取的文件路径
String path = "D:\\code\\java\\typoraImgTypeTrans\\04_Process.md";
File file = new File(path);
// 判断文件是否存在
if (file.isFile() && file.exists()){
System.out.println("fileExist");
InputStreamReader read = new InputStreamReader(new FileInputStream(file));
BufferedReader bufferedReader = new BufferedReader(read);
String lineText = null;
// 按行读取文件并打印,如果需要对内容进行操作可以在这里进行
while((lineText = bufferedReader.readLine())!=null){
System.out.println(lineText);
}
}else{
System.out.println("file doesn't exist");
}
} catch(IOException e){
e.printStackTrace();
}
}
}
执行结果