java怎么用scanner搜索
从图片来看,是用的Scanner 来读取文件路径
然后通过File对象来判断文件是否存在
import?java.io.File;import?java.util.Scanner;
public?class?Demo?{
public?static?void?main(String[]?args)?throws?Exception?{
while?(true)?{
System.out.print("what?is?the?name?of?the?input?file?");
Scanner?sc?=?new?Scanner(System.in);
String?filePaht?=?sc.nextLine();
File?file?=?new?File(filePaht);
if?(!(file.exists()?&&?file.isFile()))?{
System.out.println("Error?scanning?that?file?,?please?try?again.");
continue;
}
sc?=?new?Scanner(file);
while?(sc.hasNextLine())?{
System.out.println(sc.nextLine());
}
}
}
}
运行测试
what?is?the?name?of?the?input?file?d:\abc.txtError?scanning?that?file?,?please?try?again.
what?is?the?name?of?the?input?file?c:\test.txt
天涯
风云
突袭
狂暴
月之影