从日食中的文件读取
我正在尝试从文本文件中读取数据以将数据输入到我的java程序中。但是,无论我把文件放在哪里,eclipse都会不断地给我一个“找不到源”错误。
我已经在项目目录中创建了一个额外的sources文件夹,有问题的文件既在它又在项目的bin文件中,但它仍然找不到它。
我甚至把它放在桌面上的副本,当它要求我浏览源查找路径时,我尝试将eclipse指向那里。
无论我做什么,它都找不到文件。
这是我的代码,以防万一它相关:
System.out.println(System.getProperty("user.dir"));
File file = new File("file.txt");
Scanner scanner = new Scanner(file);
此外,它说用户目录是项目目录,那里也有一个副本。
我不知道该怎么办。
谢谢,亚历克斯
在尝试了下面的建议并再次刷新之后,我遇到了许多错误。
FileNotFoundException(Throwable).<init>(String) line: 195
FileNotFoundException(Exception).<init>(String) line: not available
FileNotFoundException(IOException).<init>(String) line: not available
FileNotFoundException.<init>(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<T>) line: not available [native method]
URLClassPath$JarLoader.ensureOpen() line: not available
URLClassPath$JarLoader.<init>(URL, URLStreamHandler, HashMap) line: not available
URLClassPath$3.run() line: not available
AccessController.doPrivileged(PrivilegedExceptionAction<T>) 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<E>.next() line: not available
CompoundEnumeration<E>.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<T>) line: not available [native method]
LocaleServiceProviderPool.<init>(Class<LocaleServiceProvider>) line: not available
LocaleServiceProviderPool.getPool(Class<LocaleServiceProvider>) line: not available
NumberFormat.getInstance(Locale, int) line: not available
NumberFormat.getNumberInstance(Locale) line: not available
Scanner.useLocale(Locale) line: not available
Scanner.<init>(Readable, Pattern) line: not available
Scanner.<init>(ReadableByteChannel) line: not available
Scanner.<init>(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);