updating csv file in resources folder in maven java project - java

A csv file in /resources in my java maven application needs to be read as well as written.
Accessing the file using getClass().getClassLoader().getResource(/myFile.csv) in ResourceLoader class, i am able to write and read the file but the issue is the data is not appearing in the application after the restart of my system or after some time duration.
I tried using getResourceAsStream() by getting input stream ,but could write the file with it.
What might be the reason to it? Is there anything am doing wrong?

Related

Storing data in text files with Java/Maven

I wrote a little Java app for analyzing .csv files. Now I want to keep reading from and writing to a .txt file, which acts similar to a mini-database. For this purpose I simply added the .txt in my project and used the Files.readString(Path) and Files.write(Path path, byte[] bytes) methods.
When I do this in IntelliJ I have no problems but as soon as I build/export the file with Maven and started with the created launcher the app didn't work because the project structure / file organization isn't the same anymore.
I also tried to just add the .txt file to the exported folder afterwards but even then I couldn't manage to implement a relative path to the file.
I'm still relatively new to programming and it's just a small app so I don't think mySQL would fit my needs. I've also read that I could store the data in a property file but I don't know if that would be the right way to archive what I want. Certainly it must be possible to somehow keep a .txt for reading and writing in your exported project. Does someone have an idea?
If you use a ยด*.txt` file for storing, that file cannot be inside the jar because the jar cannot change its contents while running.
You need to put the file somewhere else, either at some dedicated location (let the user choose/configure one), or next to the jar. To figure out your own execution path, you can use
How to get the path of a running JAR file?
Maven is one tricky tool. You need to go to the pom file and add the resource.
Unable to add resources to final jar for maven project in Intellij.
-I hope this helps
Trader

How to make Jar exe file which read/write data from text file

How to convert text file into jar file.Actually In my java project I read and write data from a text file but when i convert my project into jar executable file then reading and writing from/into text file not working what should I do to make this work please help me.
A jar file is immutable, for security reasons. You cannot save data back to a file stored in a jar file.
Data saved from a Java program must be stored outside the classpath. Where that is, is up to you.
If you program must ship with a sample/default file, to get started, you code can try reading the file from the external location. If not found there, read from the sample/default file in the jar file. After updating content in memory, save to external location, where it will be read from on next execution.
You can make use of Classloader to read the file present in jar, but ofcourse you will not be able to write to it.
To be able to read and write both file should be present in filesystem, you may want to have a look at Read and Write to Java file via Resource

How to hot deploy newly created files to Apache Tomcat in Eclipse IDE?

I have a peculiar problem. I am extracting the data from a database in the form of .csv file and then passing this file to d3.js for visualization.
The problem is, d3.js doesn't allow file:// based protocol (so I cannot give path to .csv file directly). The .csv file needs to be located on the server. But this file is generated on the run time.
I tried dumping the file in the same project folder, but as expected, the Eclipse doesn't take the updated file until next clean and build action.
Any idea how to work around this problem?
You need to place the file in the place where your application is deployed on Tomcat (e.g.: Tomact7\webapps\myProject...), not in Eclipse Workspace location, this way your js can access the File either by its URL or relative path.

Writing to a properties file in a JAR

I'm create an application that is going to be run on Windows, Mac OX and Linux. I have a properties file storing user settings which need to be read and changed on the fly.
A JAR file is compressed and is not meant to be changed on the fly which means I should write to an external file.
I'm using :
new FileInputStream("database")
new FileOutputStream("database")
How do I create a URL which is going to be consistent throughout all three operating systems. The JAR is run as an application on the desktop and I would like the file to be stored somewhere discrete.
I've tried reading from a local file in the same package as this class :
this.getClass().getResourceAsStream("database")
This works however I can't seem to create an output stream to write to the same file but this would be breaking the rule of changing a JAR file on the fly.
There are plenty of good reasons for which you should not do this, off the top of my head:
GetResourceAsStream does not necessarily get the file from the JAR itself. You coincidentally got it from there because the Jar was the first or the only element in the class path.
Writing a file in your own Jar could break the JAR signature if you are going to sign it.
The database could need to be backed up; in this case you may want to back it up separate from the code (the code could be upgraded when restoring the database).
Hope this helps.

Can you store data inside a .jar?

I'm learning java and am currently trying to develop a simple application. My question is can you store data about settings, etc in a text file internal to a .jar? If so how would you go about accessing this within java? Sorry if this is a really stupid idea.
InputStream is = this.getClass().getResourceAsStream("/data/file.txt");
The resources you are getting need to be on the classpath
Yes you can, and it's not a stupid question we all need to start somewhere.
There are two parts to your question:
Adding a data/text file to a .jar - (using ant to jar it:) add "fileset dir=..." to the jar target, where dir is set equal to the directory that has the data/text file. Refer to How can I include data text files in a jar using Ant?
Accessing that data/text file from within the java code - you need to use a ClassLoader and getResourceAsStream. Refer to Loading files in JAR in Tomcat using getResourceAsStream
Also, please take a look at https://github.com/gitjonathan/turbo-sansa, I have a working version up on it.
Can you store data inside a .jar?
Read-only data can be stored inside a JAR file. You can read such data using getResourceAsStream(...) if the JAR is on the classpath, or by using the standard JAR file API class if it is not on tle classpath.
But storing update-able data in a JAR file is problematic:
In a lot of circumstances it is impossible; e.g. because the JAR file is read-only or was downloaded on the fly.
In all other cases it would be very awkward, because the standard JAR file API class does not support update in place. (You would need to create a new ZIP file, copy across the old content apart from the file you are updating, add that file, and then rename the resulting file.)

Categories

Resources