在 try 块中声明的 finally 块中关闭 Scanner/PrintStream
Closing Scanner/PrintStream in finally-block which are declared in try-block
我试图在 finally 块中关闭 Scanner 和 PrintStream,它们在 try 块中声明。问题是,如果 try-block 由于异常而失败,则永远不会声明 Scanner 和 Printstream,从而导致 finally-block 中出现错误,我想在其中关闭它们。这是代码:
try {
File readFile = new File(readFileName);
File writeFile = new File(writeFileName);
Scanner fileScanner = new Scanner(readFile);
PrintStream output = new PrintStream(new FileOutputStream(writeFile,false)); // overwrite
while(fileScanner.hasNextLine()) {
output.println(fileScanner.nextLine());
if (!fileScanner.hasNextLine()) {
break;
}
output.println();
}
}
catch (FileNotFoundException fnfe) {
System.out.println(fnfe.getMessage());
System.exit(0);
}
finally {
fileScanner.close();
output.close();
}
编辑:感谢您的回答,我通过在 try 块 之前 声明 Scanner 和 Printstream 然后在 try 中初始化它们来解决它而无需 try-with-resources -块,像这样:
Scanner fileScanner = null;
PrintStream output = null;
try {
fileScanner = new Scanner(readFile);
output = new PrintStream(new FileOutputStream(writeFile,false));
...
}
您可以使用 try-with-resources 构造:
File readFile = new File(readFileName);
File writeFile = new File(writeFileName);
try (Scanner fileScanner = new Scanner(readFile); PrintStream output = new PrintStream(new FileOutputStream(writeFile,false))) {
while(fileScanner.hasNextLine()) {
output.println(fileScanner.nextLine());
if (!fileScanner.hasNextLine()) {
break;
}
output.println();
}
} catch (FileNotFoundException fnfe) {
System.out.println(fnfe.getMessage());
System.exit(0);
}
由于Scanner
和PrintStream
都实现了Autocloseable
接口,它们将在异常或执行try
块
后自动关闭
我试图在 finally 块中关闭 Scanner 和 PrintStream,它们在 try 块中声明。问题是,如果 try-block 由于异常而失败,则永远不会声明 Scanner 和 Printstream,从而导致 finally-block 中出现错误,我想在其中关闭它们。这是代码:
try {
File readFile = new File(readFileName);
File writeFile = new File(writeFileName);
Scanner fileScanner = new Scanner(readFile);
PrintStream output = new PrintStream(new FileOutputStream(writeFile,false)); // overwrite
while(fileScanner.hasNextLine()) {
output.println(fileScanner.nextLine());
if (!fileScanner.hasNextLine()) {
break;
}
output.println();
}
}
catch (FileNotFoundException fnfe) {
System.out.println(fnfe.getMessage());
System.exit(0);
}
finally {
fileScanner.close();
output.close();
}
编辑:感谢您的回答,我通过在 try 块 之前 声明 Scanner 和 Printstream 然后在 try 中初始化它们来解决它而无需 try-with-resources -块,像这样:
Scanner fileScanner = null;
PrintStream output = null;
try {
fileScanner = new Scanner(readFile);
output = new PrintStream(new FileOutputStream(writeFile,false));
...
}
您可以使用 try-with-resources 构造:
File readFile = new File(readFileName);
File writeFile = new File(writeFileName);
try (Scanner fileScanner = new Scanner(readFile); PrintStream output = new PrintStream(new FileOutputStream(writeFile,false))) {
while(fileScanner.hasNextLine()) {
output.println(fileScanner.nextLine());
if (!fileScanner.hasNextLine()) {
break;
}
output.println();
}
} catch (FileNotFoundException fnfe) {
System.out.println(fnfe.getMessage());
System.exit(0);
}
由于Scanner
和PrintStream
都实现了Autocloseable
接口,它们将在异常或执行try
块