简体   繁体   English

我要把我想用 Java 读取的 txt 文件放在哪里?

[英]Where do I put the txt file that I want to read in Java?

If I am building an application in Eclipse using Java, and I want this application to read from a txt file, where should I put the txt file in the package in order to not have to specify its path (so that I can just say "file.txt").如果我正在使用 Java 在 Eclipse 中构建一个应用程序,并且我希望这个应用程序从一个 txt 文件中读取,我应该将 txt 文件放在包中的什么位置以便不必指定其路径(这样我就可以说“文件.txt”)。 I thought that it was supposed to go in the src folder, but it's not working.我认为它应该放在 src 文件夹中,但它不起作用。

Right click the project folder and click New -> file.右键单击项目文件夹,然后单击新建 -> 文件。 The file will be in the Project folder and not in the source folder.该文件将在项目文件夹中,而不是在源文件夹中。

Put the file in the folder from where you run your Java application (your current/working folder).将文件放在运行 Java 应用程序的文件夹(当前/工作文件夹)中。 If you're using the default settings of Eclipse to run your application, you should put the file directly inside the Eclipse project folder.如果您使用 Eclipse 的默认设置来运行您的应用程序,您应该将该文件直接放在 Eclipse 项目文件夹中。 To create the file in the Eclipse project, you can simply drag-and-drop it there or right-click on the Eclipse project and create a new file.要在 Eclipse 项目中创建文件,您可以简单地将它拖放到那里或右键单击 Eclipse 项目并创建一个新文件。

The way this can be done is using .getResourceAsStream("file.txt")这样做的方法是使用 .getResourceAsStream("file.txt")

SO thread SO 线程

Downvoted for a correct answer?对正确答案投反对票? Wierd...怪...

If you don't want to specify a path, and want to open a File from the file system using the java.io.File API, then put it in the working directory.如果您不想指定路径,并且想使用 java.io.File API 从文件系统打开一个 File,则将其放在工作目录中。

In Eclipse, the working directory defaults to the root level of your project, but you can modify it (and also review what it is set to) in an Eclipse Run Configuration.在 Eclipse 中,工作目录默认为项目的根级别,但您可以在 Eclipse 运行配置中修改它(并查看它的设置)。 They are accessible under the "Run > Run Configurations..." menu option, and the working directory setting is under the "Arguments" tab for Java programs.它们可以在“运行 > 运行配置...”菜单选项下访问,工作目录设置在 Java 程序的“参数”选项卡下。

The important thing is for the directory containing your file to be on the classpath.重要的是包含您的文件的目录位于类路径上。 When you're using Eclipse's run dialog for the settings of your project, there's a tab for classpath.当您使用 Eclipse 的运行对话框进行项目设置时,有一个用于类路径的选项卡。 You can use it learn what's already on the classpath, and to make additions if you want to.您可以使用它来了解类路径中已有的内容,并根据需要进行添加。

As mentioned above by @tschaible, the Eclipse working directory defaults to the root of the project.正如@tschaible 上面提到的,Eclipse 工作目录默认为项目的根目录。 Therefore, the program will look for the file from the root level.因此,程序将从根级别查找文件。

However, if you intend to run the program from the command line, it acts differently because the working directory defaults to the folder that you are running the java file from (aka the src folder).但是,如果您打算从命令行运行程序,它的行为会有所不同,因为工作目录默认为您从中运行 java 文件的文件夹(也称为 src 文件夹)。 Therefore, it is important to keep this discrepancy in mind when testing your java program from the command line.因此,在从命令行测试 Java 程序时,务必牢记这种差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我将一个 txt 文件读取到 Java,我想将其编码为边,其中每个边是一对节点(DFS) - I read a txt file to Java and I want to make it be encoded as edges where each edges is a pair of nodes(DFS) 如何将要由java.io.FileReader读取的文件放在哪里? - Where do I put files to be read by java.io.FileReader? 哪里可以在Eclipse中将.txt文件放在Java中? - Where to put a .txt file in java that it can be read using eclipse? 我在哪里放置文件(.mp3)以便读取? - Where do i put file(.mp3) in order for it to be read? 如何在 java 中将 a.txt 文件读入二维数组? - How do I read a .txt file into a 2D array in java? 如何从txt文件读取Java中的2个特定列? - How do I read 2 particular columns in Java from a txt file? 如何从 Java 中的 txt 文件中读取 2 个特定列? - How do I read 2 particular columns from a txt file in Java? 我想从txt文件读取并写入现有的JTable - I want to read from a txt file and write to an existing JTable 我需要在web应用程序中放置一个文件,以便我可以从servlet中读取它 - where do i need to put a file in web appliction so that I can read it from servlet 我想将多个字符串从Java中的.txt文件解析为变量。 最简单的方法是什么? - I want to parse multiple strings into variables from a .txt file in java. What's the easiest way to do it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM