答案是:Scanner读取,初学者大部分都用过这货,然而这货还有这样两个构造方法:
public Scanner(File source);
public Scanner(InputStream stream);
那就是说:我完全可以传递个文件或者文件输入流过去,上代码:
1 public static void main(String[] args) { 2 try { 3 Scanner input = new Scanner(new FileInputStream("D:/a.txt")); 4 StringBuilder builder = new StringBuilder(); 5 while (input.hasNextLine()) { 6 builder.append(input.nextLine()); 7 } 8 System.out.println(builder.toString()); 9 input.close(); 10 } catch (FileNotFoundException e) { 11 e.printStackTrace(); 12 } 13 }
时间: 2024-11-08 16:18:28