The Artima Developer Community
Sponsored Link

Java Answers Forum
Java I/O Problem.(Thanks!)

1 reply on 1 page. Most recent reply: Aug 27, 2003 5:49 AM by David

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Jack

Posts: 6
Nickname: daxian2003
Registered: Apr, 2003

Java I/O Problem.(Thanks!) Posted: Aug 26, 2003 11:22 PM
Reply to this message Reply
Advertisement
// Java 1.1 and higher I/O Problem.
//Why??? iThanksj
//Running?

/**
*3.14159
*That was the value of pi
*This is pi/2:
*java.io.EOFException
* at java.io.DataInputStream.readFully
*(DataInputStream.java:268)
* at java.io.DataInputStream.readLong
*(DataInputStream.java:470)
* at java.io.DataInputStream.readDouble
*(DataInputStream.java:518)
* at IOProblem.main(IOProblem.java:36)
*Exception in thread "main" Process terminated with exit *code 1
*/


import java.io.*;

public class IOProblem {
// Throw exceptions to console:
public static void main(String[] args)
throws IOException {
DataOutputStream out =
new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream("Data.txt")));
out.writeDouble(3.14159);
out.writeBytes("That was the value of pi\n");
out.writeBytes("This is pi/2:\n");
out.writeDouble(3.14159/2);
out.close();

DataInputStream in =
new DataInputStream(
new BufferedInputStream(
new FileInputStream("Data.txt")));
BufferedReader inbr =
new BufferedReader(
new InputStreamReader(in));
// The doubles written BEFORE the line of text
// read back correctly:
System.out.println(in.readDouble());
// Read the lines of text:
System.out.println(inbr.readLine());
System.out.println(inbr.readLine());
// Trying to read the doubles after the line
// produces an end-of-file exception:
System.out.println(in.readDouble());
}
}


David

Posts: 18
Nickname: malven
Registered: Jul, 2003

Re: Java I/O Problem.(Thanks!) Posted: Aug 27, 2003 5:49 AM
Reply to this message Reply
You're getting in trouble because you're trying to read using a BufferedStream and the underlying stream that it is wrapping (in this case, the DateInputStream) at the same time. When you invoke inbr.readLine(), it's likely that
the entire contents of this small file are being read into the BufferedReader's cache; so the underlying streams see EOF when you ask *them* for more data.

Generally speaking, when you wrap one stream with another, it's not a good idea to continue performing operations on the underlying stream. This is definitely the case when the outer stream is buffering data.

Flat View: This topic has 1 reply on 1 page
Topic: Java inputstream reader Previous Topic   Next Topic Topic: SQL Injection

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use