• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Need help reading from file

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need help quickly. I am still having problems reading from a file one character at a time. Nothing seems to be working. I must read a file and store each character into an array. The problem I am having is getting it to read an character. I can read the bytes, but not characters. Here is some sample code. Please be very specific cause I am completely dumb when it comes to java!
FileInputStream fis = new FileInputStream(fileName);
FileOutputStream fos = new FileOutputStream("outfile.txt");
int c;
int i =0;
InputStreamReader isr = new InputStreamReader(fis);
while (( c = isr.read(someChars)) != -1) {
fos.write(c);
System.out.println(c);
i++;
This is what someone suggestted I use, but now the characters are not being written to the outfile. Thanks for any help you can give.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi pam,
The java.io. classes ending in Stream are for reading/writing bytes while those ending in Reader/Writer are for working with characters. Use them judiciously to get your stuff working.
Yet I don't think that is your exact problem. Anyways try it out.
Regds
Sathish
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think your problem is that you have to convert the 'int' representation of the character to 'char' before trying to print.
Good Luck
Vernon Gibson
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic