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.
public int fileSize(String name) throws IOException
{
try {
InputStreamRead er isr = new InputStreamRead er(new FileInputStream ("name"));
int charLength = 0;
while (isr.read() != -1)
charLength++;
return charLength;
}
catch (Exception e) {
return -1;
}
}
it always returns -1
any know where i am going wrong?
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.
public int fileSize(String name) throws IOException
{
try {
InputStreamRead er isr = new InputStreamRead er(new FileInputStream ("name"));
int charLength = 0;
while (isr.read() != -1)
charLength++;
return charLength;
}
catch (Exception e) {
return -1;
}
}
it always returns -1
any know where i am going wrong?
Comment