首先,我知道我应该使用带资源的try-catch,但是我目前没有在我的系统上使用最新的JDK。Try-Catch-Finally - Final Block无法识别变量
我有下面的代码,并试图确保资源阅读器关闭使用finally块,但下面的代码不能编译的原因有两个。首先,读者可能还没有被初始化,其次close()应该在自己的try-catch中被捕获。这两个原因都不是击败了初始try-catch块的对象吗?
我可以通过将它放在自己的try-catch中来解决finally块close()语句的问题。然而这还留下关于读者未被初始化的编译错误?
我假设我出事了吗?帮助赞赏!
干杯,
public Path [] getPaths()
{
// Create and initialise ArrayList for paths to be stored in when read
// from file.
ArrayList<Path> pathList = new ArrayList();
BufferedReader reader;
try
{
// Create new buffered read to read lines from file
reader = Files.newBufferedReader(importPathFile);
String line = null;
int i = 0;
// for each line from the file, add to the array list
while((line = reader.readLine()) != null)
{
pathList.add(0, Paths.get(line));
i++;
}
}
catch(IOException e)
{
System.out.println("exception: " + e.getMessage());
}
finally
{
reader.close();
}
// Move contents from ArrayList into Path [] and return function.
Path pathArray [] = new Path[(pathList.size())];
for(int i = 0; i < pathList.size(); i++)
{
pathArray[i] = Paths.get(pathList.get(i).toString());
}
return pathArray;
}
Excatly,看起来像无意义的代码生成:S感谢帮助解决初始化问题。 – Dave0504