Java - 两个缓冲写入器
Java - Two buffered writers
大家好,我的 BufferedWriter 一直有问题。起初我只有 1 个缓冲写入器 1 显示为 cc
并且这与输出一起完美地工作但是现在我已经尝试实现 2 我继续在
其他地方出现错误
Multiple markers at this line
- Syntax error, insert "}" to complete Statement
- Syntax error, insert "Finally" to complete
这是代码。错误从 }else if (command.equals("action 2"))
行开始
if (selected) {
if (command.equals("action1")) {
BufferedWriter aa;
try {
File writerB = new File("output1.txt"); //set transaction-list.txt to be the destination file when writeTransactions is used
if (!writerB.exists()) {
writerB.createNewFile();
}
FileWriter bb = new FileWriter(writeBalance, true); //filewriter
aa = new BufferedWriter(bb); // bufferedwriter
BufferedWriter cc;
try {
int x=0;
File writerT = new File("output2.txt"); //set transaction-list.txt to be the destination file when writeTransactions is used
if (!writerT.exists()) {
writerT.createNewFile();
}
FileWriter dd = new FileWriter(writeTransactions, true); //filewriter
cc = new BufferedWriter(dd); // bufferedwriter
String newLine = System.getProperty("line.separator"); //creates a line separator which will be used with string newLine
if (n==0) {
bw.write(DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(now) + newLine);
wb.write(c);}
bw.close;
wb.close}
}else if (command.equals("action 2"))
您没有在 try
之后制作 catch
块 - 在这两种情况下都没有。在 try
.
之后 catch
或 finally
块是 强制性的
如前所述,强烈推荐 Java 7 中引入的 try-with-resources 语句。
你没有妥善处理你的资源;一方面,它们应该在 finally
块中关闭...
... 然而,还有更好的方法,那就是使用 try-with-resources 语句,以及新的 java.nio.file API:
final Path file1 = Paths.get("output1.txt");
final Path file2 = Paths.get("output2.txt");
try (
final BufferedWriter writer1 = Files.newBufferedWriter(file1, StandardCharsets.UTF_8,
StandardOpenOption.CREATE, StandardOpenOption.APPEND);
final BufferedWriter writer2 = Files.newBufferedWriter(file2, StandardCharsets.UTF_8,
StandardOpenOption.CREATE, StandardOpenOption.APPEND);
) {
// use writer1 and writer2 here.
// Note that BufferedWriter has a .newLine() method as well.
}
大家好,我的 BufferedWriter 一直有问题。起初我只有 1 个缓冲写入器 1 显示为 cc
并且这与输出一起完美地工作但是现在我已经尝试实现 2 我继续在
Multiple markers at this line
- Syntax error, insert "}" to complete Statement
- Syntax error, insert "Finally" to complete
这是代码。错误从 }else if (command.equals("action 2"))
行开始
if (selected) {
if (command.equals("action1")) {
BufferedWriter aa;
try {
File writerB = new File("output1.txt"); //set transaction-list.txt to be the destination file when writeTransactions is used
if (!writerB.exists()) {
writerB.createNewFile();
}
FileWriter bb = new FileWriter(writeBalance, true); //filewriter
aa = new BufferedWriter(bb); // bufferedwriter
BufferedWriter cc;
try {
int x=0;
File writerT = new File("output2.txt"); //set transaction-list.txt to be the destination file when writeTransactions is used
if (!writerT.exists()) {
writerT.createNewFile();
}
FileWriter dd = new FileWriter(writeTransactions, true); //filewriter
cc = new BufferedWriter(dd); // bufferedwriter
String newLine = System.getProperty("line.separator"); //creates a line separator which will be used with string newLine
if (n==0) {
bw.write(DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(now) + newLine);
wb.write(c);}
bw.close;
wb.close}
}else if (command.equals("action 2"))
您没有在 try
之后制作 catch
块 - 在这两种情况下都没有。在 try
.
catch
或 finally
块是 强制性的
如前所述,强烈推荐 Java 7 中引入的 try-with-resources 语句。
你没有妥善处理你的资源;一方面,它们应该在 finally
块中关闭...
... 然而,还有更好的方法,那就是使用 try-with-resources 语句,以及新的 java.nio.file API:
final Path file1 = Paths.get("output1.txt");
final Path file2 = Paths.get("output2.txt");
try (
final BufferedWriter writer1 = Files.newBufferedWriter(file1, StandardCharsets.UTF_8,
StandardOpenOption.CREATE, StandardOpenOption.APPEND);
final BufferedWriter writer2 = Files.newBufferedWriter(file2, StandardCharsets.UTF_8,
StandardOpenOption.CREATE, StandardOpenOption.APPEND);
) {
// use writer1 and writer2 here.
// Note that BufferedWriter has a .newLine() method as well.
}