File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes IO package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "IO package" Watch "IO package" New topic
Author

IO package

Vivek KumarRai
Greenhorn

Joined: Nov 06, 2000
Posts: 9
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

Joined: Oct 19, 2000
Posts: 30
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

Joined: Nov 01, 2000
Posts: 119
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: IO package