我试图让我的if语句有单独的写命令,例如bw.write(b + " - " + a +" = " + c + newLine);
问题是由于newLine字符串和bw
缓冲区在curley braces内我不能让那些工作。任何想法?
try {
File file = new File("src/written.txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fw);
String newLine = System.getProperty("line.separator");
bw.write(b + " - " + a +" = " + c + newLine);
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
JOptionPane.showMessageDialog(null,"Amount Transfered: " + b + " - " + "Previous Balance: " + a +"\nRemaning Balance: " + c);
if( c == 0){
JOptionPane.showMessageDialog(null,"Transfered granted. Balance empty","Transation successful!",JOptionPane.WARNING_MESSAGE);
}else if (c > 0){
JOptionPane.showMessageDialog(null,"Transfered granted.","Transation successful!",JOptionPane.INFORMATION_MESSAGE);
}else{
JOptionPane.showMessageDialog(null,"Transfer denied due to insufficent funds.","Transaction denied!",JOptionPane.ERROR_MESSAGE);
}
答案 0 :(得分:1)
我不完全确定你的目标是什么,但看起来你正在失去BufferedWriter bw的范围。
如果你想在目前居住的{}之外使用bw,请声明:
BufferedWriter bw;
try {
.....
bw = new BufferedWriter(fw);
}
这样,在try块之前声明了bw,这使得你可以在整个代码以及try块中使用它。