1

I would like to write to a .txt file that is inside a package. I can get it to read from the exact location the .txt file is stored but not from inside the package. I'm assuming it is using class loaders but I cannot seem to get it to work. Here is what I have so far.

public void writeFile(String fileLocation) {
    Writer output = null;
    File file = new File(fileLocation);
    try {
        output = new BufferedWriter(new FileWriter(file));  
        output.append("WRITING TEST");

     output.close();
    } catch (IOException ex) {
     System.out.println("Couldn't write to file.");

    }
}

Then I use this in another class to write.

WriteFile writeFile = new WriteFile();
writeFile.writeFile("src/com/game/scores.txt");

I understand that if using class loaders you remove "src/" because that will no longer exist when the program is compiled in a .jar.

user3080274
  • 107
  • 1
  • 2
  • 8
  • 1
    I'm not sure what you are asking. Are you trying to write to a resource? – mikea Jan 15 '14 at 17:39
  • I am not sure that there is a generic way to do that for files within the Java package hierarchy. What if the file is within a JAR file? – thkala Jan 15 '14 at 17:40
  • The file will be inside the .jar file. I would like to write to it to update the scores in my game as the user goes through the levels. – user3080274 Jan 15 '14 at 17:40
  • You can't write to files inside jars http://stackoverflow.com/questions/14403745/write-to-file-method-in-jar – Typo Jan 15 '14 at 17:41
  • Uh, you are going to modify the JAR file? Just for keeping scores in a game? I cannot think of a way to do that, short of using ZipFile and treating the JAR as an archive... – thkala Jan 15 '14 at 17:42
  • Sorry! No wonder I couldn't find anything online about how to do it. – user3080274 Jan 15 '14 at 17:42
  • 1
    @user3080274: it's also not a very good idea. Code and variable data should not be forced together like this... – thkala Jan 15 '14 at 17:44
  • So if there is no way of writing to a txt file in a .jar, what is the best way to store the scores and access and update them? – user3080274 Jan 15 '14 at 17:44
  • @user3080274: For each operating system there is a convention on where user data like this is stored. Are those high scores supposed to be shared among users? – thkala Jan 15 '14 at 17:46
  • The game just needs to store whether the user gets bronze, silver, or gold. So there will just be scores stored for one user and that is all – user3080274 Jan 15 '14 at 17:48
  • Could I instead have a folder, which contains game.jar and scores.txt and then access the scores.txt to read it and write to it like that? – user3080274 Jan 15 '14 at 17:54

5 Answers5

1

You could use a class in that package to give you the location of the folder. Try something like

public URL getPackageLocation() {
    return getClass().getResource(".");
}

This should give you the location of the folder from which this method is being called from.

J Code
  • 454
  • 1
  • 7
  • 20
  • You can get the location of a file within a .jar file this way (I've used it many times to call images and what not). However, I do not think you can edit a .txt file within a jar. – J Code Jan 15 '14 at 17:57
1

It is not possible to write or update a file inside jar. Since jar itself is a file.

Please refer this link.

Write To File Method In JAR

Community
  • 1
  • 1
ashokramcse
  • 2,841
  • 2
  • 19
  • 41
0

From the comments you already know that you cann't write to a file, which resides in a JAR file. At best what you can do, is creating your file, relative to the path where the JAR is located like bellow:

 mylocation
          |-- my-jar.jar
          |-- com
                |--game
                      |--myfile.txt
Sazzadur Rahaman
  • 6,938
  • 1
  • 30
  • 52
0

I would like to write to it to update the scores in my game as the user goes through the levels.

While it might be possible to write to the JAR file, I don't recommend it for this use case. Just write it somewhere at:

Path userdir = Paths.get(System.getProperty("user.home"), ".myApp", "<my app version>");
Community
  • 1
  • 1
Puce
  • 37,247
  • 13
  • 80
  • 152
0

You can't write into Jar file. Writing into Jar file is not recommendable. You can write outside the jar file.

Please refer this and this stack overflow question for more details.

Community
  • 1
  • 1
Dhasneem
  • 4,037
  • 4
  • 33
  • 47