<Java コード>
import java.io.;
import java.util.;
class IOExercise{
public static void main(String[] args) throws IOException{
double s2;
BufferedReader kin =null;
DataInputStream in =null;
DataOutputStream out =null;
System.out.println("Enter the number:");
try{ kin = new BufferedReader(new InputStreamReader(System.in)); in = new DataInputStream(new BufferedInputStream(new FileInputStream("number.data"))); out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("number.data"))); String str = kin.readLine(); double num = Double.parseDouble(str); double s = Math.sqrt(num); System.out.println("Square root value " + num+ " is "+ s); out.writeDouble(s); s2= in.readDouble(); System.out.println("The square root value " + s + " is written to the file \"number.data\" "); System.out.println("The value read from the file \"number.data\" is " + s2 ); System.out.println("Square of " + s2 + " is "+ s2*s2 ); }catch(FileNotFoundException e){ System.out.println("File not found " + e); }finally{ in.close(); out.close(); kin.close(); } }
}
実行結果
Enter the number:
9
9
Square root value 9.0 is 3.0
Exception in thread "main" java.io.EOFException
at java.io.DataInputStream.readFully(DataInputStream.java:197)
at java.io.DataInputStream.readLong(DataInputStream.java:416)
at java.io.DataInputStream.readDouble(DataInputStream.java:468)
at IOExercise.main(IOExercise.java:22)
質問
number.dataに文字が書かれていたので、ちゃんと書き込まれているはずなのですが、なぜか読み込むことができずにEOFExceptionが起きてしまいます。どこがおかしいのでしょうか。

回答2件
あなたの回答
tips
プレビュー