Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

IO package

 
Vivek KumarRai
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
i have the following code.when i print the file contents using BufferedReader iam getting the results properly. but when using DataInputStream for reading iam getting EOFException.Pls Help

the code is

import java.io.*;
public class IOTest {
private File f;
private FileInputStream fis;
private FileOutputStream fos;
IOTest(){}
IOTest(String s)
{
try{
f = new File(s);
f.createNewFile();
fos = new FileOutputStream(f);
OutputStreamWriter os = new OutputStreamWriter(fos);
BufferedWriter bf = new BufferedWriter(os);
PrintWriter pw = new PrintWriter(bf);
pw.println("this is test");
pw.close();
bf.close();
os.close();
fos.close();
//f.close();
}catch(IOException e){
e.printStackTrace();
//System.out.println("IOTest:Usage IOTest filename");
}
}
public void readContents(){
try{
fis = new FileInputStream(f);
InputStreamReader is = new InputStreamReader(fis);
BufferedReader dis = new BufferedReader(is);
String str = dis.readLine();
System.out.println(str);
try{
DataInputStream ds = new DataInputStream(fis);
String s = ds.readUTF();
}catch(EOFException eof){
System.out.println("EOF Exception Caught");
eof.printStackTrace();
}
}catch(IOException e){
e.printStackTrace();
}
}
public static void main(String args[]){
IOTest io = new IOTest(args[0]);
io.readContents();
}
}
 
prasanthi kothapa
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi vivek,
you have used the readUTF() method of the DataInputStream.
this reads the text which is only in the UTF format.Hence
you are getting an EOFException.Try saving a text file in
the UTF-8 format(this is one of the options while saving a text file,the default is ansi) and read it.It works that way.
But i don't know how to create a file in that format
using the File object.
please correct me if i am wrong..
bye.
 
Yoo-Jin Lee
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Does anyone know how to save a file in a UTF-8 format? For example, in WordPad there does not seem to be an option that specifies this. What's the extension for this?
Thanks.
Yoo-Jin.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic