Scanner类的nextLine方法不仅可以从键盘接收数据,也可以从文件读取数据,且都是以回车为分隔.
1 public class SY64 { 2 public static void main(String[] args) throws Exception { 3 int lineNumber=0; 4 File f1 = new File("d:\\test.txt"); 5 File f2 = new File("d:\\test2.txt"); 6 PrintStream ps = new PrintStream(f2); 7 Scanner in = new Scanner(f1); 8 String st = null; 9 while(in.hasNext()){ 10 st = in.nextLine(); 11 ps.println((++lineNumber) + st); 12 } 13 ps.close(); 14 in.close(); 15 } 16 }
本文介绍了一个Java程序示例,该程序使用Scanner类的nextLine方法从文件中逐行读取数据,并将每一行的内容连同行号一起写入到另一个文件中。此方法不仅适用于从键盘输入获取数据,同样可以用于文件读取。
2361

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



