aspose file tools*
The moose likes I/O and Streams and the fly likes File IO problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "File IO problems" Watch "File IO problems" New topic
Author

File IO problems

Nevris
Greenhorn

Joined: Feb 09, 2000
Posts: 14
Is there a way to read in a file and then count the number of each digit and each space/tab and every other character? I tried string tokenizer but that didn't work because the data isn't seperated by spaces ... eg 1 j 3 s ... it's more like 1j3s .....
And yes I know this is almost just like what I said in my other message.
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20725
    ∞

Yes, there is a way.
I would use FileInputStream. I would read byte by byte. I would have an array of 256 ints that would count how many of each byte I saw.

permaculture Wood Burning Stoves 2.0 - 4-DVD set
Nevris
Greenhorn

Joined: Feb 09, 2000
Posts: 14

Ok I get the part about the InputStreamReader ... I have that already ... what do you mean with the array?
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
It's important to read exactly what people write here. Paul didn't write InputStreamReader, but FileInputStream.
The key point is that any InputStream can read it's data byte by byte, without transforming into or out of unicode. If the data in your file is just ASCII characters, then reading from a FileInputStream will return either an ASCII character (in the range 0..255) or -1 on an error or end of file.
If you declare an integer array with 256 'slots', each slot can hold a count of the number of times that character appears in the file.
For example:

[This message has been edited by Frank Carver (edited February 11, 2000).]


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20725
    ∞

I wouldn't use a reader. I would use a plain stream.
Each byte that you encounter can have a value of 0 to 255. The values 32 to 127 are visible characters. If the file has end of line markers, you will certainly get values of 10 and or 13. So if you make an array of 256 ints, you can count each occurance of each byte encountered. As you read in a byte, use that value to index the array. Then increment that array value.
Nevris
Greenhorn

Joined: Feb 09, 2000
Posts: 14
::sigh::
I wonder if I'll ever get java. I /think/ I understand what you guys are saying. I'm going to try it and see if it works. If it does *cheer* if not back to the drawing board eh.
Nevris
Greenhorn

Joined: Feb 09, 2000
Posts: 14
Any idea why I'd get this when I run the program?

Enter the data file to be read: c:\jbuilder3\test1.txt
java.lang.reflect.InvocationTargetException: java.lang.ArrayIndexOutOfBoundsExce
ption: 0
at CharCount.main(CharCount.java:23)
at java.lang.reflect.Method.invoke(Native Method)
at com.borland.jbuilder.util.BootStrap.invokeMain(Unknown Source)
at com.borland.jbuilder.util.BootStrap.main(Unknown Source)

Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
<code>
java.lang.reflect.InvocationTargetException: java.lang.ArrayIndexOutOfBoundsException: 0
at CharCount.main(CharCount.java:23)
at java.lang.reflect.Method.invoke(Native Method)
at com.borland.jbuilder.util.BootStrap.invokeMain(Unknown Source)
at com.borland.jbuilder.util.BootStrap.main(Unknown Source)
</code>
Based on the message: look at file CharCount.java, line 23. That's what's throwing the exception. Is there an array there? Should the array have an element 0 (the first element)? The message says element 0 is what is out of bounds, which means the array must not have any elements at all. Is the array initialized in your code? Or does it come from somewhere else?
I gather you used Frank's code from above. There's something unstated about how the code must be run - see if you can track down where the error is coming from.


"I'm not back." - Bill Harding, Twister
Nevris
Greenhorn

Joined: Feb 09, 2000
Posts: 14
Ok here is line 23 ....
FileInputStream fin = new FileInputStream(args[0]);
is it having a problem with the 0 after the args?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Only if there is no element 0. Where does "args" come from? How is it defined? What's it used for?
Nevris
Greenhorn

Joined: Feb 09, 2000
Posts: 14
:P
Well I got the other problem all fixed and it works really well now. The only problem is it scrolls past the early characters when it displays the array type thing (technical phrase)...is there a command I can put in to have it pause halfway through or so? Or can I change the window somehow? (msdos prompt window)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
You can just direct the output to a file with your java command:
java -someoptions YourClassName > output.txt
Then you can browse it at your leisure with whatever editor you prefer.
Nevris
Greenhorn

Joined: Feb 09, 2000
Posts: 14
I have to do that already. I can't figure out how to find the longest string though. Any suggestions?
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
If by "longest String" you mean the one with the largest count,
just keep track of the largest as you print them out, then summarize at the bottom. Or just keep track of the largest, and don't bother printing out each value.
For example: try the following modification to my program above;
replace the 'for' loop which prints out the values with:
Nevris
Greenhorn

Joined: Feb 09, 2000
Posts: 14
By largest string I mean the largest line that was read in from the file. .... ie.
alkdisehals
adjladi
it would return the first one since it's longer than the second oen
Pawel Veselov
Ranch Hand

Joined: Jan 14, 1999
Posts: 165
Uff, is that the thing what you want ?



------------------
With best of best regards, Pawel S. Veselov ( aka Black Angel )

With best of best regards, Pawel S. Veselov ( aka Black Angel )
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File IO problems