Java 相对文件路径如何在 Eclipse 中工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/437382/
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
How do relative file paths work in Eclipse?
提问by PHLAK
So my 2009 new years resolution is to learn Java. I recently acquired "Java for Dummies" and have been following along with the demo code in the book by re-writing it using Eclipse. Anyway, every example in the book that uses a relative path does not seem to read the .txt file it's supposed to read from.
所以我 2009 年的新年决心是学习 Java。我最近获得了“Java for Dummies”,并一直在关注书中的演示代码,使用 Eclipse 重写它。无论如何,书中使用相对路径的每个示例似乎都没有读取它应该读取的 .txt 文件。
Here is the sample code:
这是示例代码:
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.GridLayout;
class TeamFrame extends JFrame {
public TeamFrame() throws IOException {
PlayerPlus player;
Scanner myScanner = new Scanner(new File("Hankees.txt"));
for (int num = 1; num <= 9; num++) {
player = new PlayerPlus(myScanner.nextLine(), myScanner.nextDouble());
myScanner.nextLine();
addPlayerInfo(player);
}
add(new JLabel());
add(new JLabel(" ------"));
add(new JLabel("Team Batting Aberage:"));
add(new JLabel(PlayerPlus.findTeamAverageString()));
setTitle("The Hankees");
setLayout(new GridLayout(11,2));
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
void addPlayerInfo(PlayerPlus player) {
add(new JLabel(player.getName()));
add(new JLabel(player.getAverageString()));
}
}
And you can see in the below screen shot I have included this file.
你可以在下面的屏幕截图中看到我已经包含了这个文件。
image no longer available
图像不再可用
Also, I have verified that when I build the application that a copy of Hankees.txt is placed in the bin folder with the compiled .class files.
此外,我已经验证,当我构建应用程序时,Hankees.txt 的副本与已编译的 .class 文件一起放置在 bin 文件夹中。
Lastly, if I change line 12 to the following and place Hankees.txt in the root of my C:\ drive the program compiles and runs fine.
最后,如果我将第 12 行更改为以下内容并将 Hankees.txt 放在我的 C:\ 驱动器的根目录中,程序将编译并正常运行。
Scanner myScanner = new Scanner(new File("C:\Hankees.txt"));
So basically, my question is what am I doing wrong? Or is Eclipse responsible for this in some way?
所以基本上,我的问题是我做错了什么?或者 Eclipse 以某种方式对此负责?
Thanks for any and all help!
感谢您的任何帮助!
采纳答案by jjnguy
You need "src/Hankees.txt"
你需要 "src/Hankees.txt"
Your file is in the source folder which is not counted as the working directory.\
您的文件位于不计为工作目录的源文件夹中。\
Or you can move the file up to the root directory of your project and just use "Hankees.txt"
或者您可以将文件移动到项目的根目录并使用 "Hankees.txt"
回答by nasty pasty
Yeah, eclipse sees the top directory as the working/root directory, for the purposes of paths.
是的,出于路径的目的,eclipse 将顶级目录视为工作/根目录。
...just thought I'd add some extra info. I'm new here! I'd like to help.
...只是想我会添加一些额外的信息。我是新来的!我愿意帮忙。
回答by Rob
Paraphrasing from http://java.sun.com/javase/6/docs/api/java/io/File.html:
从http://java.sun.com/javase/6/docs/api/java/io/File.html 转述:
The classes under java.io
resolve relative pathnames against the current user directory, which is typically the directory in which the virtual machine was started.
下的类根据java.io
当前用户目录(通常是启动虚拟机的目录)解析相对路径名。
Eclipse sets the working directory to the top-level project folder.
Eclipse 将工作目录设置为顶级项目文件夹。
回答by Stephen Denne
A project's build path defines which resources from your source folders are copied to your output folders. Usually this is set to Include all files.
项目的构建路径定义将源文件夹中的哪些资源复制到输出文件夹。通常这被设置为包括所有文件。
New run configurations default to using the project directory for the working directory, though this can also be changed.
新的运行配置默认使用项目目录作为工作目录,尽管这也可以更改。
This code shows the difference between the working directory, and the location of where the class was loaded from:
此代码显示了工作目录与加载类的位置之间的区别:
public class TellMeMyWorkingDirectory {
public static void main(String[] args) {
System.out.println(new java.io.File("").getAbsolutePath());
System.out.println(TellMeMyWorkingDirectory.class.getClassLoader().getResource("").getPath());
}
}
The output is likely to be something like:
输出可能类似于:
C:\your\project\directory
/C:/your/project/directory/bin/
回答by Bernie Perez
This is really similar to another question. How should I load files into my Java application?
这与另一个问题非常相似。 我应该如何将文件加载到我的 Java 应用程序中?
How should I load my files into my Java Application?
我应该如何将我的文件加载到我的 Java 应用程序中?
You do notwant to load your files in by:
你不希望加载的文件:
C:\your\project\file.txt
this is bad!
这很糟糕!
You should use getResourceAsStream.
您应该使用 getResourceAsStream。
InputStream inputStream = YourClass.class.getResourceAsStream(“file.txt”);
And also you should use File.separator; which is the system-dependent name-separator character, represented as a string for convenience.
而且你应该使用File.separator; 这是系统相关的名称分隔符,为方便起见表示为字符串。
回答by Sudip Bhandari
You can always get your runtime path by using:
您始终可以使用以下方法获取运行时路径:
String path = new File(".").getCanonicalPath();
This provides valuable information about where to put files and resources.
这提供了有关放置文件和资源的位置的宝贵信息。