Java 在eclipse中从文件中读取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/681059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:50:03  来源:igfitidea点击:

Read from file in eclipse

javaeclipsefile-io

提问by Buzkie

I'm trying to read from a text file to input data to my java program. However, eclipse continuosly gives me a Source not found error no matter where I put the file.

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

I've made an additional sources folder in the project directory, the file in question is in both it and the bin file for the project and it still can't find it.

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

I even put a copy of it on my desktop and tried pointing eclipse there when it asked me to browse for the source lookup path.

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

No matter what I do it can't find the file.

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

here's my code in case it's pertinent:

这是我的代码,以防万一:

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


    Scanner scanner = new Scanner(file);

in addition, it says the user directory is the project directory and there is a copy there too.

此外,它说用户目录是项目目录,那里也有一个副本。

I have no clue what to do.

我不知道该怎么做。

Thanks, Alex

谢谢,亚历克斯

after attempting the suggestion below and refreshing again, I was greeted by a host of errors.

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

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.access0(URLClassPath$JarLoader, URL) line: not available  
URLClassPath$JarLoader.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.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
System.out.println(System.getProperty("user.dir"));
    File file = new File(System.getProperty("user.dir") + "/file.txt");


    Scanner scanner = new Scanner(file);
0(URLClassPath, int) line: not available URLClassPath.next() line: not available URLClassPath.hasMoreElements() line: not available ClassLoader.hasMoreElements() line: not available CompoundEnumeration<E>.next() line: not available CompoundEnumeration<E>.hasMoreElements() line: not available ServiceLoader$LazyIterator.hasNext() line: not available ServiceLoader.hasNext() line: not available LocaleServiceProviderPool.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

code used:

使用的代码:

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

采纳答案by Pablo Santa Cruz

Did you try refreshing (right click -> refresh) the project folder after copying the file in there? That will SYNC your file system with Eclipse's internal file system.

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

When you run Eclipse projects, the CWD (current working directory) is project's root directory. Not bin's directory. Not src's directory, but the root dir.

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

Also, if you're in Linux, remember that its file systems are usually case sensitive.

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

回答by kgiannakakis

Have you tried using an absolute path:

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

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

回答by paxdiablo

There's nothing wrong with your code, the following works fine for me when I have the file.txt in the user.dir directory.

您的代码没有任何问题,当我在 user.dir 目录中有 file.txt 时,以下内容对我来说很好用。

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

Don't trust Eclipse with where it says the file is. Go out to the actual filesystem with Windows Explorer or equivalent and check.

不要相信 Eclipse 的文件所在位置。使用 Windows 资源管理器或等效工具访问实际文件系统并检查。

Based on your edit, I think we need to see your import statements as well.

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

回答by PATRY Guillaume

You are searching/reading the file "fiel.txt" in the execution directory (where the class are stored, i think).

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

If you whish to read the file in a given directory, you have to says so :

如果您希望读取给定目录中的文件,则必须这样说:

##代码##

You could also give the directory with a relative path, eg "./images/photo.gif) for a subdirecory for example.

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

Note that there is also a property for the separator (hard-coded to "/" in my exemple)

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

regards Guillaume

问候纪尧姆

回答by shreyu108

you just need to get the absolute-path of the file, since the file you are looking for doesnt exist in the eclipse's runtime workspace you can use - getProperty() or getLocationURI() methods to get the absolute-path of the file

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

回答by Electron

I am using eclipse and I was stuck on not being able to read files because of a "file not found exception". What I did to solve this problem was I moved the file to the root of my project. Hope this helps.

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

回答by cdhw

Sometimes, even when the file is in the right directory, there is still the "file not found" exception. One thing you could do is to drop the text file inside eclipse, where your classes are, on the left side. It is going to ask you if you want to copy, click yes. Sometimes it helps.

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