gpt4 book ai didi

java - "cannot find symbol"试图关闭 I/O 对象时

转载 作者:搜寻专家 更新时间:2023-10-31 19:47:27 25 4
gpt4 key购买 nike

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 对象。 fis.close()、dis.close()、br.close() 都给我找不到符号,即使我导入了所有 I/O 库(导入 java.io.*;)并启动了所有对象。

最佳答案

 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 block 可以看到变量。

关于java - "cannot find symbol"试图关闭 I/O 对象时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15281640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com