[英]Can't close my file
我使用Formatter创建了文件,当我尝试关闭它的finally块时,它说实例变量尚未初始化,但是如果我在try块中将其关闭,它可以工作,但是我不想这样做。 另外,请注意,我也不知道我是否正确使用了我的异常,因为我不知道它们是什么两个。
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.");
}
这段代码为我编译(我不得不捏造Account类):
public class NewFile
{
public static void main(String[] args)
{
Formatter file = null;
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 (final FileNotFoundException fileNotFoundException)
{
System.err.println("File not found.");
}
catch (final SecurityException securityException)
{
System.err.println("Do not have required permission.");
}
catch (final FormatterClosedException formatterClosedException)
{
System.err.println("File is already closed.");
}
catch (final IllegalStateException illegalStateException)
{
System.err.println("Error reading from file.");
}
finally
// close the file
{
if (file != null)
{
file.close();
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.