Java 如何在 Netbeans 中包含文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25856945/
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 01:19:34  来源:igfitidea点击:

How To Include Text File With Netbeans

javanetbeans

提问by user3624385

I'm very new to Java. I want to read from a file, but I keep getting my "file not found" exception message. I learned that my .txt file had to be in the same directory as my Java program. On netbeans, how do I place my .txt file in the same directory as my Java program. Sorry this question is so basic, I'm in my first few weeks of java

我对Java很陌生。我想从文件中读取,但我不断收到“找不到文件”异常消息。我了解到我的 .txt 文件必须与我的 Java 程序位于同一目录中。在 netbeans 上,如何将我的 .txt 文件放在与我的 Java 程序相同的目录中。抱歉,这个问题太基础了,我刚接触 Java 的前几周

回答by Kick Buttowski

There are two ways to accomplish what you are looking for

有两种方法可以完成您正在寻找的内容

  1. Absolute Path

    like "C:\\Users\\kick\\Documents\\NetBeansProjects\\ReadFile\\newfile.txt"

  1. 绝对路径

    喜欢 "C:\\Users\\kick\\Documents\\NetBeansProjects\\ReadFile\\newfile.txt"

For the file separatorread this:

对于文件分隔符,请阅读:

With the Java libraries for dealing with files, you can safely use / (slash, not backslash) on all platforms. The library code handles translating things into platform-specific paths internally.

You might want to use File.separator in UI, however, because it's best to show people what will make sense in their OS, rather than what makes sense to Java.

Update: I have not been able, in five minutes of searching, to find the "you can always use a slash" behavior documented. Now, I'm sure I've seen it documented, but in the absense of finding an official reference (because my memory isn't perfect), I'd stick with using File.separator because you know that will work.

使用用于处理文件的 Java 库,您可以在所有平台上安全地使用 /(斜杠,而不是反斜杠)。库代码在内部处理将事物转换为特定于平台的路径。

但是,您可能希望在 UI 中使用 File.separator,因为最好向人们展示什么对他们的操作系统有意义,而不是什么对 Java 有意义。

更新:在五分钟的搜索中,我无法找到记录的“您始终可以使用斜杠”行为。现在,我确定我已经看到它被记录在案,但在没有找到官方参考资料的情况下(因为我的记忆力并不完美),我会坚持使用 File.separator,因为你知道这会起作用。

Source of the quoted answer

引用答案的来源

  1. Relative Path

    like "newfile.txt"

  1. 相对路径

    喜欢 "newfile.txt"

Note: your file has to be located in project folder which is current working directory. In better sense, the folder that contains all of your related content

注意:您的文件必须位于当前工作目录的项目文件夹中。更确切地说,包含所有相关内容的文件夹

For example, if the name of your project is ReadFile, the file hast to be in folder that has the same name which ReadFile.

例如,如果您的项目名称为 ReadFile,则该文件必须位于与 ReadFile 同名的文件夹中。

Note: Personally, I manually create a txt file for my goal and reside it in main folder.

注意:就我个人而言,我为我的目标手动创建了一个 txt 文件并将其驻留在主文件夹中。

回答by fabian

  1. Go to the FilesTab (next to Projectsand Services)
  2. In the context menu of the root folder of your project use New ><approquiate type>to create a new file
  1. 转到“文件”选项卡(在“项目服务”旁边)
  2. 在项目根文件夹的上下文菜单中,使用New ><approquiate type>创建一个新文件

Of course you can also use other means to move your file there, e.g. the file explorer of your OS.

当然,您也可以使用其他方式将文件移动到那里,例如您操作系统的文件浏览器。

However I'd recommend using the "working directory" option of the project configuration (you can reach this using the combobox at the top of the window) to use a directory you can reach easier using your OS file explorer.

但是,我建议使用项目配置的“工作目录”选项(您可以使用窗口顶部的组合框访问此选项)来使用您可以使用操作系统文件资源管理器更轻松访问的目录。

回答by Usman Yaqoob

Create the text file and you have to put the file in the folder of project like in my case:

创建文本文件,你必须把文件放在项目的文件夹中,就像我的例子:

C:\Users\UsmanYaqoob\Documents\NetBeansProjects\BookQuestions

C:\Users\UsmanYaqoob\Documents\NetBeansProjects\BookQuestions

BookQuestions is my project

BookQuestions 是我的项目