• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't insert records in .dat file using DataOutputStream  RSS feed

 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


It compiles successfully and prints the statement on line no 34, "Record inserted !" but it doesn't insert records in Record.dat file and after fetching info from it, give null as output. please need help to figure out where I went wrong.
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You never update your eof variable which controls the while loop which would cause the loop to run forever as !eol is always true.
But it doesn't run forever because of an exception being thrown. What exception? Well, as you can see now printing only e.getMessage() is not so good idea because the exception contained no message thus null output.
The exception was java.io.EOFException (a subclass of IOException). This exception is thrown when you try to read past the end of file.
Which is to be expected as you have an infinite loop reading data in your code.

Change your while loop to:Also, your code prints only the last part of information that was read. Is it what you intended?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to call flush() on the output stream to force the data to be written. Either that or if you have finished with the output stream call close() to flush and close the stream.
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:You need to call flush() on the output stream to force the data to be written. Either that or if you have finished with the output stream call close() to flush and close the stream.

There is no need to explicitly call close(). OP uses try-with-resources which does the job.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:
Tony Docherty wrote:You need to call flush() on the output stream to force the data to be written. Either that or if you have finished with the output stream call close() to flush and close the stream.

There is no need to explicitly call close(). OP uses try-with-resources which does the job.

Oops missed that, sorry for muddying the waters.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change your while loop to:
now changing this line it prints only last record and skips first two records.
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what I see, you're adding 3 records, but you cannot retrieve them one by one.
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote:now changing this line it prints only last record and skips first two records.

That is because you override what you read in each iteration of the loop.
You have two options:
- Print what you read inside the loop.
- Store what you read in some kind of collection and print it all after the loop.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya Pawel, now I print fetched records in same loop.
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote: . . . override . . .
Is that spelt correctly? Should it say “overwrite”?
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It worked !!! Thank you everyone, It has been 2 hours I was trying to figure out it. It also worked using
used in this video so was using that.
by Derek Banas.

It wasn't working because I didn't catch EOFException in that I was supposed to assign eof = true so it would become false in this loop condition
thank you once again It is the best feeling when you see your code runs successfully.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it Campbell he meant overwrite
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote:It wasn't working because I didn't catch EOFException in that I was supposed to assign eof = true so it would become false in this loop condition

I don't think it is a correct way to end the "while" loop (wait until exception is thrown, and then in a catch block update "eof", which is after the fact).
This is something like "I known that something wrong is going to happen at exact time, but not going to do nothing until it happens".

And you should get a comment from someone in a minute about the length of your "main" method
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Change your while loop to:

Using the available() method to read to the end of the file is dangerous and is not what the method is intended for. The Java docs say:
Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream. The next caller might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.

When reading from a local file the code will probably work all of the time but if there are any delays in the the data stream such as can occur when reading a file across a network the available() method is likely to return 0 before the end of the file is reached.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is something like "I known that something wrong is going to happen at exact time, but not going to do nothing until it happens".

yes I got that
And you should get a comment from someone in a minute about the length of your "main" method
yes I'll make separate methods for inserting and fetching records.
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:Using the available() method to read to the end of the file is dangerous and is not what the method is intended for.

Ouch, this time you corrected me. You are right.
Tony Docherty wrote:When reading from a local file the code will probably work all of the time (...)

We are reading from the local file, aren't we
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then what should I use there instead of and ? because I'm doing same program using Object Streams i.e. ObjectInputStream and ObjectOutputStream.
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check Tony's comment based on Pawel's suggestion. He described the circumstances, when this method would work as you want, and when could cause an unexpected results.
You're reading file "Record.dat" which is being stored locally, so the delays shouldn't occur in that particular your case, so likely "while( din.available() > 0) {..}" would work here.
But is it going to be true about file being stored locally always/in a future? If not, then you'd need to look for a different way to escape the loop on a certain time.
Just to clarify - that "eof = true;" assignment in a catch block doesn't have an impact to a while loops condition, since its value is changed after the fact, that exception was thrown.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Vilda yes that file is always going to be local so I'll use .
Just to clarify - that "eof = true;" assignment in a catch block doesn't have an impact to a while loops condition, since its value is changed after the fact, that exception was thrown.
yes I got about that loop and exception. thank you once again.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote:then what should I use there instead of and ? because I'm doing same program using Object Streams i.e. ObjectInputStream and ObjectOutputStream.

For most InputStream methods you would check to see if the returned int value is -1 which only occurs when the End Of File has been reached however most of the DataInputStream methods return a specific data type rather than an int so when calling these methods you need to catch an EOFException which only occurs when the End Of File has been reached. So to answer your question the proper approach is to have an indefinite loop eg while(true) {..} wrapped in a try-catch statement which catches the EOFException. Your try-catch statement can also catch other exceptions but you must handle these as exceptional conditions rather than a normal end to reading the file.

Personally I find using Exceptions to signify a normal end to an operation somewhat confusing and misleading and wouldn't advocate replicating this way of using Exceptions in your code.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For most InputStream methods you would check to see if the returned int value is -1 which only occurs when the End Of File has been reached however most of the DataInputStream methods return a specific data type rather than an int
yes I tried that but didn't work as it returns specific data type than int. yes right now I'm using din.available what else I can do to make me feel content, that I've run code successfully
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote: . . . Personally I find using Exceptions to signify a normal end to an operation somewhat confusing and misleading . . .
Agree. I think it is bad design that you have an Exception rather than a has next method, but from what it says in the Java™ Tutorials, I think we are stuck with that.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Notice that DataStreams detects an end-of-file condition by catching EOFException, instead of testing for an invalid return value. All implementations of DataInput methods use EOFException instead of return values.
Ref: JLS
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!