[英]BufferedWriter not writing in Java
我正在尝试从其他类调用方法,因此它可以出现在我的bufferwriter中。 我的代码如下:
public void Deposit(double amount) {
Bank bank = new Bank();
ArrayList<Client> customers = bank.getCustomers(); // Gets Customer Info from Bank
if (amount <= 0) {
System.err.println("You can not deposit that");
return;
} else {
checkInterest(0); // resets interest rates
amount = amount + amount * interest; //Applies interest to deposited amount
balance += amount; // Balance is == amount
System.out.println("You have deposited £" + amount + "Interest Rate of " + (interest * 100) + "%");
System.out.println("You now have a balance of £" + balance);
}
try {
FileWriter ac = new FileWriter("D:\\programming\\Java\\JavaBanking\\Transactions.txt", true);
BufferedWriter out = new BufferedWriter(ac);
String s = String.format("You have deposited the following amount:%n" + "£" + String.valueOf(amount) + "%nIn the account number:%n" +
getAccountNumber() + "%nAt: " + LocalDateTime.now() + "%nYour current balance is: £" + balance + "%n" + toString());
out.write(s);
//fw.write(t);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
BasicInfor-客户端类
public class Client {
private Object fullName;
private Account account;
public Client(String fullName, Account account) { // Passes in First Name and Account Type
// TODO Auto-generated constructor stub
this.fullName = fullName; // Creates Fields
this.account = account; // Adds account to Customers
}
}
public String BasicInfo() { //Return
return "FullName: " + fullName + "\n" +
account + "Sort Code :" + SortCode();
}
我正在尝试从Bufferwriter中出现的Client类获取Basic Info方法,但是发生的是它没有写任何东西,只是空白,如果我从bufferwriter中取出BasicInfo,那么所有内容都会写入并显示在注释中完美,但是如果我将其添加到txt文件中,则什么也不会出现。
请确保您提供的绝对文件路径正确。 您遇到的问题是Java无法识别您使用的文件路径。
更改文件格式:
String path = "D:\\programming\\Java\\JavaBanking\\Transactions.txt";
path = path.replaceAll("\\", "/");
FileWriter ac = new FileWriter(path,true);
我测试了您的BufferedWriter
和FileWriter
,它正在工作。
public class Main{
public static void main(String args[]) throws IOException{
FileWriter ac = new FileWriter("/Users/haifzhan/Transactions.txt",true);
BufferedWriter out = new BufferedWriter(ac);
String s = String.format("You have deposited the following amount:%n" + "£" + String.valueOf(13) + "%nIn the account number:%n"+
1 + "%nAt: " + LocalDateTime.now() +"%nYour current balance is: £" + 333 +"%n" + "ddd" );
System.out.println(s);
out.write(s);
out.close();
}
}
输出为:
You have deposited the following amount:
£13
In the account number:
1
At: 2016-03-24T12:38:19.822
Your current balance is: £333
ddd
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.