简体   繁体   中英

Where to place an input .txt file for a jar of your netbeans application

The problem I'm having is rather trivial. I made a simple application in netbeans Java. The application on it's start loads an input file named try.txt for uploading some data. I have placed this file in the project folder and the following code works fine for file read operation

      FileInputStream fstream = new FileInputStream("try.txt");

When I debug the program the application works fine. But when I build a jar of my project and run the jar through the command prompt, I get the following error

java.io.FileNotFoundException: try.txt (The system cannot find the file specified)

The problem is resolved if I use the absolute path of try.txt like

C:\\Desktop\\try.txt

But this is not what I want since I'm supposed to distribute the jar to other users as well. I want to know that is there any default directory of my jar where I can put the file. Or Is there any way I can include the file to the jar so it is uploaded upon execution

Regards

您应该将文本文件放在包的顶部...即jar文件的根目录。

  1. Put the text file in whatever location in the Jar that best suits it. (The specifics of that come down to which IDE or build tool is being used.)
  2. Access the text file by URL obtained as mentioned in the embedded-resource info. page ..

URL urltoResource = this.getClass().getResource("/path/to/the.resource");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM