BufferedWriter不是用Java编写的

时间:2016-03-24 18:06:12

标签: java filewriter bufferedwriter

我正在尝试从不同的类调用一个方法,因此它可以出现在我的缓冲区中。我的代码如下:

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方法,但是如果我从缓冲区写入器中取出BasicInfo然后写入并显示所有内容,它就不会写任何空白的内容。在笔记中完美但如果我添加它没有出现在txt文件中。

1 个答案:

答案 0 :(得分:0)

请确保您提供的绝对文件路径正确无误。您遇到的问题是Java无法识别您使用的文件路径。

更改文件格式:

String path = "D:\\programming\\Java\\JavaBanking\\Transactions.txt";
path = path.replaceAll("\\", "/");
FileWriter ac = new FileWriter(path,true);

我测试了你的BufferedWriter&amp; 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