This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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
 
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic