how to import a text file in netbeans? [duplicate]

三世轮回 提交于 2021-01-29 19:09:28

问题


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 work File 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);

回答1:


Make sure your text file is in the classpath




回答2:


You have a number of choices.

1. Relative Path

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 "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.

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

2. Absolute Path

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.

2.1 A "well known" location

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

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

3 Embedded resource

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.

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

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)



来源:https://stackoverflow.com/questions/58963290/how-to-import-a-text-file-in-netbeans

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!