FileInputStream 读取的文件存储在哪里?

Where is the file stored to be read by FileInputStream?

无论我做什么,我都会 'File not found!'。

FileInputStream fin;
    try {
        fin = new FileInputStream("foo.txt");
        String str = IOUtils.toString(fin);
        System.out.println(str);
    } catch (FileNotFoundException f) {
        System.out.println("File not found!");
    }

您工作的目录中是否有 foo.txt?

如果您正在使用命令 window 并且位于某个位置,比如 C:\,那么您的代码期望 foo.txt 出现在那里。

如果您的 foo.txt 出现在其他路径中,请在您的代码中使用完整路径。

确保您使用的路径正确。尝试右键单击并继续“属性”并检查文件的路径。复制并粘贴路径并将所有 \ 替换为 /\.

如果您临时将此行添加到您的代码中:

System.out.println(new File("foo.txt").getAbsolutePath());    

它应该会告诉您它希望在何处找到该文件。如果文件不在该位置,则您必须指定路径或移动文件以使其位于该位置。