• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading/Writing Binary/Text Files  RSS feed

 
Bryan Malumphy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I'm writing a program that reads and writes to binary and text files. This is the almost completed program, however, one of two things is happening that I cannot diagnose or treat. Either when it prompts for a second line in the binary segment of the program it doesn't write the second line, or when it reads the binary file created it doesn't read the second line. Any insight would be appreciated.

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

That happens because in the part that reads from the binary file, lines 26 to 35, there's no loop or anything like that. It just does is.readUTF() one time, reading the first string that was written to the file. There should be a loop to read the rest of the strings.

There are a lot of other things that could be improved in this program. For example, don't put everything into one very long main() method. Split it up into separate methods, which each do one thing, instead of one main() method which does everything.
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

Are you really using a Scanner for binary files? Scanners are only suitable for text files. Have you read the Java™ Tutorials Section about I/O?
You also have methods much much too long. Move about 99% of that code out of the main method.
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you are not using the Scanner on the file. Sorry for my mistake. You will probably find a Scanner best for reading the text file. To read it line by line try something like this:-Although BufferedReader is faster and maybe more forceful with files, Scanner is much more versatile; if you know there are three ints and a double to each line you can use nextInt() thrice and nextDouble(). Read about using Paths in that tutorials section I showed you. Read about try (...) here and you will then know why there is no close() call.
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bryan Malumphy wrote: . . .
I would use a Formatter for writing to a text file, but I have never managed to get a Formatter into append mode, which I presume is why you have passed true to the constructor. Don't use a file output stream for text files. Use a file writer. It is all in the tutorials section.
 
Bryan Malumphy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot guys. I'll be reading over the tutorials tonight when I get home. I appreciate the input.
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!