我正在学习 Java(对不起,我的英语很差,这不是我的母语),当我在 Eclipse(JavaSE-1.7)中执行“try-finally”块时,在我输入的每个“尝试”中都会出现以下消息:
此行有多个标记 - 语法错误,插入“}”以完成 Block - 语法错误,插入“Finally”以完成 BlockStatements
这是完整的代码:
package Java;
public class Arquivo3 {
private Path BdC = Paths.get("C:/xti/files/conta.txt");
private Charset utf8 = StandardCharsets.UTF_8;
public void armazenarContas(ArrayList<Conta> contas) throws IOException{
try(BufferedWriter writer = Files.newBufferedWriter(BdC, utf8)) {
for (Conta conta : contas) {
writer.write(conta.getCliente() + ";" + conta.getSaldo() + "\n");
}
}
}
public ArrayList<Conta> recuperarContas() throws IOException{
ArrayList<Conta> contas = new ArrayList<Conta>();
try (BufferedReader reader = Files.newBufferedReader(BdC, utf8)){
String line = null;
while((line = reader.readLine()) != null) {
String[] t = line.split(";");
Conta conta = new Conta(t[0], Double.parseDouble(t[1]));
contas.add(conta);
}
}finally {
return contas;
}
}
public static void main(String[] args) throws IOException{
/*
ArrayList<Conta> contas = new ArrayList<Conta>();
contas.add(new Conta("Ricardo", 12000.23));
contas.add(new Conta("Lawrence", 11050.32));
contas.add(new Conta("Sandra", 18000.21));
contas.add(new Conta("Beatriz", 23200.09));
*/
Arquivo3 operacao = new Arquivo3();
//operacao.armazenarContas(contas);
ArrayList<Conta> contas2 = operacao.recuperarContas();
for (Conta conta : contas2) {
conta.exibeSaldo();
}
}
}