简体   繁体   English

在哪里放置一罐netbeans应用程序的输入.txt文件

[英]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. 我在netbeans Java中做了一个简单的应用程序。 The application on it's start loads an input file named try.txt for uploading some data. 开始的应用程序将加载一个名为try.txt的输入文件,用于上传一些数据。 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 但是,当我构建项目的jar并通过命令提示符运行jar时,出现以下错误

java.io.FileNotFoundException: try.txt (The system cannot find the file specified) java.io.FileNotFoundException:try.txt(系统找不到指定的文件)

The problem is resolved if I use the absolute path of try.txt like 如果我使用try.txt的绝对路径,就可以解决问题

C:\\Desktop\\try.txt C:\\ Desktop \\ try.txt

But this is not what I want since I'm supposed to distribute the jar to other users as well. 但这不是我想要的,因为我也应该将jar分发给其他用户。 I want to know that is there any default directory of my jar where I can put the file. 我想知道那是我的jar的任何默认目录,我可以在其中放置文件。 Or Is there any way I can include the file to the jar so it is uploaded upon execution 或者有什么办法可以将文件包含到jar中,以便在执行时将其上传

Regards 问候

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

  1. Put the text file in whatever location in the Jar that best suits it. 将文本文件放在最适合它的Jar中的任何位置。 (The specifics of that come down to which IDE or build tool is being used.) (具体细节取决于使用的IDE或构建工具。)
  2. Access the text file by URL obtained as mentioned in the embedded-resource info. 通过按嵌入式资源信息中所述获得的URL访问文本文件 page .. ..

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

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

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