File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes io question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "io question" Watch "io question" New topic

io question

Abdul Latif
Ranch Hand

Joined: Jul 18, 2001
Posts: 89
consider this code
public class DataStreamDemo1 {
public static void main (String args[]) {
try {
File f = new File("c:\\raftest\\");
FileInputStream fis = new FileInputStream(f);
FileOutputStream fos = new FileOutputStream(f);
DataInputStream dis= new DataInputStream(fis);
DataOutputStream dos= new DataOutputStream(fos);

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

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


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

Joined: Mar 22, 2000
Posts: 13037
You should certainly flush and close the output stream before attempting to read from the file.
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
It worked fine for me. I am using jdk1.3 on win 98.
I agree. Here's the link:
subject: io question
It's not a secret anymore!