我使用Formatter来创建我的文件,当我尝试关闭它时,finally块表示实例变量还没有被初始化,但是如果我在我的try块中关闭它,它会起作用,但是我没有&#39 ;我想这样做。另外,请注意,我不知道我是否正确使用了我的例外情况,因为我不知道它们中的两个是什么。
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();
}
}
}
}
这是错误: 变量文件可能尚未初始化
答案 0 :(得分:1)
只需将final Formatter file;
更改为
Formatter file = null;
请注意,我不相信可以final
这样做,但是这样,编译器会看到文件变量已初始化为某些内容。
然后在最后,检查null:
finally {
if (file != null) {
file.close();
}
}
修改强>
或者按照MadProgrammer,使用Java 7尝试使用资源。
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.");
}
答案 1 :(得分:0)
此代码为我编译(我必须捏造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();
}
}
}
}