It's not a secret anymore!
The moose likes Beginning Java and the fly likes File Read  & Writing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "File Read  & Writing" Watch "File Read  & Writing" New topic

File Read & Writing

Anthony Smith
Ranch Hand

Joined: Sep 10, 2001
Posts: 285
file = "test.txt";
I have to use PrintWriter to print out my info, but it once a char not a byte. How can I convert my data to a char or read it as a char.
Right now, this does not compile cause
out.write(buf, 0, bytesRead) once a char not a byte.
Please Help
File file1 = new File(file);
FileInputStream fis = null;
fis = new FileInputStream(file1);
PrintWriter out = new PrintWriter(res.getWriter());
byte[] buf = new byte[4 * 1024]; // ???
int bytesRead;
while ((bytesRead = != -1)
out.write(buf, 0, bytesRead);
Terence Doyle
Ranch Hand

Joined: May 30, 2001
Posts: 328
create a BufferedReader and pass in the FileInputStream like this
BufferedReader buff = new BufferedReader( fis ) ;
The method reads in data from the InputStream and converts it to a char. It also has a readLine() methos and skip(long chars) which can be handy.
Hope that helps,

Terry Doyle
Sun Certified Programmer for Java 2 Platform

Raising Flares debut album 'Ignition' out now

Terry Doyle <br />SCPJ 1.4 , SCWCD , SCMAD(Beta)
I agree. Here's the link:
subject: File Read & Writing
It's not a secret anymore!