I've got a piece of code which mostly works, but I don't understand why. Here's a simplified version of the code illustrating the issue:
String thisdir = System.getProperty("user.dir");
String newdir = thisdir "\\src\\test\\resources\\Test.txt";
File input = new File(newdir);
FileWriter writer = new FileWriter(input);
for (int i = 0; i < 1; i ) {
writer.write("completed");
}
for (int i = 0; i < 2; i ) {
writer.write(System.getProperty( "line.separator" ));
writer.write("completing");
}
String path = Paths.get(this.getClass().getResource("/Test.txt").toURI()).toFile().getPath();
The code creates "Test.txt" in the \src\test\resources directory. It then writes "completed" in the first line, followed by "completing" x2 in the next two lines. Finally it defines "path" to be the path of the file.
My questions are:
- Suppose that, in the \src\test\resources directory, I make a Test.txt file and add some random text into it. Then, after I run this code, the random text disappears and is replaced by "completed" and "completing" x2. Why? I haven't used any methods such as
trim
to remove text. My first guess was that Java was simply overwriting the text (as opposed to deleting everything and starting from a blank file), however when I tested that it doesn't work - if this hypothesis is true, then if I rerun the code withi < 1
in thefor
loop that writes "completing", the file should still contain two lines of "completing", but it doesn't. - Conversely, if I start with no file in the \src\test\resources directory, then the line defining
path
returns a NullPointerException. Why? The earlier code should have created "Test.txt" in that folder (certainly after I run the code it is there), and the file should be detected. If I start with a Test.txt file in the directory then the code works andpath
returns the expected output, as well.
The tutorial I've seen seems to indicate that Java will not create a new file if the file already exists. However, it doesn't explain either of my questions.
CodePudding user response:
For the first question when the file is opened for writing (as it is when you create the FileWriter) it is cleared, the length is set to 0. If you want to append you need to open the file for appending. There is a constructor with a boolean append flag you can use for that.
For the second question getClass().getResource("/Test.txt")
refers to the compiled resources typically found in target/classes or bin. If you delete the file from src/resources it will not be built and there is no file in the target directory. It won't help that you create the file in src when you run the program; by then it is too late. The resource is not found and you get a NullPointerException
.
CodePudding user response:
I think for your first question that will help - How to append text to an existing file in Java?
For the second question: It's normally that if you do not create the file from the Java code the File constructor gets the file path for the file but it is not creating it. So you must use createNewfile() method which is provided by File's api.