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 -
Expand|Select|Wrap|Line Numbers
- public int countChars(InputStream is) throws IOException
- {
- int count = 0;
- BufferedInputStream bis = new BufferedInputStream(is);
- while(bis.read() != -1)
- {
- count++;
- }
- return count;
- }