• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

io question

 
Abdul Latif
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
consider this code
import java.io.*;
public class DataStreamDemo1 {
public static void main (String args[]) {
try {
File f = new File("c:\\raftest\\Datatext.java");
FileInputStream fis = new FileInputStream(f);
FileOutputStream fos = new FileOutputStream(f);
DataInputStream dis= new DataInputStream(fis);
DataOutputStream dos= new DataOutputStream(fos);

dos.writeBoolean(true);
dos.writeUTF("the moving finger writes");

boolean b = dis.readBoolean();
String s = dis.readUTF();


System.out.println(b);
System.out.println(s);


}
catch (IOException e) {
System.out.println("cannot find the file");
}

}
}
This code writes aboolean & then writes a utf string. After writing it tries to read the boolean value with the readBoolean() & thentries to read the UTf sring with the readUTF().
the program only prints true & not the string. why ?
the readUTF() should read the string accroding to me. Can u please make things clear about UTf & unicode encoding. do the exam has questions on encoding
please help
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should certainly flush and close the output stream before attempting to read from the file.
 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It worked fine for me. I am using jdk1.3 on win 98.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic