本文介绍了如何在netbeans中导入文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有老师的作业,我将串行程序与 OMP 并行.这是 Barnes-Hut 程序,也是我第一次使用 netbeans.我有一个带有数字的文本文件,我必须将它导入到项目中,以便它可以使用文本文件中的值.如何将 example.txt 导入 netbeans?我试过这个,但它不起作用File myFile = new File("example.txt");它还包含一个扫描仪

I have an assignment from my teacher i have make a serial program into a parallel with OMP.It's the Barnes-Hut one and its the first time i'm using netbeans.I have a text file with someone numbers and i gotta import it into the project so it can use the values the text file has.How can i import the example.txt into netbeans?I've tried this but it doesn't workFile myFile = new File("example.txt");It also contains a scanner

public static void main(String[] args) {

    // for reading from stdin
    Scanner console = new Scanner(System.in);

推荐答案

您有多种选择.

您可以使用相对路径.这意味着你把你目前的工作"位置并从那里查找文件,这基本上就是您现在要做的.

You can use a relative path. This means you take your current "working" location and look for the file from there, this is basically what you're doing now.

这个问题是,工作"位置发生变化,并且基于执行程序的位置.请记住,课程的位置与工作"位置不同.位置.

The problem with this is, the "working" location changes, and is based on the location from which the program is executed. Remember, the location of the class is not the same as the "working" location.

Netbeans默认"工作"location 通常是项目的根目录.您可以通过多种方式配置 Netbeans 以使用特定的工作"程序.位置,但您需要考虑不再在 Netbeans 中运行时会发生什么.

Netbeans "default" "working" location is usually the root directory of the project. There are ways you can configure Netbeans to use a specific "working" location, but you need to consider what happens when you're no longer running in Netbeans.

所以简单"解决方案是将文件拖放到 Netbeans 项目目录中.稍微先进的解决方案是配置项目的工作"程序.目录到包含文件的位置

So a "simple" solution would be too drop the file into the Netbeans project directory. A slightly more advance solution would be to configure the project's "working" directory to a location containing the file

这是当您使用从根位置到文件的完全限定路径时(即 C:\some\place).

This is when you use a fully qualified path from the a root location to the file (ie C:\some\place).

问题在于,从一个计算机系统到另一个计算机系统的路径很少相同,您需要记住每次都将文件放入此位置.

The problem with this is, rarely are this paths the same from one computer system to another and you need to remember to place the file into this location each time.

绝对路径解决方案的一种形式是将文件放入众所周知"的目录中.位置,通常基于操作系统.

A form of the absolute path solution is to place the file into a "well known" location, typically based on the OS.

例如,在 Windows 上,您通常会使用类似 {user.home}\AppData\Remote\{您的应用程序名称} 之类的内容.

For example, on Windows, you would typically use something like {user.home}\AppData\Remote\{name of your application}.

这将允许您将文件放入本质上是静态"的文件中.定位并在需要时加载它.

This would allow you to place the file in, what is essentially a "static" location and load it when ever you needed to.

但是,这可能已经满足您的需求了

But, this is probably over kill for your needs

更常见的解决方案是嵌入"您的应用程序的资源.本质上,这是将文件放置在应用程序类路径中,这允许 Java 运行时定位它.

A more common solution is to "embed" the resource with your application. Essentially, this is placing the file within the applications class path, which allows the Java runtime to locate it.

在 Netbeans 中,这意味着将文件放在 src 目录中,最好放在子目录中,例如 resources.

In Netbeans, this means putting the file within the src directory, preferably in a subdirectory, like resources for example.

然后,当您想读取文件时,您需要使用 Class#getResource(String)Class#getResourceAsStream(String) 获得访问它.

Then, when you want to read the file, you'd need to use Class#getResource(String) or Class#getResourceAsStream(String) to gain access to it.

因此,根据可用信息和上面的示例,您可能会执行以下操作...

So, based on the available information, and the example above, you might do something like...

try (InputStream is = YourClass.getResourceAsStream("/resources/example.txt")) {
    // Read the file...
} catch (IOException exp) {
    // Handle the exception
}

请记住,这会将资源呈现为只读"(至少对于这个简单示例的上下文)

Just remember, this renders the resource "read-only" (at least for context of this simple example)

这篇关于如何在netbeans中导入文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 17:33
查看更多