Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

IOException when trying to load from binary file  RSS feed

 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have log.dat binary file to store some application data in it. It is stored in applicationdata package.
Also, there is a static ApplicationData class in the same package with following method:

I also tried with this:

But every time method is invoked, an IOException is thrown. What is wrong here?
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not Swing related, moving to I/O. And what's the stack trace?
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for posting it under Swing category.
Here is a stack trace:
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Solution to the issue:

Also, log.dat file had to be initialized first; otherwise NullPointerException is always thrown.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I've made a mistake. The above solution(s) work only when I run from IDE. When I build JAR and run application, I get the samo exception. Any advices?
 
Paul Clapham
Sheriff
Posts: 22502
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You said something about "building a JAR". You didn't explicitly say that the log.dat file is in that JAR. However if it is, then it's a mistake to pretend it is a file and to read it with a FileInputStream. Entries in a JAR are not files.

Continuing with the assumption that log.dat is in the JAR: you have way too much code. Simplify it to this:



On the other hand perhaps log.dat is not in the JAR. In that case you're going to have to tell us what "the same exception" means, since you already mentioned two different exceptions.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, now I see I got that wrong from the beginning. So, I suppose it's time for new start on this issue.
This is the idea:
log.dat file is going to be a binary file to store some application data in it (e.g. list of recently opened files in text editor I'm working on).
How to handle writing and reading to that file, with condition that file should be also accessed when I build JAR?
Perhaps, the idea is wrong, I don't know... it seemed to me like a good solution.
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can read files in a JAR like Paul has shown. Writing to files in JAR files is not possible. Period. That's because Java is using the file, and that means you can't modify it.
 
John Vorwald
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you want to store application data, have you thought about using preferences.
Preferences description

To help locate files, I use the following



 
albert thomsan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are not using the file format properly. Because you first open the file and then trying to read it. You have wrote some syntax which is not proper. You should read about the file object and its format of using. You get IOException because there is a problem in reading or writing the file.
 
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!