Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DataInputStream and DataOutputStream in Jar file (no need to reply now)

 
Neil Cartmell
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. I've been learning I/O recently and everything seems fine except for when i put my program into a jar file.

My current program is a game that tests how fast you can type the alphabet and i want it to store the fastest time, which is a double variable in a file. I wanted to use serialisation instead of just using a text file.

Basically my program, when it loads, it tries to read a file that has the highest score in it, if the file isn't there my program catches an exception and then calls a method to write the file. It then saves the fastest time whenever someone beats the old time. This worked absolutely fine! Just how i hoped it would work unitl i put my program into a jar file. Now my program creates the file once opened like it is supposed to but it won't save anything.

I earlier had another program that saved to txt. Even as a jar file this worked fine as long as the files were in the same folder as the application so i had wrongly assumed it would work the same way.

Underneath i will put the code for the methods that read and write from the file. And underneath that i will put the whole class just in case anyone is interested in seeing it. Please can anyone give me some advice on how to handle this and what i need to look into.






And here is the whole class.


 
Neil Cartmell
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok this is embarrassing. It turns out it's just a bug in my code, It DOES save but just not the first time. I have to beat the high scrore twice for it to save. So it turns out it's just my error. Not sure what the problem yet is but i'm sure i will find it pretty quick.

I hope no one wasted their time reading this. Sorry!

edit: And if any one is interested. I just had to swap lines 81 and 82 over for it to work properly!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic