I have a program that will write to a config.properties file with information that is shown in a separate .java form that is shown once you press a button.
The code to write that I current have is:
finally {
prop.setProperty("row0", textToWrite.replace(" ", "_").replace(":", "."));
}
// save properties to project root folder
prop.store(output, null);
Although once you set it, you need to close the program to see the change in the .properties file and therefore on the table inside the program.
I've tried searching for code that will refresh the file although with no luck.
EDIT:
As asked, a simple example would be this:
Properties file at the start:
Row0=Item1&&Item123;
Row1=Item2&&Item234;
Row2=Item3&&Item345;
The program will edit these so it should turn out like this:
Row0=NewItem&&NewItem2;
Row1=Item1&&Item123;
Row2=Item2&&Item234;
Although it only updates the file after the program is closed.
I am writing to a .properties file because it is a small amount of information that will be able to be read/edited without too much effort.
Most likely the cause of your problem is that you read properties file only at the start of your application. As you are changing the property file through code and want the other part of the code to see the properties change, so you need to introduce a properties file reload logic.
One way to do this is to move your properties file read logic in a separate method and call this method every time you make a change to the property file.
Other way could be using listeners, observers, etc.
Related
I'm working on a game and I am at that part that I want to save the game progress into a text file (or maybe a properties file would be good too), but I'd like to save that file to a place that is not reachable for the players. I was thinking about saving it to a source folder inside the program, but I am not able to save or load a text file from there, only images. Could anybody suggest something how/where to save the game stats that players can not just go into the settings file and modify their score or level or something like that?
With the properties file my only problem is the saving, where I need an output stream or a writer to save it, what I', not able to get
Assuming your player's pc has a regular hard drive with regular standards then:
If the game is offline(and maybe later synced with a server) then the answer is NO; if your app can access the file so does the user; even if you encrypt the data written to the save file people can just reverse engineer your app and get the keys and algorithms and modify the file; to put it simply you can only complicate it, their access to save files is inevitable.
If your game is only possible to be played online then you can do sanity check for every action of the players and save the progress in an inaccessible by players manner;
About the read only, been asked before:
create a read-only file
For the second one, keep in mind that as you know the path to the file and the name of it, the user don't. Saving the file using "scary" name, in un-trivial path will protect the file from any changes for a while
From java I want to read .properties file and if property is present I want to re-set the property again. lets say .properties file has entry password=123 now I want to check If password entry is there replace 123 with 567. but need to keep all content as it is. how to do that? please help
One solution I can think of rightaway is
Load all the properties from the base .properties file.
Create a new temp properties file and loop through the entries from original file to write the same in to new file. In the loop you can change the value of any property while writing it to a new file.
After the loop delete the original file and rename the temp file as original file.
This approach has couple of limitations,
Do not use it if you are really concerned about the file last modified/created date, since we are creating a whole new file here.
If the original file is too big, this approach may cause memory problems.
Hope this helps!
I am using watch Service in order to track changes made to a file and print these changes in a text area. suppose only new data will be written(no delete and no replace) to this file is there a way to get this new data without the need to rewrite the over all file when changes done.
The Watch Service isn't able to tell what changed about a file. Assuming you have access to the old content of the file, you could use a diff library like java-diff-utils to find the changes. Example 1 on their wiki should match up with your issue.
Check out
I'm writing an application that is writing to a file. I'm wondering if it's possible to write to a folder, without specifying a file name. The way I have it set up now, my program will overwrite the previous saved file. I'm looking to have it add to the folder rather than replace.
Here's the line in question:
File testFile = new File("C:/TargetFolder/testFile.png");
There is no way to write to a file without assigning a file name. However, if you don't want to chose a file name you can have your system generate a random one. For example look at these options: What is the best way to generate a unique and short file name in Java.
Another option would be to add numbers to your file name like: test01.png, test02.png and so on.
If you don't want to do the unique file in the folder logic yourself and you don't care much about the exact name, you might use:
java.io.File.createTempFile("testFile", ".png", new File("C:/TargetFolder"));
I want to make a .properties file to use instead of numerous cookies. I have been trying to figure out how to do this by googleing, but maybe I'm just missing something or not looking for the right thing.
This will eventually be user based, so I wanted to know if I could get it to create a new file based on the user's login, but every beginners guide I find seems to say just make a text file by hand.
Could someone provide me with a page that explains how to do this well, or give me the basic code that I'll need to get me started.
And also what code I need to add new key,value's to the file and how to get the values from it.
Take a look at Properties.list. Using this method you can easily write properties into an OutputStream and with help of ByteArrayOutputStream you can convert theese properties into a string.
To read properties from a file use Properties.load.
Unfortunately there's no standard Java API to just add or remove a property from a file - you'll have first to load the whole file into a Properties instance and then save it back into the file after you made the required changes using Properties.setProperty.
java.util.Properties has load and store methods that will read and write the properties for you. In that case you just add to the Properties map and then store it to an OutputStream or a Writer.
For doing it user based, you would need a directory to store it in and then base the file name on the user name or id, but make sure it is safe for a file name.