java - 在eclipse中读取文件

标签 java eclipse file-io

我正在尝试从文本文件中读取数据以将数据输入到我的 Java 程序中。然而,无论我把文件放在哪里,eclipse 都会不断地给我一个找不到源的错误。

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

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

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

这是我的代码,以防相关:

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);

最佳答案

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

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

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

关于java - 在eclipse中读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/681059/

相关文章:

java - Android Activity 从未被 GC 导致内存不足异常

java - 如何避免使用 toString().getBytes ("UTF-8") 来避免 OOM 错误?有没有更好的方法从 StringWriter 转换为 byte[]?

java - 如果两个库的类具有相同的类和包名称供其内部使用

java - 为什么 xml 文件无法与可执行文件 jar 一起使用

java - 该方法必须覆盖父类(super class)方法

java - 在 Java 中,使用 BufferedWriter 或直接写入文件有什么区别?

java - 有没有办法将一个 ArrayList 与另一个 ArrayList 建立一对多关系?

java - 连接Eclipse到Docker容器进行远程调试

python - 做多线程txt文件I/O时遇到 "MemoryError",寻找更好的解决方案

c# - 如何将文件从 "resources"复制到磁盘上的文件夹