3

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 with i < 1 in the for 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 and path 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.

Allure
  • 261
  • 1
  • 9
  • For your second question, the line that creates path has many places where a NullPointerException can occur. It will be enlightening to see where it actually happens. One way to do this is to break each function call into its own line and the see which line causes the NPE. – Code-Apprentice Oct 27 '21 at 06:46
  • For the first question, what happens if you have an existing file with say 20 characters and you open it and write 5 characters to the file? – Code-Apprentice Oct 27 '21 at 06:52
  • One last tip: check out the [`FileWriter` documentation](https://docs.oracle.com/javase/8/docs/api/java/io/FileWriter.html). – Code-Apprentice Oct 27 '21 at 06:53
  • @Code-Apprentice I tried what you suggested for the first question and all the other 20+ characters are deleted; it's consistent with Java deleting the file and writing everything from scratch. I have yet to test your suggestion for the first question; I'm working to understand that line since I don't understand it well. – Allure Oct 27 '21 at 06:58
  • You can start with something like `Class klass = this.getClass();` then do `Resource resource = klass.getResource("/Test.txt");`. Continue in this way and add appropriate prints to see what value is null. – Code-Apprentice Oct 27 '21 at 15:05

2 Answers2

2

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.

ewramner
  • 5,810
  • 2
  • 17
  • 33
0

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.