aspose file tools*
The moose likes Beginning Java and the fly likes should be simple....reading char... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "should be simple....reading char..." Watch "should be simple....reading char..." New topic
Author

should be simple....reading char...

ryan headley
Ranch Hand

Joined: Jun 28, 2000
Posts: 156
I've read from files before using the File class and readLine() methods but how do I read the file one character at a time??
I need to read in a large file, compare every character in the file count how many times it occurred, etc....
I can't seem to find anything that will do a readChar(), all of them have some sort of int as a return type and read in a character array....
What do I need?


Ryan Headley<br /><a href="http://www.sudovi.com" target="_blank" rel="nofollow">http://www.sudovi.com</a>
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
The Readers, like InputStreamReader and BufferedReader have methods that read the input into a char array of your choosing, which you can then process one arrayfull at a time.


"JavaRanch, where the deer and the Certified play" - David O'Meara
ryan headley
Ranch Hand

Joined: Jun 28, 2000
Posts: 156
Okay, but won't that Array get huge??
Let's say I wanted to count the occurences of each letter of the alphabet in the Bill of Rights.
My array is going to have hundreds of thousands of entries.
Is there not a way to say :
read first character, do something, read next character do something, etc.
I am not interested in speed or effieciency, its just an exercise am I working on with some fellow java beginners.
ryan burgdorfer
Ranch Hand

Joined: Jan 24, 2001
Posts: 219
The read() method of BufferedReader reads one character. Since it returns an int, I believe you just need to cast the result to char, and then you can do the desired processing on it before calling read() again.
Someone correct me if I am wrong...
(Like I even have to type that )
------------------
  • Ryan Burgdorfer
  • Java Acolyte in
  • Columbus, OH USA


<UL TYPE=SQUARE><I><LI>Ryan Burgdorfer<BR><LI>Java Acolyte</I></UL>
ryan headley
Ranch Hand

Joined: Jun 28, 2000
Posts: 156
I think I'll try that.
It "should" work, but hoping to avoid even having to cast, but it makes no real difference.
Thanx
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Even if you use a bigger array, you just read one bufferfull at a time, say 1000 characters, process them (do your counting etc), then re-fill the buffer with the next 1000 characters and continue processing, etc.
Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
why not store it in collection it's easier and you can achieve the result with minimum work.

Val SCJP <BR>going for SCJD
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Because the I/O classes (Readers and Streams etc.) do not have methods that use collections.
Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
There is no need for that unless i missed something. Why not just use StreamTokenizer and then whenever reading from IO read it into it , it has ttype to check what type of characters dealing with and then append it to collection. I have sample to show if needed.
ryan headley
Ranch Hand

Joined: Jun 28, 2000
Posts: 156
Sure, I'd love to see a sample...
thanx
Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
hi , this is dirty code , which should have been written much better but at the time i dind't and so its reduntant.
ryan burgdorfer
Ranch Hand

Joined: Jan 24, 2001
Posts: 219
Val,
Have you tried compiling and running that code? I ask because you have declared your main() method to throw an exception, rather than calling the other method that throws that exception in a try / catch block within main().
Is that legal? My understanding of exceptions is that you would only do something like that when you expect the caller of the method to deal with the exception (in a try /catch block). But in the case of main(), there will be no one calling that method but the VM...thus no way to deal with the exception if one is thrown.
Can anyone shed some light on this situation? Is it allowed? If it is allowed, it seems like it would be bad practice, since this would basically nullify the purpose of throwing an exception (by not dealing with it)...
------------------
  • Ryan Burgdorfer
  • Java Acolyte in
  • Columbus, OH USA
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
It's legal. It's bad practice.

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
Like i said this is not a good code. It's just a toy that shows you that it can be done using Collection classes this won't be used in reall application ofcourse when your main throws any Exceptoins this is bad . It's just a toy.
ryan headley
Ranch Hand

Joined: Jun 28, 2000
Posts: 156
So far I like the idea of reading into a character array.
I've done that, HOWEVER, in order to initialize that array I have to give it an initial size. Here begins the drama. I will never know the number of characters in the file being passed, therefore how will I know how large to set my array.
Here is what code I have so far:

Okay, that will read in what ever file I have but only up to 250 characters. If the file is too small, it appears that the rest of the data in the array should be null, however it won't let me compare char to null in order not to print extra spaces. I did a test to print what would be in one of the unoccupied spaces of the array and it appears to be a space. I can't compare char to space because even if that works, I want it to print the necessary spaces, (i.e. spaces between words).
Anymore suggestions?
Thanks
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

You could turn your char into a Character and store them in an ArrayList. Then it wouldnt matter what size the file is.


SCJP
Visit my download page
ryan headley
Ranch Hand

Joined: Jun 28, 2000
Posts: 156
Actually, I finally got it.
Created a FileReader, passed it the input file, created a LineNumberReader and passed it the FileReader. Then assigned a String readnums to null.
Inside a while I did something like this:

Now i've added GUI, a filechooser to choose different files. Then I will try to provide a graphical histogram...wish me luck.
Ryan
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
So far I like the idea of reading into a character array.
I've done that, HOWEVER, in order to initialize that array I have to give it an initial size.
no no no. It only returns enough characters to fill the array. Then on the next read it fills the array again from where it left off.
From the API:

Note that it returns the number of chars actually read. So if you gave it an array of 1,000 entries but there were only 10 chars left to read, it returns 10. If there are 5,000 chars to read it only returns 1,000 and waits for the next read().


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: should be simple....reading char...