【发布时间】:2016-10-13 09:19:30
【问题描述】:
我是否可以使用扫描器从用户那里读取以输入某个输入,然后创建一个新实例以从文件中读取?
Scanner sc = new Scanner(System.in);
System.out.print("Enter the file name: ");
String fileName = sc.next();
sc = new Scanner(fileName);
displayAll(sc); //a static void method that takes the Scanner object as a parameter and is supposed to read and display the input stored in the .txt file
【问题讨论】:
-
是的,你可以。你为什么不试试呢?
-
只需使用
if,找到匹配后,重新初始化扫描仪 -
@PavneetSingh 他有一个工作代码。如果文件名是带有扩展名的文件的路径,它将起作用
-
@xenteros OP 说
user to enter a certain input然后create a new instance of it to read from a file
标签: java java.util.scanner filereader