爲了這個任務的目的,我們被要求製作一個使用文件類的程序(我知道輸入流比較好),但是,我們必須要求用戶輸入.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