简体   繁体   中英

BufferedWriter not writing in Java

I am trying to call a method from a different class, so it can appear in my bufferwriter. My code is below:

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- Client class `

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();
}       

I am trying to get the Basic Info method from the class Client appeaing in the Bufferwriter, but what happens is that it doesnt write anything it is just blank , if I take out the BasicInfo from the bufferwriter then everything is written and displays in the note perfectly but if I add it in nothing appears in the txt file.

Please make sure the absolute file path your provided is correct. The issue you have is Java cannot recognize the file path you use.

Change your file format:

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

I tested your BufferedWriter & FileWriter , it's working.

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();
        }
    }

Output is:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM