Java 我想在 eclipse 中使用的文本文件放在哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2850674/
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
Where to put a textfile I want to use in eclipse?
提问by MJB
I need to read a text file when I start my program. I'm using eclipse and started a new java project. In my project folder I got the "src" folder and the standard "JRE System Library" + staedteliste.txt... I just don't know where to put the text file. I literally tried every folder I could think off....I cannot use a "hard coded" path because the text file needs to be included with my app...
当我启动我的程序时,我需要读取一个文本文件。我正在使用 eclipse 并开始了一个新的 java 项目。在我的项目文件夹中,我得到了“src”文件夹和标准的“JRE 系统库”+ staedteliste.txt ......我只是不知道将文本文件放在哪里。我真的尝试了我能想到的每个文件夹......我不能使用“硬编码”路径,因为文本文件需要包含在我的应用程序中......
I use the following code to read the file, but I get this error:
我使用以下代码读取文件,但出现此错误:
Error:java.io.FileNotFoundException:staedteliste.txt(No such file or directory)
public class Test {
ArrayList<String[]> values;
public static void main(String[] args) {
// TODO Auto-generated method stub
URL url = Test.class.getClassLoader().getResource("src/mjb/staedteliste.txt");
System.out.println(url.getPath()); // I get a nullpointerexception here!
loadList();
}
public static void loadList() {
BufferedReader reader;
String zeile = null;
try {
reader = new BufferedReader(new FileReader("src/mjb/staedteliste.txt"));
zeile = reader.readLine();
ArrayList<String[]> values = new ArrayList<String[]>();
while (zeile != null) {
values.add(zeile.split(";"));
zeile = reader.readLine();
}
System.out.println(values.size());
System.out.println(zeile);
} catch (IOException e) {
System.err.println("Error :"+e);
}
}
}
}
采纳答案by leonbloy
Ask first yourself: Is your file an internalcomponent of your application? (That usually implies that it's packed inside your JAR, or WAR if it is a web-app; typically, it's some configuration file or static resource, read-only).
先问问自己:您的文件是应用程序的内部组件吗?(这通常意味着它被打包在你的 JAR 中,或者 WAR 如果它是一个 web 应用程序;通常,它是一些配置文件或静态资源,只读)。
If the answer is yes, you don't want to specify an absolute pathfor the file. But you neither want to access it with a relative path(as your example), because Java assumes that path is relative to the "current directory". Usually the preferred way for this scenario is to load it relatively from the classpath.
如果答案是肯定的,则您不想为文件指定绝对路径。但是您既不想使用相对路径(如您的示例)访问它,因为 Java 假定该路径是相对于“当前目录”的。通常这种情况的首选方法是从 classpath 相对地加载它。
Java provides you the classLoader.getResource()method for doing this. And Eclipse (in the normal setup) assumes src/
is to be in the root of your classpath, so that, after compiling, it copies everything to your output directory ( bin/
), the java files in compiled form ( .class
), the rest as is.
Java 为您提供了用于执行此操作的classLoader.getResource()方法。并且 Eclipse(在正常设置中)假定src/
位于您的类路径的根目录中,因此,在编译之后,它会将所有内容复制到您的输出目录 ( bin/
)、已编译形式的 java 文件 ( .class
) 中,其余部分保持原样。
So, for example, if you place your file in src/Files/myfile.txt
, it will be copied at compile time to bin/Files/myfile.txt
; and, at runtime, bin/
will be in (the root of) your classpath. So, by calling getResource("/Files/myfile.txt")
(in some of its variants) you will be able to read it.
因此,例如,如果您将文件放在 中src/Files/myfile.txt
,它将在编译时复制到bin/Files/myfile.txt
; 并且,在运行时,bin/
将在您的类路径(的根)中。因此,通过调用getResource("/Files/myfile.txt")
(在其某些变体中),您将能够阅读它。
Edited: Further, if your file is conceptually tied to a java class (eg, some com.example.MyClass
has a MyClass.cfg
associated configuration file), you can use the getResource() method from the classand use a (resource) relative path: MyClass.getResource("MyClass.cfg")
. The file then will be searched in the classpath, but with the class package pre-appended. So that, in this scenario, you'll typically place your MyClass.cfg
and MyClass.java
files in the same directory.
编辑:此外,如果您的文件在概念上与 java 类相关联(例如,某些文件com.example.MyClass
具有MyClass.cfg
关联的配置文件),您可以使用该类中的getResource() 方法并使用(资源)相对路径:MyClass.getResource("MyClass.cfg")
。然后将在类路径中搜索该文件,但预先附加了类包。因此,在这种情况下,您通常会将MyClass.cfg
和MyClass.java
文件放在同一目录中。
回答by Gilbert Le Blanc
Depending on your Java class package name, you're probably 4 or 5 levels down the directory structure.
根据您的 Java 类包名称,您可能在目录结构下的 4 或 5 级。
If your Java class package is, for example, com.stackoverflow.project, then your class is located at src/com/stackoverflow/project.
例如,如果您的 Java 类包是 com.stackoverflow.project,则您的类位于 src/com/stackoverflow/project。
You can either move up the directory structure with multiple ../
, or you can move the text file to the same package as your class. It would be easier to move the text file.
您可以使用多个 向上移动目录结构../
,也可以将文本文件移动到与您的类相同的包中。移动文本文件会更容易。
回答by Greg Adamski
One path to take is to
一种方法是
- Add the file you're working with to the classpath
Use the resource loader to locate the file:
URL url = Test.class.getClassLoader().getResource("myfile.txt"); System.out.println(url.getPath()); ...
- Open it
- 将您正在使用的文件添加到类路径
使用资源加载器定位文件:
URL url = Test.class.getClassLoader().getResource("myfile.txt"); System.out.println(url.getPath()); ...
- 打开它
回答by pdbartlett
You should probably take a look at the various flavours of getResource in the ClassLoader class: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/ClassLoader.html
您可能应该看看 ClassLoader 类中 getResource 的各种风格:http: //java.sun.com/j2se/1.4.2/docs/api/java/lang/ClassLoader.html
回答by mr popo
Suppose you have a project called "TestProject" on Eclipse and your workspace folder is located at E:/eclipse/workspace. When you build an Eclipse project, your classpath is then e:/eclipse/workspace/TestProject
. When you try to read "staedteliste.txt", you're trying to access the file at e:/eclipse/workspace/TestProject/staedteliste.txt
.
假设您在 Eclipse 上有一个名为“TestProject”的项目,并且您的工作区文件夹位于 E:/eclipse/workspace。当您构建一个 Eclipse 项目时,您的类路径是e:/eclipse/workspace/TestProject
. 当您尝试阅读“staedteliste.txt”时,您正在尝试访问位于e:/eclipse/workspace/TestProject/staedteliste.txt
.
If you want to have a separate folder for your project, then create the Files folder under TestProject and then access the file with (the relative path) /Files/staedteliste.txt
. If you put the file under the src folder, then you have to access it using /src/staedteliste.txt
. A Files folder inside the src folder would be /src/Files/staedteliste.txt
如果您想为您的项目创建一个单独的文件夹,请在 TestProject 下创建 Files 文件夹,然后使用 (相对路径) 访问该文件/Files/staedteliste.txt
。如果将文件放在 src 文件夹下,则必须使用/src/staedteliste.txt
. src 文件夹内的 Files 文件夹将是/src/Files/staedteliste.txt
Instead of using the the relative path you can use the absolute one by adding e:/eclipse/workspace/
at the beginning, but using the relative path is better because you can move the project without worrying about refactoring as long as the project folder structure is the same.
您可以e:/eclipse/workspace/
在开头添加绝对路径而不是使用相对路径,但使用相对路径更好,因为只要项目文件夹结构相同,您就可以移动项目而无需担心重构。
回答by sandejai
MJB
麻省理工学院
Please try this
请试试这个
In eclipse "Right click" on the text file u wanna use, see and copy the complete path stored in HDD like (if in UNIX "/home/sjaisawal/Space-11.4-template/provisioning/devenv/Test/src/testpath/testfile.txt")
put this complete path and try.
if it works then class-path issue else GOK :)
在 Eclipse 中“右键单击”您要使用的文本文件,查看并复制存储在 HDD 中的完整路径(如果在 UNIX 中“/home/sjaisawal/Space-11.4-template/provisioning/devenv/Test/src/testpath/测试文件.txt")
把这个完整的路径并尝试。
如果它有效,那么类路径问题否则 GOK :)
回答by Bernard Banta
Just create a folder Files
under src
and put your file there.
This will look like src/Files/myFile.txt
只要创建一个文件夹Files
下src
,并把你的文件存在。这看起来像src/Files/myFile.txt
Note:In your code you need to specify like this Files/myFile.txt
e.g.
注意:在您的代码中,您需要像这样指定Files/myFile.txt
例如
getResource("Files/myFile.txt");
So when you build your project and run the .jar file this should be able to work.
因此,当您构建项目并运行 .jar 文件时,这应该能够正常工作。
回答by Mark
If this is a simple project, you should be able to drag the txt file right into the project folder. Specifically, the "project folder" would be the highest level folder. I tried to do this (for a homework project that I'm doing) by putting the txt file in the src folder, but that didn't work. But finally I figured out to put it in the project file.
如果这是一个简单的项目,您应该能够将 txt 文件直接拖到项目文件夹中。具体来说,“项目文件夹”将是最高级别的文件夹。我试图通过将 txt 文件放在 src 文件夹中来做到这一点(对于我正在做的家庭作业项目),但这不起作用。但最后我想出把它放在项目文件中。
A good tutorial for this is http://www.vogella.com/articles/JavaIO/article.html. I used this as an intro to i/o and it helped.
一个很好的教程是http://www.vogella.com/articles/JavaIO/article.html。我用它作为 i/o 的介绍,它有帮助。
回答by Nenad Bulatovic
Take a look at this video
看看这个视频
All what you have to do is to select your file (assuming it's same simple form of txt file), then drag it to the project in Eclipse and then drop it there. Choose Copy instead of Link as it's more flexible. That's it - I just tried that.
您所要做的就是选择您的文件(假设它是 txt 文件的简单形式),然后将其拖到 Eclipse 中的项目中,然后将其放在那里。选择复制而不是链接,因为它更灵活。就是这样 - 我刚试过。