【问题标题】:Read from file in eclipse在eclipse中从文件中读取
【发布时间】:2009-03-25 10:46:16
【问题描述】:

我正在尝试从文本文件中读取数据以将数据输入到我的 java 程序中。但是,无论我将文件放在哪里,eclipse 都会不断地给我一个 Source not found 错误。

我在项目目录中创建了一个额外的源文件夹,有问题的文件在它和项目的 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);

【问题讨论】:

    标签: java eclipse file-io


    【解决方案1】:

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

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

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

    【讨论】:

    • 我认为问题是我刷新了 src 和 files 文件,但没有刷新项目文件,所以它从未找到它。感谢您的帮助
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

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

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

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

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

      问候 纪尧姆

      【讨论】:

        【解决方案4】:

        我正在使用 Eclipse,但由于“找不到文件异常”而无法读取文件。我为解决这个问题所做的是将文件移动到项目的根目录。希望这会有所帮助。

        【讨论】:

          【解决方案5】:

          您的代码没有任何问题,当我在 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 资源管理器或等效工具访问实际文件系统并检查。

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

          【讨论】:

            【解决方案6】:

            您只需要获取文件的绝对路径,因为您要查找的文件在 Eclipse 的运行时工作区中不存在 您可以使用 - getProperty() 或 getLocationURI() 方法来获取文件的绝对路径

            【讨论】:

              【解决方案7】:

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

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2020-11-01
                • 2016-11-03
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多