java写一个程序,实现从文件中读出文件内容,并将其打印在屏幕当中,并标注上行号
public class PrintFileWithLineNumber {
public static void main(String[] args) throws IOException {
printFile("D:\\count.txt");
}
private static void printFile(String string) throws IOException {
File file = new File(string);
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String s = null;
int i = 1;
while ((s = br.readLine()) != null) {
System.out.println(i + " " + s);
i++;
}
}
}