Java IO Error when writing but not when reading

So the problem is I try to read the configuration file that is packed inside the .jar which works fine but then when it comes to writing to the file the file can not be found yet they are using the same

getClass().getResource(Path);

it only seems to work with the input stream.

Here is all the code of my IO class.

package com;

public class IO {

public boolean CheckStream () {
    String LineRead;

    try {

        InputStream IS = getClass().getResourceAsStream("Config.txt");

        InputStreamReader ISR = new InputStreamReader (IS,Charset.forName("UTf-8"));
        BufferedReader BR = new BufferedReader(ISR);

        if ((LineRead = BR.readLine()) != null) {
            BR.close();
            return true;
        }

        IS.close();

    } catch (IOException e) {
        e.printStackTrace();
    }

    return false;
}

public void Write (String Path, String [] ThingsToWrite) throws FileNotFoundException {
    OutputStream Out = new FileOutputStream (getClass().getResource(Path).getPath());
    PrintStream PS = new PrintStream (Out);

    for (int i = 0; i < ThingsToWrite.length; i ++) {
        PS.print(ThingsToWrite[i]);
    }

    PS.close();
}

}

Any Help is greatly appreciated thanks.

Jon Skeet
people
quotationmark

You can't just write to a file within a jar file - it's not a file in the regular sense.

While you could unpack the whole jar file, write the new content, then pack it up again, it would be better to redesign so that you don't need to update the jar file.

For example, you might have a regular local file which is used if it's present, but then fall back to reading from the jar file otherwise. Then you only need to write to the local file.

people

See more on this question at Stackoverflow