为了这个任务的目的,我们被要求制作一个使用文件类的程序(我知道输入流比较好),但是,我们必须要求用户输入.txt文件的名称。用户输入他们想要读取的文件名称?
public class input {
public static void main(String[] args) throws FileNotFoundException {
Scanner s = new Scanner(System.in);
String name;
int lineCount = 0;
int wordCount = 0;
System.out.println("Please type the file you want to read in: ");
name = s.next();
File input = new File("C:\\Users\\Ceri\\workspace1\\inputoutput\\src\\inputoutput\\lab1task3.txt");
Scanner in = new Scanner(input);
我怎么会得到
File input = new File(...);
搜索的文件只是打字 'lab1task3' 不起作用。
编辑: - 错误
Exception in thread "main" java.io.FileNotFoundException: \lab1task3.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at inputoutput.input.main(input.java:19)
你想要得到的'File'给定文件夹里面?或者你想搜索整个文件系统? – MinecraftShamrock 2015-02-11 17:26:01
你会得到什么错误?应该搜索该文件还是只是打开它? – Cyphrags 2015-02-11 17:26:12
@MinecraftShamrock是在给定的文件夹或相关文件夹中。 – 2015-02-11 17:27:08