• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

IO package

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic