1

I'm pretty new to Java and I'm facing a problem that I can't find a solution for. I have written a program that reads and writes to a txt file. When I run it in NetBeans it seems to be working fine, but when I try to run the JAR file, it seems as if the program can't access the txt file.

I created the file using the following code :

File data = new File("src/data.txt");

Could the reason why it won't work be in the creation of the file? And if not what could it be?

EDIT: The text file is being modified as program advances, if I use the inputstream I won't be able to write to or change the txt file, should I change the way on which I'm managing my data? ( txt files) or is there a way for the jar to still have access to the Txt file?

Shahar Kazaz
  • 37
  • 1
  • 6

2 Answers2

7

This statement

File data = new File("src/data.txt");

relys on the file being present in the file system. You need to read the file as a resource so that the JAR file can be run independently of the text file

InputStream input = getClass().getResourceAsStream("/path/to/data.txt");

Edit:

I suggest using the strategy of storing the the text file in as a "default", then when changes are required, write a new file under "user.home". Check the existence of the modified file before reading any data.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Just out of interest: are you able to explain the difference between your solution and @shamimz's answer below? I can't quite work it out. – devrobf Sep 24 '13 at 14:31
  • 1
    Also I just realised OP says that he is writing to the file as well; so surely his problem is more than just this? It sounds like he really does want to read/write to a file on disk, and his problem is the use of the `src` directory which probably doesn't exist... – devrobf Sep 24 '13 at 14:33
  • 1
    @jazzbassrob In relation to your first question there are some subtle differences between the 2 such as this approacj requires the absolute path to be specified, i.e. leading `/` needed. See this [answer](http://stackoverflow.com/questions/676250/different-ways-of-loading-a-file-as-an-inputstream) – Reimeus Sep 24 '13 at 14:36
  • @ShaharKazaz Only use the text file JAR file as a "default" input. See the update – Reimeus Sep 24 '13 at 16:25
5

Method 1: Where you are creating jar and running the program there is no such folder called "src" under the jar directory. You should create a folder called "src" in same directory of jar location. This will allow file write operation.

Method 2: If you want to keep the txt file inside the jar then you can use:

InputStream in = this.getClass().getClassLoader()
                            .getResourceAsStream("your package path/data.txt");

A complete example:

package com.file.loader;

public class Main {
    public static void main(String[] args) throws IOException {
    InputStream in = Main.class.getClassLoader().getResourceAsStream(
            "com/file/loader/data.txt");
    }
}
Robbie III
  • 15
  • 4
Shamim Ahmmed
  • 8,265
  • 6
  • 25
  • 36