I am trying to make code that can read a text file and write to it, and I'm having two issues. Here is a link to the code. I don't know what I am doing wrong, and need some guidance on what I could be doing better.
The main issue is when using my methods that write to the file, when I look at the actual .txt file it is NEVER the correct number, or even numbers at all. The only thing inside the text file after writing to it is random symbols that make no sense. I'm not sure why this is, I'm using data and file output streams and using the dos.writeInt method, but the numbers are never written to the file correctly. I don't get any errors from Java or anything indicating it is working incorrectly, so I would greatly appreciate any help. I am a beginner so it may be something I am not aware of.
Another issue I am having is none of my File x = new File("nameOfFile.txt") methods are creating a file. In order for the code to work I have to have already created a .txt file with the same name. Ideally, this program would just create a text file itself according to the code, but it does not. I have a previous project I did where I used the same exact method and that class did create a file, but here in this code it does not.
I've tried searching online and researched all the file creation and writing methods I could find, but nothing is working for me. Again, I'd really appreciate help to understand why this is happening and what I am doing wrong.
Subreddit
Post Details
- Posted
- 3 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/learnjava/c...