This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Data file does not contain any input

 
Ranch Hand
Posts: 543
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
 
Master Rancher
Posts: 3852
35
  • 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: 543
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sure

 
Norm Radder
Master Rancher
Posts: 3852
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The posted code does not compile without errors from missing classes.
 
Rancher
Posts: 4611
47
  • 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: 543
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: 543
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
Master Rancher
Posts: 3852
35
  • 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: 3323
86
  • 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: 543
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that explains it

thanks guys
 
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • 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: 3323
86
  • 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: 543
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys will bare that in mind from now on =)
 
Bartender
Posts: 7196
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java 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
Bartender
Posts: 7196
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java 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: 543
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
Bartender
Posts: 7196
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java 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: 69782
277
  • 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: 543
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: 69782
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On behalf of everybody else, too: that's a pleasure
 
Right! We're on it! Let's get to work tiny ad!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic