BufferedWriter writer = new BufferedWriter(new FileWriter(destinyFile));
String content = "";
while (!content.equals("exit")) {
content = reader.readLine();//read input from the console
writer.write(content + "\n");//write the input to the destinyFile
}
writer.close();
System.out.println(destinyFile.toString()); //this only gives the fileName and I want to display the content of the file.
I'm confused about the inner workings of the writer.write(content) bit. I thought that with this piece of code you write every String to the destinyFile.
And how to display the contents of this file?
How to read the contents of the file that contains all that is written to it?
Resolved
Comments (6)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
20 December 2020, 18:19
This is because destinyFile is a String, no more important that any other String. The earlier code set that String's value to a file name, but it is not a file, it is still a String.
The FileWriter uses that file name to create and write to an actual file on your hard drive (if you are testing on your cpu). You will need to navigate to the actual file to see the data contained within.
I have a folder on my desktop that I use only for program outputs. Its a simple location I can easily find and then see the outputs I am receiving to test my programs. I just copy and paste the directory into java and add a filename, then go back to it after running the program and the output is there. (See picture)
That copy + paste is the directory only, make sure to add a filename to yours. I suggest making it a .txt file so you can open it too. Something along the lines of:
C:/users/liliane/desktop/myOutput.txt
(your actual path may vary)
This would create a file on your desktop names "myOutput.txt" and the lines you are looking for would be contained within.

0
Liliane Top Backend Developer at Procura
21 December 2020, 09:28

0
Liliane Top Backend Developer at Procura
21 December 2020, 09:30
I tried to have a txt file within my IntelliJ. Nothing gets written to it and it only gives me the memory location. I'll try to have a txt file on my desktop and see if that works. But not sure why this isn't working...
0
Liliane Top Backend Developer at Procura
21 December 2020, 09:48
I'm one step closer. I added the following
public static File file = new File("testout.txt"); // as it is in the same directory
FileOutputStream foutput = new FileOutputStream(file);
If I understand you correctly this is what you were referring to when you wrote destinyFile is just a String. So now I made it into a pathname.
However it still only gives me a memory location of the file but doesn't add anything to that file.
0
Liliane Top Backend Developer at Procura
21 December 2020, 09:59solution
One step closer.
Even though it is in the same directory it only works to read the file if I give it the full pathname:
File file = new File("/Users/lilianetop/Desktop/excercises/src/InputOutput/testout");
and then use:
FileInputStream fin = new FileInputStream(file);
SO with your help I'm able to read the file.
I also succeeded in writing to the same file with FileOutputStream. Very Happy🥳
And I found out that the following also works:
File file = new File("../testout");
+2
Guadalupe Gagnon
21 December 2020, 14:12solution
yes and yes. They only work with the full file path.
+2