Help coderanch get a
new server
by contributing to the fundraiser
  • 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

File Read & Writing

 
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
 
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic