-
April 17th, 2009, 09:42 AM
#1
Counting characters in a file
I need to read in a file and count each character in it. I tried using a while(input.hasNext) loop but that put me into an endless loop. Should i be parsing the file first? I read the APIs but i didn't find any particular hasNext method
-
April 17th, 2009, 11:53 AM
#2
Re: Counting characters in a file
Ugly and direct, considering the deprecated readLine() method, but it would do it:
PHP Code:
import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.text.DecimalFormat;
public class CharCounter {
public static void main(String[] args) {
File file = new File("text.txt"); FileInputStream fis = null; BufferedInputStream bis = null; DataInputStream dis = null; DecimalFormat f = new DecimalFormat("#0");
try { fis = new FileInputStream(file); bis = new BufferedInputStream(fis); dis = new DataInputStream(bis); double count = 0;
while (dis.available() != 0) { count += dis.readLine().length(); } System.out.println("chars in file: "+f.format(count)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?
I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)
//always looking for job opportunities in AU/NZ/US/CA/Europe :P
willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));
USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!
-
April 17th, 2009, 01:35 PM
#3
Re: Counting characters in a file
Originally Posted by Xeel
Ugly and direct, considering the deprecated readLine() method
You could do the same thing with a BufferedReader and avoid the deprecated method.
Today, most software exists, not to solve a problem, but to interface with other software...
I. O. Angell
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
April 17th, 2009, 04:16 PM
#4
Re: Counting characters in a file
Originally Posted by dlorde
You could do the same thing with a BufferedReader and avoid the deprecated method.
I know... It was a kinda "do-my-homework-for-me" question, so I just took the first "how to read/write files in java" tutorial google gave me and edited the code.
Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?
I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)
//always looking for job opportunities in AU/NZ/US/CA/Europe :P
willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));
USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!
-
April 18th, 2009, 07:12 AM
#5
Re: Counting characters in a file
so I just took the first "how to read/write files in java" tutorial google gave me and edited the code.
Well that's got to be one of the worst file reading tutorial examples I've ever seen.
Ignoring the deprecated method (I suppose it may not have been deprecated when the tutorial was written) it still is dreadful.
1. The input stream is never closed so if you cut and pasted this into a long running app you'd never release the file resources this code uses and if the code was called repeatedly you'd eventually run out of resources.
2. Using the available() method to test whether the end of file has been reached is totally wrong. The available() method in the words of the API docs "Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream". So returning 0 doesn't mean the end of the file has been reached it just means at an instant in time there was nothing to read.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|