CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2008
    Posts
    17

    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

  2. #2
    Join Date
    Jul 2005
    Location
    Currently in Mexico City
    Posts
    568

    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!

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Counting characters in a file

    Quote Originally Posted by Xeel View Post
    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.

  4. #4
    Join Date
    Jul 2005
    Location
    Currently in Mexico City
    Posts
    568

    Re: Counting characters in a file

    Quote Originally Posted by dlorde View Post
    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!

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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
  •  





Click Here to Expand Forum to Full Width

Featured