[英]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.