This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
My first Java post ever!!! I was hoping somebody could help me out with a school project i'm working on. We are learning data streams and I'm stuck at a certain point. I'm not sure why i'm receiving a null when I try to read the order.txt file. Can somebody maybe take a look at the code and point me in the right direction?
You don't say exactly what goes wrong for you, but the one major thing I see is that you try to open the file for reading before it's been written to; it doesn't work like that, and on some operating systems you won't even be allowed to try. You have to wait to open the file for reading until after you've written to it and closed it. Just move these lines
down to around line 50 in the listing, right after "output.close()". That should get you to a better place. I can't guarantee there aren't other problems but that's the main one.
Thank you for the reply. Well you have me going in the right direction. Basically here is what im trying to do...
Collect input and then store it to a file named order.txt located on the root C:\.
After I do that, I prompt the user if they wish to add any additional records.
If they choose yes, I loop through the prompts again, if not, I write out the information to the text file.
Then I am supposed to read the data in the text file and display it in a message box.
After making your suggested change, I had to switch some other code around too. It has brought me closer to my destination point because now I am getting the message box and it is reading the text file, however I still get that null message when the program tries to read the text file until it reaches the end. After running some debugging, my code seems to be erroring out here:
The error triggers after it processes the while loop for the second time.
seems to be assuming that readInt() returns -1 if there are no more ints to read. It does not -- if you look at the Javadoc for java.io.DataInput, you'll see that it throws EOFException if you call it when there is no more data. You're catching and reporting that as an error -- it's not really an error, it's just the end of the file.
Now, as to how to fix this, there are several alternatives, in decreasing order of preference:
1) Write the data as text, not binary data, and use Scanner to read in the text. Scanner can tell you if a "next int" exists or not. Storing data as text is more flexible and is to be preferred much of the time.
2) Before writing any data to the file, write a number representing the amount of data that follows. This is pretty traditional with binary data files.
3) Catch EOFException, and just terminate the loop and close the file, without reporting an error. This is generally considered very bad style.
P.S. Thanks for using CODE tags!
Joined: Apr 21, 2011
Thanks so much for your suggestions. I chose option 3, not because I wanted too, but because I believe this is what my instructor wants and what my text book teaches (which is unfortunate, haha).
For other readers out there, this is what I changed...
I will definitely keep in mind your suggestions for further projects, and you are welcome for the CODE tags . I was wondering how you got your code to show up nice and pretty instead of just plain text.