0

I am working on a JavaFX project. There is a 'settings' module that saves the user's input into a txt file and reads it every time the app launches. But I find that I can't read it after I packaged it into .jar file, so I changed the reading func to 'Inputstream':

    //File myFile = new File(fileName);
    InputStream myFile = FileIO.class.getClassLoader().getResourceAsStream(fileName);
    Scanner scanner = new Scanner(myFile);

and I can't write in the txt as well.

I thought I should put the txt outside the .jar file, but then how can I package it to a .exe file?

I am expecting that by some means I can implement my function and get an .exe file

  • *"There is a 'settings' module that saves the user's input into a txt file"* - Exception then is, the file should be stored externally from the jar/exe (good luck writing to a exe file without have your anti virus go nuts). On most systems, the "program" area is "sandboxed" or has restricted access, so you probably won't be able to write there anyway. – MadProgrammer Jan 12 '23 at 03:40
  • Most platforms have conventions for storing "user data" and you should consider making use of them based on your target platform (which one can assume is Windows), [for example](https://stackoverflow.com/questions/53405312/java-reading-the-properties-file-outside-of-jar-file/53405540#53405540) and [for example](https://stackoverflow.com/questions/53974191/get-current-path-of-executed-file/53982349#53982349) – MadProgrammer Jan 12 '23 at 03:40
  • 1
    [Please do not upload images of code/data/errors.](//meta.stackoverflow.com/q/285551) – Stephen C Jan 12 '23 at 03:42
  • Welcome! Can you please read about the [problems with images of text](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors/285557#285557) and then edit to convert your images of text into actual text? Likely useful: [/help/formatting](https://stackoverflow.com/help/formatting) – Ragnar Jan 12 '23 at 05:21
  • The marked duplicates are for how to handle read/write application configuration and data files, not specifically about how to package, which is a wider, more complicated topic. You may want to study a [jpackage tutorial](https://docs.oracle.com/en/java/javase/19/jpackage/index.html) and see if that is something you wish to use for packaging your distribution. – jewelsea Jan 12 '23 at 06:50

0 Answers0