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.
I got the contractor project for my SCJD exam. I went through the data file format. I managed to read the file just using the DataInputStream (for the numeric values in the header), and for the text values i just used the FileInputStream's read(byte b), and converted the byte into a String. But i want to follow the encoding stuff given in the instructions. So i thought of using InputStreamReader(InputStream in, String encoding), and the DataInputStream to read the binary primitive datas as before. But i am stuck with the following problem. I start of using the DataInputStream and read the header values. I read the magic cookie, the offset value, and the number of fields. Then i read the length of the first field name. All using the DataInputStream. Then the few bytes is text value encoded in US-ASCII format. So now i want to use the InputStreamReader and read the next few bytes. Once i made use of the InputStreamReader, i want to go back to using the DataInputStream to read the next available primitive data, but i couldnt. It complains that EOF has been reached and throws EOFException. It seems like the InputStreamReader is stuck somewhere and makes it look like EOF? I am using these three objects InputStream inputFile = new FileInputStream("db-2x2.db"); DataInputStream inputStream = new DataInputStream(inputFile); Reader reader = new InputStreamReader(inputFile,"US-ASCII"); Thoughts appreciated. [ August 29, 2003: Message edited by: Ken Wong ]
Hi Ken, Try "public final void readFully(byte b)" on your DataInputStream followed by some "new String(byte, String charsetName)". Best, Phil.
Joined: Aug 29, 2003
Thankyou, that solved my encoding usage problem. I can convert string to a byte array using a certain encoding, and convert the read bytes into a string using a certain encoding. So i will be safe to say that i followed the instructions of using the US-ASCII enconding while writing into and reading out of the file. But out of curiosity why is that when i couple InputStreamReader and DataInputStream i cannot switch between them. After i switch to the InputStreamReader i cant get back to the DataInputStream. Throws an EOFException. Ken
Joined: Jun 02, 2003
Hi Ken, I am happy I could help.
But out of curiosity why is that when i couple InputStreamReader and DataInputStream i cannot switch between them. After i switch to the InputStreamReader i cant get back to the DataInputStream. Throws an EOFException.
I really don't know. I just suppose that both input streams are not synchronized in some way, but as anyway you got a solution... Please don't reply to me directly, because I'll quit this forum for a while, probably till I've done with my assignment (db is done, I am starting the sockets part and will finish with server and client GUIs). I am actually in a hurry to get my assignment done and I noticed that when reading posts in this forum, helping people you can help is so compelling than you do it even when you have not the time to do so. F..g altruism ! You'll find a lot of people here willing to help you as far as your question is precise (as yours was) and simple. But for more technical questions, I think that other forums here on JavaRanch (the ones specialized in a given technology) are a better place to post them. For example, a very interesting discussion between Jim and Max about FileChannel has been moved recently to such a forum. Our new bartender Andrew is amazingly fast and willing to reply and bring help, and I couldn't count the number of times Andrew helped me (even when the issue was rather technical or complex ). Best and good luck, Phil. [ August 30, 2003: Message edited by: Philippe Maquet ]