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!
Related
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.
I've been googling and I've not found one example for this.
I am able to extract the contents for a DOCX file but so far no clue how to get the contents of an EXCEL file.
I know you use
SpreadsheetMLPackage spreadsheetMLPackage = SpreadsheetMLPackage.load(file);
to load the file, but I don't know how to proceed from here. I've check whatever methods SpreadsheetMLPackage has but nothing has gotten me the contents.
First you need to understand the structure of a xlsx file.
Unzip one, or run it through the docx4j webapp.
For how the parts relate to one another, see:
http://openxmldeveloper.org/blog/b/openxmldeveloper/archive/2007/08/13/1970.aspx
I guess the key method you'll want is getWorksheet
But first you'll need to get the WorkbookPart; do that with spreadsheetMLPackage.getWorkbookPart()
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.
Ok so I know the value of the line, I dont have the line number, how would I edit only 1 line?
Its a config file, i.e
x=y
I want a command to edit x=y to x=y,z.
or even x=z.
In Java you can use `Properties class:
app.config file:
x=y
java:
public void writeConfig() throws Exception {
Properties tempProp = new Properties();
tempProp.load(new FileInputStream("app.config"));
tempProp.setProperty("x", "y,z");
tempProp.store(new FileOutputStream("app.config"), null);
}
If you are using that configuration format, you might want to use
java.util.Properties
component to read/write on that file.
But if you just want to edit it by hand, you can just read the file line by line and match the variable you want to change.
One way to do it is to:
Read the file into memory; e.g. as an array of Strings representing the lines of the file.
Locate the String/line you want to change.
Use a regex (or whatever) to modify the String/line
Write a new version of the file from the in memory version.
There are many variations on this. You also need to take care when you write the new version of the file to guard against losing everything if something goes wrong during the write. (Typically you write the new version to a temporary file, rename the old version out of the way (e.g. as a backup) and rename the new version in place of the old one.)
Unfortunately, there is no way to add or remove characters in the middle of a regular text file without rewriting a large part of the file. This "problem" is not specific to Java. It is fundamental to the way that text files are modelled / represented on most mainstream operating systems.
Unless the new line has the exact same length as the old one, your best bet is to
Open a temporary output file
Read the config file, line by line
Search for your key
If you can't find it, just write the line you just read to the output file
If you can find it, write the new value to the temporary file instead
Until you hit EOF
Delete old file
Rename new file to the old file
IF your config file is small, you can also do the whole parsing/modification step in memory and then write the final result back to the config file, that way you skip the temporary file (although a temporary file is a good way to prevent corruption if something breaks while you write the file).
If this is not what you're looking for, you should edit your question to be a lot more clear. I'm just guessing what you're asking for.
If your data is all key and value pairs, for example ...
key1=value1
key2=value2
... then load them into a Properties object. Off the top of my head, you'll need a FileInputStream to load the properties, modify with myProperties.put(key, value) and then save the properties with the use of a FileOutputStream.
Hope this helps!
rh