I am using a property file for some configurable values. But I do not know why I can read the property value but when I want to update the existing or write a new key/value pair it is not working
Properties props = new Properties();
try
{
String key = "maxHolidays";
File file = ResourceUtils.getFile("classpath:config.properties");
InputStream in = new FileInputStream(file);
props.load(in);
String maxHolidays = (String) props.get(key);
System.out.println(maxHolidays);
props.setProperty("newkey", "newvalue");
OutputStream out = new FileOutputStream(file);
props.store(out, null);
I finally found the solution. All answers here were basically not right. It is possible to change a property file during runtime. The only mistake I made was not closing the Inputstream when opening the Outputstream. Here is the code that worked for me.
File file = ResourceUtils.getFile("classpath:First.properties");
FileInputStream in = new FileInputStream(file);
Properties props = new Properties();
props.load(in);
System.out.println(props.getProperty("country"));
in.close();
FileOutputStream out = new FileOutputStream(file);
props.setProperty("country", "germany");
props.store(out, null);
System.out.println(props.getProperty("country"));
out.close();
But what is really annoying that no Exception was thrown. That cost me 3 days to solve this issue. Operating system seems to block writing to file when InputStream is still open. But again I expect some exception to be thrown what is not the case.
This should works,
Properties props = new Properties();
try {
String key = "maxHolidays";
File file = ResourceUtils.getFile("file:config.properties");
InputStream in = new FileInputStream(file);
props.load(in);
String maxHolidays = (String) props.get(key);
System.out.println(maxHolidays);
props.setProperty("newkey", "newvalue");
OutputStream out = new FileOutputStream(file);
props.store(out, null);
} catch (IOException e) {
e.printStackTrace();
}
put your config.properties in the root of your project folder
Related
I'm trying to create sub directories in my apps cache folder but when trying to retrieve the files I'm getting nothing. I have some code below on how I created the sub directory and how I'm reading from it, maybe I'm just doing something wrong (well clearly I am lol) or maybe this isn't possible? (though I haven't seen anywhere that you can't). thank you all for any help!
creating the sub dir
File file = new File(getApplicationContext().getCacheDir(), "SubDir");
File file2 = new File(file, each_filename);
Toast.makeText(getApplicationContext(), file2.toString(), Toast.LENGTH_SHORT).show();
stream = new FileOutputStream(file2);
stream.write(bytes);
reading from it
File file = new File(context.getCacheDir(), "SubDir");
File newFile = new File(file, filename);
Note note;
if (newFile.exists()) {
FileInputStream fis;
ObjectInputStream ois;
try {
fis = new FileInputStream(new File(file, filename));
ois = new ObjectInputStream(fis);
note = (Note) ois.readObject();
fis.close();
ois.close();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
return null;
}
return note;
}
I've also tried with this and nothing
String file = context.getCacheDir() + File.separator + "SubDir";
I don't see anywhere in the code you posted where you actually create the sub-directory. Here's some example code to save a file in a sub-directory, by calling mkdirs if the path doesn't yet exist (some parts here need to be wrapped in an appropriate try-catch for an IOException, but this should get you started).
File cachePath = new File(context.getCacheDir(), "SubDir");
String filename = "test.jpeg";
boolean errs = false;
if( !cachePath.exists() ) {
// mkdir would work here too if your path is 1-deep or
// you know all the parent directories will always exist
errs = !cachePath.mkdirs();
}
if(!errs) {
FileOutputStream fout = new FileOutputStream(cachePath + "/" + filename);
fout.write(bytes.toByteArray());
fout.flush();
fout.close();
}
You need to make your directory with mkdir.
In your code:
File file = new File(getApplicationContext().getCacheDir(), "SubDir");
file.mkdir();
File file2 = new File(file, each_filename);
I am trying to read multiple files as BLOB (from database), decompress, zip them and stream the zipped file using JAX-WS. The above code streams the zip file but the downloaded zip file is not opened by windows program throwing CRC errors. After going through some posts, I added zipOutputStream.finish() (just before zipOutStream.flush()) which seemed to fix the error but zips only one file and ignores other files.
Any help is much appreciated
try (ResultSet resultSet = pstmt.executeQuery()) {
try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream()) {
try (ZipOutputStream zipOutputStream = new ZipOutputStream(byteStream)) {
while (resultSet.next()) {
Blob blob = getBlob();
try (InputStream fileInputStream = new GZIPInputStream(blob.getBinaryStream())) {
try (ByteArrayOutputStream fileByteStream = new ByteArrayOutputStream()) {
// Custom method: Copy the above fileInputStream to fileByteStream
IOUtils.copy(fileInputStream, fileByteStream);
ZipEntry entry = new ZipEntry(fileName);
zipOutputStream.putNextEntry(entry);
zipOutputStream.write(fileInputStream.toByteArray());
zipOutputStream.flush();
zipOutputStream.closeEntry();
}
}
try (InputStream inputStream = new ByteArrayInputStream(byteStream.toByteArray());) {
InputStreamDataSource inputDataSource = new InputStreamDataSource(fileName, inputStream);
dataHandler = new DataHandler(inputDataSource);
}
}
}
}
}
The program runs well with getResource, but after made it into the jar file, it has FileNotFoundException. It cannot find out test.conf.
My code is
URL url = getClass().getResource("test.conf");
File fin = new File(url.getPath());
FileInputStream fis = null;
try {
fis = new FileInputStream(fin);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
I think I can fix the problem by using getResourceAsStream. But I'm not sure how to change getResource to getResourceAsStream.
Your example suggests that your reading code already works on a FileInputStream. You should be able to make it work with the more general InputStream. This will allow you to just pass the result of getResourceStream() to your reading code, without having to worry about Files.
So:
URL url = getClass().getResource("test.conf");
File fin = new File(url.getPath());
FileInputStream fis = new FileInputStream(fin);
ReadResourceFromStream(fis);
Changes to:
InputStream is = getClass().getResourceAsStream("test.conf");
ReadResourceFromStream(is);
I have a block of jsp code like this. Here blockerdata, criticaldata, majordata and minordata are stringbuilder strings and their value is appended through a loop and value is assigned dynamically. Now I'm tryong to write them into an xml file like this.
<%
System.out.println(blockerdata);
System.out.println(criticaldata);
System.out.println(majordata);
System.out.println(minordata);
try
{
File file1 = new File("WebContent/criticaldata.xml");
File file2 = new File("WebContent/majordata.xml");
File file3 = new File("WebContent/minordata.xml");
File file4 = new File("WebContent/blockerdata.xml");
FileOutputStream fop1 = new FileOutputStream(file1);
FileOutputStream fop2 = new FileOutputStream(file2);
FileOutputStream fop3 = new FileOutputStream(file3);
FileOutputStream fop4 = new FileOutputStream(file4);
// if file doesnt exists, then create it
if (!file1.exists()) {
file1.createNewFile();
}
if (!file2.exists()) {
file2.createNewFile();
}
if (!file3.exists()) {
file3.createNewFile();
}
if (!file4.exists()) {
file4.createNewFile();
}
// get the content in bytes
byte[] contentInBytes1= criticaldata.toString().getBytes();
byte[] contentInBytes2= majordata.toString().getBytes();
byte[] contentInBytes3= minordata.toString().getBytes();
byte[] contentInBytes4= blockerdata.toString().getBytes();
fop1.write(contentInBytes1);
fop2.write(contentInBytes1);
fop3.write(contentInBytes1);
fop4.write(contentInBytes1);
fop1.flush();
fop2.flush();
fop3.flush();
fop4.flush();
fop1.close();
fop2.close();
fop3.close();
fop4.close();
}
catch ( IOException e)
{
}
%>
Problem is, the code doesn't seem to be working. I tried to do it using printwriter also but
the files are not being generated. Also I want to rewrite the file if it already exists. Can somebody please help me on how to do this ?
how to check if file exists and open it?
if(file is found)
{
FileInputStream file = new FileInputStream("file");
}
File.isFile will tell you that a file exists and is not a directory.
Note, that the file could be deleted between your check and your attempt to open it, and that method does not check that the current user has read permissions.
File f = new File("file");
if (f.isFile() && f.canRead()) {
try {
// Open the stream.
FileInputStream in = new FileInputStream(f);
// To read chars from it, use new InputStreamReader
// and specify the encoding.
try {
// Do something with in.
} finally {
in.close();
}
} catch (IOException ex) {
// Appropriate error handling here.
}
}
You need to create a File object first, then use its exists() method to check. That file object can then be passed into the FileInputStream constructor.
File file = new File("file");
if (file.exists()) {
FileInputStream fileInputStream = new FileInputStream(file);
}
You can find the exists method in the documentation:
File file = new File(yourPath);
if(file.exists())
FileInputStream file = new FileInputStream(file);