public static int howMany(String FileName)
{
BufferedReader br = null;
try
{
FileInputStream fis = new FileInputStream(FileName);
DataInputStream dis = new DataInputStream(fis);
br = new BufferedReader(new InputStreamReader(dis));
}
catch (FileNotFoundException e)
{
System.out.print("FILE DOESN'T EXIST");
}
finally
{
fis.close();
dis.close();
br.close();
}
String input;
int count = 0;
try
{
while ((input = br.readLine()) != null)
{
count++;
}
}
catch (IOException e)
{
System.out.print("I/O STREAM EXCEPTION");
}
return count;
}
For some reason, I cannot close any I/O objects. fis.close(), dis.close(), br.close() all give me cannot find symbol even though I imported all the I/O library (import java.io.*;) and initiated all the objects.
In the above program, "Cannot find symbol" error will occur because “sum” is not declared. In order to solve the error, we need to define “int sum = n1+n2” before using the variable sum.
The “cannot find symbol” error comes up mainly when we try to use a variable that's not defined or declared in our program. When our code compiles, the compiler needs to verify all the identifiers we have. The error “cannot find symbol” means we're referring to something that the compiler doesn't know about.
Misspelled method name Misspelling an existing method, or any valid identifier, causes a cannot find symbol error. Java identifiers are case-sensitive, so any variation of an existing variable, method, class, interface, or package name will result in this error, as demonstrated in Fig.
“cannot resolve symbol” means that the Java compiler cannot locate a symbol referenced in your Java source code. The causes for this common error include: A missing class file. A faulty CLASSPATH. A symbol name is mispelled or miscapitalized.
BufferedReader br = null;
FileInputStream fis =null;
DataInputStream dis null;
try {
fis = new FileInputStream(FileName);
dis = new DataInputStream(fis);
br = new BufferedReader(new InputStreamReader(dis));
}
Put them out of your try block
, so that your finally block can see the variables.
You have to declare all the streams outside the try
block, otherwise they won't be visible in the finally
block:
FileInputStream fis = null;
DataInputStream dis = null;
BufferedReader br = null;
Alternatively, you could use Java 7's new try-with-resources syntax to automate resource closing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With