gpt4 book ai didi

java - 无法关闭我的文件

转载 作者:太空宇宙 更新时间:2023-11-04 06:39:31 25 4
gpt4 key购买 nike

我使用 Formatter 创建我的文件,当我尝试关闭它时,finally block 它说实例变量尚未初始化,但是如果我在 try block 中关闭它,它会起作用,但我不想这样做。另外,附注,我也不知道我是否正确使用了异常,因为我不知道其中两个是什么。

public class NewFile
{
public static void main(String[] args)
{

final Formatter file;

try// create file
{
file = new Formatter("Records.txt");

Account[] records = new Account[4];

records[ 0 ] = new Account( 100, "January", "Smith", 34.56 );
records[ 1 ] = new Account( 200, "Sally", "Anderson", 467.10 );
records[ 2 ] = new Account( 300, "Joe", "Wright", -67.60 );
records[ 3 ] = new Account( 400, "Henry", "Hein", 0.00 );


for(Account display : records)
{
file.format("\n %d %s %s %,.2f\n", display.getAccount(),display.getFirst(), display.getLast(), display.getBalance());
}
}

catch(FileNotFoundException fileNotFoundException)
{
System.err.println("File not found.");
}
catch(SecurityException securityException)
{
System.err.println("Do not have required permission.");
}

catch(FormatterClosedException formatterClosedException)
{
System.err.println("File is already closed.");
}

catch(IllegalStateException illegalStateException)
{
System.err.println("Error reading from file.");
}

finally//close the file
{
file.close();
}
}

好的,这是我在评论后得到的:

import java.util.Formatter;
import java.io.FileNotFoundException;
import java.lang.IllegalStateException;
import java.lang.SecurityException;
import java.util.FormatterClosedException;

public class NewFile
{
public static void main(String[] args)
{

Formatter file;

try// create file
{
file = new Formatter("Records.txt");

Account[] records = new Account[4];

records [ 0 ] = new Account( 100, "January", "Smith", 34.56 );
records[ 1 ] = new Account( 200, "Sally", "Anderson", 467.10 );
records[ 2 ] = new Account( 300, "Joe", "Wright", -67.60 );
records[ 3 ] = new Account( 400, "Henry", "Hein", 0.00 );


for(Account display : records)
{
file.format("\n %d %s %s %,.2f\n", display.getAccount(),display.getFirst(), display.getLast(), display.getBalance());
}
}

catch(FileNotFoundException fileNotFoundException)
{
System.err.println("File not found.");
}
catch(SecurityException securityException)
{
System.err.println("Do not have required permission.");
}

catch(FormatterClosedException formatterClosedException)
{
System.err.println("File is already closed.");
}

catch(IllegalStateException illegalStateException)
{
System.err.println("Error reading from file.");
}

finally//close the file
{
if(file != null)
{
file.close();
}
}
}
}

这是错误:变量文件可能尚未初始化

最佳答案

只需更改 final Formatter file;

Formatter file = null;

请注意,我不相信这可以是 final为此,编译器将看到文件变量将被初始化为某些内容。

然后在最后检查是否为 null:

finally {
if (file != null) {
file.close();
}
}
<小时/>

编辑
或者根据 MadProgrammer,使用 Java 7 的 try 资源。

try (Formatter file = new Formatter("Records.txt")) {
// do stuff with file here

} catch(FileNotFoundException fileNotFoundException) {
System.err.println("File not found.");
} catch(SecurityException securityException) {
System.err.println("Do not have required permission.");
} catch(FormatterClosedException formatterClosedException) {
System.err.println("File is already closed.");
} catch(IllegalStateException illegalStateException) {
System.err.println("Error reading from file.");
}

关于java - 无法关闭我的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24875188/

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