Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File Read & Writing

 
Anthony Smith
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 = in.read(buf)) != -1)
{
out.write(buf, 0, bytesRead);
}
 
Terence Doyle
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
create a BufferedReader and pass in the FileInputStream like this
BufferedReader buff = new BufferedReader( fis ) ;
The BufferedReader.read() 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic