1

im a newbye and this is my first post. Ive made a game aplication on eclipse that works perfectly. It uses a few .txt files for scores and player options.

The problem is when i try to export it as runnable jar file, well that's the problem, it makes a jar file and makes it impossible for me to write on any of the .txt files i have. I know this because ive tried, well not hundreds but getting close, of solutions and some of which allowed me to read the files but still i cant write on them. I realize this is the normal functioning of a jar file, so my questions are:

How can i have/make an external folder to the jar file in the same directory containing all my txt files? So that it can read and write in those files, and, what methods should i use in my existing code?

Im only showing how i read/write one of those files, but its the same for every other file and im also showing some of the comment on other past solutions:

    private final String cubesScore =  "resF\\score.txt";
    //private final String cubesScore =  "/score.txt";
    //private final String cubesScore =  "//resF//score.txt";

    try{
        /*
        try{
            File file = new File(cubesScore);
            //FileReader reader = new FileReader(new File(new File("."), "score.txt"));

            if(file.createNewFile()){
                System.out.println("File created successfully!");
            } else{
                System.out.println("File already exists.");
            }

        } catch(IOException e){
            System.out.println("An error occurred on score.txt!!");
        }
        */
        Scanner scanner = new Scanner(new File(cubesScore));

        //InputStream source = this.getClass().getResourceAsStream(cubesScore);
        //Scanner scanner = new Scanner(source);

        /*
        InputStream inputStream = this.getClass().getResourceAsStream(cubesScore);
        InputStreamReader inputReader = new InputStreamReader(inputStream);
        BufferedReader reader = new BufferedReader(inputReader);
        */
        int value;
        int i = 0;
        while(scanner.hasNext()){
            value = scanner.nextInt();
            if(value >= 0){
                mostCubesDestroyed[i] = value;
                System.out.println(value);
            }
            else 
                System.out.println("File corrupted");
            ++i;
        }
        scanner.close();
    } catch (NullPointerException | FileNotFoundException e) {
        System.out.println("File Not Found/Null");
    }

write:

        try {
             PrintWriter out = new PrintWriter(cubesScore);
             //OutputStream out = this.getClass().getResourceAsStream(cubesScore);
             //out = new PrintWriter(out);
             //BufferedWriter out = new BufferedWriter(new FileWriter(cubesScore));
             //out.write(mostCubesDestroyed[0]);
             //out.newLine();
             out.println(mostCubesDestroyed[0]);
             System.out.println(mostCubesDestroyed[0]+" Cubes GameMode 0");
             //out.write(mostCubesDestroyed[1]);
             //out.newLine();
             out.println(mostCubesDestroyed[1]);
             System.out.println(mostCubesDestroyed[1]+" Cubes GameMode 1");
             //out.write(mostCubesDestroyed[2]);
             //out.newLine();
             out.println(mostCubesDestroyed[2]);
             System.out.println(mostCubesDestroyed[2]+" Cubes GameMode 2");
             //out.write(mostCubesDestroyed[3]);
             //out.newLine();
             out.println(mostCubesDestroyed[3]);
             System.out.println(mostCubesDestroyed[3]+" Total Cubes Destroyed");
             out.close();
          } catch (IOException e) {
                 System.out.println("Error, try again!!");
      }

i realize keeping the commented code makes it slightly harder to read but still i wanted to show you some things ive tried...

ive also tried to create the file the first time the app runs but to no success:

      try{
            File file = new File(cubesScore);
            //FileReader reader = new FileReader(new File(new File("."), "score.txt"));

            if(file.createNewFile()){
                System.out.println("File created successfully!");
            } else{
                System.out.println("File already exists.");
            }

        } catch(IOException e){
            System.out.println("An error occurred on score.txt!!");
        }

so thats my problem, if anyone been here before and somehow managed to find a solution or you simply know how to produce the desired result, which is to read/write on a .txt file external to the jar(because internal leads to all sorts of issues) then pls tell me what i should do, ive seen more then a hundred post and videos and still couldnt find the desired solution.

Edit: This has been resolved below, turns out i needed a . on "/score.txt" well a . in all files.

ArcNub Gamer
  • 68
  • 10
  • A .jar file in the classpath is read-only. (Some systems might allow you to write to it, but since that is not guaranteed, you should treat it as read-only.) Write to a file in a reliably available cross-platform location instead, like `System.getProperty("user.home")`. – VGR Jan 24 '17 at 21:47
  • Ty for your help, turns out the solution was even simpler then i thought ... – ArcNub Gamer Jan 25 '17 at 13:57

2 Answers2

0

Did you try this?: private final String CUBES_SCORE = "./score.txt"; if you want it in a subdirectory, you have to create the subdirectory also. Also, take a look at this: How do I create a file and write to it in Java?

Community
  • 1
  • 1
Brian Pipa
  • 808
  • 9
  • 23
  • Ty alot, i cant believe ive been through so much time and struggle and in the end, a single . before "/score.txt" was the missing link -.-, already tested and worked, created the files, didnt read first time cuz there was no information, wrote information on them to 0(starting values) on all lines, then read and worked. Now i have another issue but its not related with this issue, first ill try to solv and if not ill create a new post, so this one is resolved. i dont know how to make the post end though. – ArcNub Gamer Jan 25 '17 at 13:54
0

I think there is some problem in your path private final String cubesScore = "..\resF\score.txt";

hope it helps :)

Arun Yadav
  • 203
  • 3
  • 9