java 将控制台输出到文本文件中?- 爪哇

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14906458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:48:22  来源:igfitidea点击:

Output console into text file? - Java

javaconsoletext-files

提问by Alexander

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.

如何将我的“System.out.printf”转储到文件中(转储为不擦除文件内容)?或者可能为每个实例创建单独的文件。任何帮助都感谢初学者在这里。谢谢。

回答by Evgeniy Dorofeev

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

1)您可以在运行程序时将标准输出重定向到文件

java AccountTest >> test.txt 

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

2)您可以在程序开始时重新分配标准输出

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

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

3) 你可以使用 java.io.PrintWriter 而不是 System.out

    PrintWriter out = new PrintWriter(new FileWriter("test.txt", true));
    out.printf( "account1 balance: $%.2f\n", account1.getBalance() );

回答by Nowhere man

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.

这取决于你如何运行这个程序。如果您手头有 POSIX shell,您可以将其标准输出重定向到一个文件。

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):

这个每次都会创建一个新的纪元日志文件(这$(…)可能是一个特定于 bash 的功能,我不确定):

$ java -jar myjar.jar > log-$(date +%s)