无法使用Eclipse创建正常的File.txt

时间:2014-11-13 22:26:45

标签: java eclipse

我遇到一个问题,我无法创建普通文件! 这是我的代码:

public class test {

    public static void main(String[] args) throws Exception {
        File myFile = new File("word.txt");
        System.out.println("Attempting to read from file in: "+myFile.getCanonicalPath());

        Scanner input = new Scanner(myFile);
        String in = "";
        in = input.nextLine();
    }

}

我有这个错误:

Attempting to read from file in: C:\Users\AHMED\workspace\Video\word.txt
Exception in thread "main" java.io.FileNotFoundException: word.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 test.main(test.java:11)

1 个答案:

答案 0 :(得分:1)

这一行:

File myFile = new File("word.txt");

不会在磁盘上创建新文件。它只是在内存中创建一个File对象。

因此,当您尝试从文件中读取时,它不存在(在磁盘上),因此您的错误。

要在磁盘上实际创建一个真实文件,您需要向其写入数据,或者通过触摸&#34;创建一个空文件。文件: