I don't usually work with Java and got a problem with file management in Java. I'm using NetBeans IDE for coding Java.
I've added 2 resource text files under the same package of the main class of my Java program. I'm using one file to take input and want to write the output to the other file runtime. Reading data from 1st file is perfectly working using ImputStreamReader
and BufferedReader
. But, writing to the resource file is not working.
I've used following code:
File file = new File(this.getClass().getResource("outputFileName.text").getPath());
PrintWriter pw = new PrintWriter(new FileWriter(file, true));
pw.write("Some String Data...");
And getting error at runtime as follows-
Jan 30, 2017 5:05:46 AM MyPackage.MyFile MyFunction
SEVERE: null
java.io.FileNotFoundException: file name with correct path.........
.........
.........
It's indicating that, in the 2nd line of this code segment is wrong. And I've printed the absolute path of the FILE object and it shows the correct one. Even I've checked that, both of the resource files are move under build folder.
But, another strange thing is canRead()
, canWrite()
and exists()
functions- all are returning false
!!!
I've searched a lot within this short time but got no solutions yet that meets my target. If my question is a duplicate one my apologies for that. But, what's wrong with this code? How can I solve this problem and be able to do what I want?