从eclipse中的文件中读取

我正在尝试从文本文件中读取数据到我的java程序。 但是,无论我把文件放在哪里,eclipse都会连续给我一个Source not found错误。

我在项目目录中创建了一个额外的sources文件夹,有问题的文件在它和项目的bin文件中,它仍然无法找到它。

我甚至把它的副本放在我的桌面上,并在它要求我浏览源查找路径时尝试指向eclipse。

无论我做什么都找不到文件。

这是我的代码,如果它是相关的:

System.out.println(System.getProperty("user.dir")); File file = new File("file.txt"); Scanner scanner = new Scanner(file); 

另外,它说用户目录是项目目录,也有副本。

我不知道该怎么做。

谢谢,Alex

在尝试下面的建议并再次刷新之后,我遇到了许多错误。

 FileNotFoundException(Throwable).(String) line: 195 FileNotFoundException(Exception).(String) line: not available FileNotFoundException(IOException).(String) line: not available FileNotFoundException.(String) line: not available URLClassPath$JarLoader.getJarFile(URL) line: not available URLClassPath$JarLoader.access$600(URLClassPath$JarLoader, URL) line: not available URLClassPath$JarLoader$1.run() line: not available AccessController.doPrivileged(PrivilegedExceptionAction) line: not available [native method] URLClassPath$JarLoader.ensureOpen() line: not available URLClassPath$JarLoader.(URL, URLStreamHandler, HashMap) line: not available URLClassPath$3.run() line: not available AccessController.doPrivileged(PrivilegedExceptionAction) line: not available [native method] URLClassPath.getLoader(URL) line: not available URLClassPath.getLoader(int) line: not available URLClassPath.access$000(URLClassPath, int) line: not available URLClassPath$2.next() line: not available URLClassPath$2.hasMoreElements() line: not available ClassLoader$2.hasMoreElements() line: not available CompoundEnumeration.next() line: not available CompoundEnumeration.hasMoreElements() line: not available ServiceLoader$LazyIterator.hasNext() line: not available ServiceLoader$1.hasNext() line: not available LocaleServiceProviderPool$1.run() line: not available AccessController.doPrivileged(PrivilegedExceptionAction) line: not available [native method] LocaleServiceProviderPool.(Class) line: not available LocaleServiceProviderPool.getPool(Class) line: not available NumberFormat.getInstance(Locale, int) line: not available NumberFormat.getNumberInstance(Locale) line: not available Scanner.useLocale(Locale) line: not available Scanner.(Readable, Pattern) line: not available Scanner.(ReadableByteChannel) line: not available Scanner.(File) line: not available 

使用的代码:

 System.out.println(System.getProperty("user.dir")); File file = new File(System.getProperty("user.dir") + "/file.txt"); Scanner scanner = new Scanner(file); 

您是否在复制文件后尝试刷新(右键单击 – >刷新)项目文件夹? 这将使您的文件系统与Eclipse的内部文件系统SYNC。

运行Eclipse项目时,CWD(当前工作目录)是项目的根目录。 不是bin的目录。 不是src的目录,而是根目录。

此外,如果您使用的是Linux,请记住其文件系统通常区分大小写。

您是否尝试过使用绝对路径:

 File file = new File(System.getProperty("user.dir") + "/file.txt"); 

您正在搜索/读取执行目录中的文件“fiel.txt”(我认为该类存储在哪里)。

如果你想读取给定目录中的文件,你必须这样说:

 File file = new File(System.getProperty("user.dir")+"/"+"file.txt"); 

您也可以为目录提供相对路径,例如“./images/photo.gif”,例如子目录。

请注意,分隔符还有一个属性(在我的示例中硬编码为“/”)

关于纪尧姆

我正在使用eclipse,因为“找不到文件exception”而无法读取文件。 我为解决这个问题所做的是将文件移动到项目的根目录。 希望这可以帮助。

您的代码没有任何问题,当我在user.dir目录中有file.txt时,以下工作正常。

 import java.io.File; import java.util.Scanner; public class testme { public static void main(String[] args) { System.out.println(System.getProperty("user.dir")); File file = new File("file.txt"); try { Scanner scanner = new Scanner(file); } catch (Exception e) { System.out.println(e); } } } 

不要将Eclipse与文件所在的位置信任。 使用Windows资源管理器或同等文件转到实际文件系统并检查。

根据您的编辑,我认为我们还需要查看您的import语句。

你只需要获取文件的绝对路径,因为你要查找的文件不存在于eclipse的运行时工作空间中,你可以使用 – getProperty()或getLocationURI()方法来获取文件的绝对路径

有时,即使文件位于正确的目录中,仍然存在“找不到文件”exception。 您可以做的一件事是将文本文件放在eclipse中,您的类位于左侧。 它会询问您是否要复制,单击是。 有时它有帮助。