How Can an App Use Files Inside the Jar For Read and Write

How can an app use files inside the JAR for read and write?

I need to store data into files inside .jar file and read it again

No you don't.

Instead store the 'default' file inside the Jar. If it is changed, store the altered file in another place. One common place is a sub-directory of user.home. When checking for the file, first check the existence of an altered file on the file system, and if it does not exist, load the default file.


Note that it is generally better to describe the goal, rather than the strategy. 'Store changed file in Jar' is a strategy, whereas 'Save preferences between runs' might be the goal.

Related: What is the XY problem?

Is there a way to write to a file that is inside a JAR?

A JAR file is a zip folder, so it can be extracted and worked upon using the java.util.zip package.

However, a running jar may be write protected, preventing content modification.

It is better suited to store configuration in a corresponding location in the operating system. On windows, the common location is the windows registry.

There is a Java library for editing the registry as shown in this answer .

Library Source: https://github.com/apache/npanday/tree/trunk/components/dotnet-registry/src/main/java/npanday/registry

To store config on other operating systems, you can use a file inside a folder named after your application.

  • Common Location on Linux: '~/.config' folder
  • Common location on Mac: '~/Library/Application Support' folder.

Reading/Writing to Properties Files inside the jar file

Your code writes to a local file mainProperties.properties the properties.

After you run your part of code, there you will find that a file mainProperties.properties has been created locally.

FileOutputStream fos = new FileOutputStream("mainProperties.properties");

Could order not to confuse the two files you specify the local file to another name. e.g. mainAppProp.properties .

  • Read the complete contents of the resource mainProperties.properties.
  • Write all the necessary properties to the local file mainAppProp.properties.

 FileOutputStream fos = new FileOutputStream("mainAppProp.properties");

switch if file exists to your local file , if not create the file mainAppProp.properties and write all properties to it.

  • Test if file mainAppProp.properties exists locally.
  • Read the properties into a new "probs" variable.
  • Use only this file from now on.

Under no circumstances you can write the properties back into the .jar file.

Test it like

    [...]
if (propKey == null) {
// Key is not present so enter the key into the properties file
mainFile.setProperty(confirmKey, "testtest");


[...]
Reader reader = null;
try
{
reader = new FileReader( "mainAppProp.properties" );
Properties prop2 = new Properties();
prop2.load( reader );
prop2.list( System.out );
}
catch ( IOException e )
{
e.printStackTrace();
}
finally
{
if (reader != null) {
reader.close();
}
}
}
[...]
}

output : with prop2.list( System.out );

-- listing properties --

defaultXMLPath2=testtest

content of the file mainAppProp.properties

#testtest3

#Mon Jul 14 14:33:20 BRT 2014

defaultXMLPath2=testtest

How to Create a Jar that uses a file to save app information to

Jar files are read only, that means you can't save informations inside the jar. You can save files inside an read them but you can't edit them.

In your case there are more or less two options:

  1. Save the *.json file in e.g the OS Temp path or somewhere else where you can read and write them.
  2. Handle like zip - https://stackoverflow.com/a/4056713/8087490 -

    1. Locate the JAR file and open as a ZIP archive reader.
    2. Create a ZIP archive writer to write a new version of JAR file.
    3. Write the application's files to the writer.
    4. Write all resources from the ZIP reader to the writer, excluding old versions of the applications files.
    5. Close the reader and writer.
    6. Rename the new version of the JAR to replace the old one.

Read/Write to a .txt file from JAR

To read from the Jar file: How to read a file from jar in Java?

The file is an archive file. It is a zip file with a .jar extension. You shouldn't be writing to it. If the jar file has been signed (security projected) you cannot write to it. Changing a single bit in the file will invalidate it.

What you should do is store a default file in Jar and load that to the "user.home" folder if it is not already there.

Java - Writing to txt in a JAR file

When you are trying to write a text file inside a jar, java is not recognizing absolute path to text file.

it will be something like

C:User/adom/documents/jarName.jar!/fileName.txt 

This is not a absolute path so file could not be written. Try writing file externally.

How can my Java program store files inside of its .jar file?

Yes you can do this.

Non-code resources in a JAR file on the classpath can be accessed using Class.getResourceAsStream(String). Applications routinely do this, for example, to embed internationalized messages as resource bundles.

To get your file into the JAR file (at project build time!), just copy it into the appropriate place in the input directory tree before you run the jar command. Build tools such as Maven, Gradle, etc can automate that for you.



Is there a way to add files to the archive within the app?

In theory, your application could store files inside its own JAR file, under certain circumstances:

  • The JAR has to be a file in the local file system; i.e. not a JAR that was fetched from a remote server.
  • The application has to have write access to the JAR file and its parent directory.
  • The application must not need to read back the file it wrote to the JAR in the current classloader; i.e. without exiting and restarting.
  • The JAR must not need to be be signed.

The procedure would be:

  1. Locate the JAR file and open as a ZIP archive reader.
  2. Create a ZIP archive writer to write a new version of JAR file.
  3. Write the application's files to the writer.
  4. Write all resources from the ZIP reader to the writer, excluding old versions of the applications files.
  5. Close the reader and writer.
  6. Rename the new version of the JAR to replace the old one.

The last step might not work if the initial JAR is locked by the JVM / OS. In that case, you need do the renaming in a wrapper script.

However, I think that most people would agree that this is a BAD IDEA. It is simpler and more robust to just write regular files.



Related Topics



Leave a reply



Submit