2016-03-24 8 views
0

別のクラスのメソッドを呼び出そうとしているので、バッファライタに表示される可能性があります。私のコードは以下の通りです:BufferedWriterがJavaで書かれていない

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にappeaingクラスのクライアントからの基本情報方法を取得しようとしているが、何が起こるかはそれということですしています私がBasicInfoをバッファライタから取り出した場合、すべてが書き込まれ、メモに完全に表示されますが、何も追加しないと、txtファイルには表示されません。

+1

。 –

+0

バッファは何も実行しませんが、書き込みは1回しか実行されません。 –

+1

コードが間違っていると思われます。私はあなたのデバッガのコードをステップ実行し、 's'の内容を確認します –

答えて

0

指定した絶対パスが正しいことを確認してください。あなたが持っている問題は、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(); 
     } 
    } 

出力は、次のとおりです。あなたがする必要はありません3kings @

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 
関連する問題