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;
}
由于某种原因,我无法关闭任何I / O对象。即使我导入了所有的I / O库(导入java.io. *;)并启动了所有对象,fis.close(),dis.close(),br.close()都给我找不到符号。
答案 0 :(得分:5)
BufferedReader br = null;
FileInputStream fis =null;
DataInputStream dis null;
try {
fis = new FileInputStream(FileName);
dis = new DataInputStream(fis);
br = new BufferedReader(new InputStreamReader(dis));
}
将它们从try block
中取出,以便你的finally块可以看到变量。
答案 1 :(得分:1)
您必须在try
块之外声明所有流,否则它们将无法在finally
块中显示:
FileInputStream fis = null;
DataInputStream dis = null;
BufferedReader br = null;
或者,您可以使用Java 7的新try-with-resources语法来自动关闭资源。
答案 2 :(得分:1)
你的对象在你的块中是空的
FileInputStream fis =null;
DataInputStream dis null;
在块之外定义它们
try{
fis = new FileInputStream(FileName);
dis = new DataInputStream(fis);
}
答案 3 :(得分:0)
你已经将fis
限定在try块中,所以决赛并不知道它在那里。尝试:
FileInputStream fis;
DataInputStream dis;
try
{
fis = new FileInputStream(FileName);
dis = new DataInputStream(fis);
br = new BufferedReader(new InputStreamReader(dis));
}
答案 4 :(得分:0)
试试这个:
BufferedReader br = null;
FileInputStream fis = null;
DataInputStream dis = null;
try
{
fis = new FileInputStream(FileName);
dis = new DataInputStream(fis);
br = new BufferedReader(new InputStreamReader(dis));
}