• Post Reply Bookmark Topic Watch Topic
  • New Topic

Data file does not contain any input  RSS feed

 
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys I wrote to a dat file but to my surprise it contains no data at all it's blank but I explicitly wrote to the file

any thing I'm doing wrong?  here is the code



the code which I'm referring to is in the main method
 
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you delete the extra statements and leave the minimum that is needed to compile and execute the code for testing?
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sure

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The posted code does not compile without errors from missing classes.
 
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to put some debugging statements in there so you can check the value of what is supposed to be being sent to the file.
That might help you narrow down where the problem lies.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey Dave thanks for the advice I tried adding a  println to see if the block of code is actually getting executed,and it indeed is debug gets printed out a couple of times so the block of code is getting executed but the data is not been written to the file and if it is it's blank for some reason,

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed the code a little and it worked I declared the DataOutputStream outside the try block and closed it in a finally block it now displays info

but the question is how come it was not writing the data to the ou file? thanks

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post a small, complete program that will compile and execute for testing.
 
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's because you weren't flushing or closing the output stream so the data wasn't all being sent to the file.
you should always always always close any streams you have opened once you have finished with them - the easiest way is to use the try-with-resources feature.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that explains it

thanks guys
 
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam: you have a habit of posting incomplete code.  Read about what a SSCCE is (that should be a link).  Here's what one would look like in your case:

Note that:

* It's formatted correctly
* It contains the necessary import statements
* It compiles
* It doesn't reply on any missing classes

And finally, it works.  You will find that creating an SSCCE will help you find you solution.  And if not, it can be posted and others can try it.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And to add to what Knute has said it only contains the relevant code. It's far easier to spot a bug in 17 lines of code than in 200+ lines of code.

What you should remember is the more effort you put into writing your question, supplying all the information and a good SSCCE the more likely you are to get a quick and accurate answer to your problem. People who consistently don't put in that effort find over time they get fewer and fewer responses to their posts from the many experts here.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys will bare that in mind from now on =)
 
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DataOutputStream only supports serializing primitives and won't support writing out collections (e.g. Maps and Lists).  If you use ObjectOutputStream you can then serialize and write Objects. This comes in handy when you want to push the Object I/O down into their respective classes, thus cleaning up encapsulation and simplifying the IO calls needed at the higher level.

Here I've taken an educated guess at your classes. This is stand alone code meant as an example of what could be done.

Edit: Created read/write methods in LocationMain.
 
Carey Brown
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you need to have Locations implement a Map. This unnecessarily exposes the internals of the Locations class.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow thanks Carey much appreciated =)

I'll study that and try to implement that in my code

 
Carey Brown
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note: I edited my posted source code to add new read/write methods for improved modularity.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:And to add to what Knute has said it only contains the relevant code. . . .
Please don't think we are all getting at you, but another thing about code: Unless you are trying to show the difference between code block A and code block B, remove all the unnecessary comments and commented‑lines, which includes //TODO comments from Eclipse. You could probably reduce the length of your first code block by 50% like that. You will also get more marks if you hand in code without extraneous comments.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell no worries

I've learned a great lot from you guys already,it's an honour,I'm forever thankful =)
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On behalf of everybody else, too: that's a pleasure
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!