streams!?!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • marky123
    New Member
    • May 2007
    • 2

    #1

    streams!?!

    this particular question is givin me a headache, can any1 help???

    Using the countChars() method from the previous answer, write a method called fileSize() declared as follows:

    public int fileSize (String name) {
    ...
    }

    which counts the number of characters in the file whose name is supplied as the "name" parameter. This method should return the number of characters in the file, or -1 if an IOException occurs.
    Insert the code for your method in the space below:

    Previous answer -
    Code:
    public int countChars(InputStream is) throws IOException
    {
       int count = 0;
       BufferedInputStream bis = new BufferedInputStream(is);
       while(bis.read() != -1) 
    {
          count++;
       }
       return count;
    }
    Last edited by JosAH; May 7 '07, 04:01 PM. Reason: fixed the tags (added [code] ... [/code] tags)
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    So basically you have to create an InputStream from a file name, right?
    Have a look at the FileInputStream class for that.

    kind regards,

    Jos

    ps. don't forget to close the InputStreams where needed.

    Comment

    • marky123
      New Member
      • May 2007
      • 2

      #3
      cheers, all done!

      Comment

      Working...