0

Given my code:

import java.util.Scanner;

public class AccountTest {

    public static void main(String[] args) {
        Account account1 = new Account(50.00);
        Account account2 = new Account(0.00);

        System.out.printf("account1 balance: $%.2f\n", account1.getBalance());
        System.out.printf("account2 balance: $%.2f\n\n", account2.getBalance());

        Scanner input = new Scanner(System.in);

        System.out.print("Enter withdrawal amount for account1: ");

        double withdrawalAmount = input.nextDouble();

        System.out.printf("\nsubtracting %.2f from account1 balance\n",
                withdrawalAmount);

        account1.debit(withdrawalAmount);

        System.out.printf("account1 balance: $%.2f\n", account1.getBalance());
        System.out.printf("account2 balance: $%.2f\n\n", account2.getBalance());

        System.out.print("Enter withdrawal amount for account2: ");
        withdrawalAmount = input.nextDouble();
        System.out.printf("\nsubtracting %.2f from account2 balance\n",
                withdrawalAmount);
        account2.debit(withdrawalAmount);

        System.out.printf("account1 balance: $%.2f\n", account1.getBalance());
        System.out.printf("account2 balance: $%.2f\n", account2.getBalance());
    }
}

How can I get my "System.out.printf" dumped into a file (dumped as in not erasing the file's content)? Or perhaps creating separate files for each instance. Any help is appreciated beginner here. Thanks.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Alexander
  • 480
  • 2
  • 5
  • 21
  • 3
    Already an answer. http://stackoverflow.com/questions/1994255/how-to-write-console-output-to-a-txt-file?rq=1 – Kaliber64 Feb 16 '13 at 03:02
  • If I am not mistaken that will read an input line then save to text file. Note that in my code I do not intend to input anything at all on the console. – Alexander Feb 16 '13 at 03:07
  • @user2077545 Check out the top rated answer in that question: http://stackoverflow.com/a/1994283/1211906 – Marc Baumbach Feb 16 '13 at 03:11
  • Yeah but that just replaces the file's content which I was hoping to avoid. Not to mention that all my lines are smashed together in the file. – Alexander Feb 16 '13 at 03:23

2 Answers2

2

1) you can redirect stdout to a file when running your program

java AccountTest >> test.txt 

2) you can reassign stdout at the beginning of your program

    PrintStream out = new PrintStream(new FileOutputStream("test.txt", true));
    System.setOut(out);

3) you can use java.io.PrintWriter instead of System.out

    PrintWriter out = new PrintWriter(new FileWriter("test.txt", true));
    out.printf( "account1 balance: $%.2f\n", account1.getBalance() );
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
1

It depends on how you run this program. If you have a POSIX shell at hand, you can just redirect its standard output to a file.

The following would append to a log file:

$ java -jar myjar.jar >> log

this one would create a new epoch-dated log file each time (the $(…) might be a bash-specific feature, I'm not sure):

$ java -jar myjar.jar > log-$(date +%s)
Nowhere man
  • 5,007
  • 3
  • 28
  • 44