如何将所有输出打印到txt文件

时间:2015-09-06 01:05:33

标签: java file object output printwriter

我是初学者,我遇到了将输出发送到csci.txt文件的问题。我可以在我的控制台中看到所有输出并且它工作正常(我认为)但是如何将完全相同的输出打印到csci.txt文件中?

这是Driver classmain()

import java.io.*;

public class Driver {
    public static void main (String[] args) throws IOException{
        int choice;
        PrintWriter pw = new PrintWriter(new FileWriter("csis.txt"));
        Decimal dec = new Decimal(pw);
        //Binary bin = new Binary(pw);
        //Hexadecimal hex = new Hexadecimal(pw);
        Menu menu = new Menu(pw);

        do {
            menu.display();
            choice = menu.getSelection();
            switch (choice) {
                case 1: dec.decToBin(); break;
                case 2: dec.decToHex(); break;
                //case 3: bin.binToDec(); break;
                //case 4: bin.binToHex(); break;
                //case 5: hex.hexToDec(); break;
                //case 6: hex.hexToBin(); break;
            }
        }while (choice != 7);

        pw.close();
    }
}

以下是Menu class

import java.util.Scanner;
import java.io.*;

public class Menu {
    Scanner kb = new Scanner(System.in);
    int choice;

    public PrintWriter pw;
    public Menu(PrintWriter pw) {
        this.pw = pw;
    }
    public void display(){
        System.out.println("Hello, there.");
        System.out.println("1: Decimal to Binary.");
        System.out.println("2: Decimal to Hexadecimal.");
        System.out.println("3: Binary to Decimal.");
        System.out.println("4: Binary to Hexadecimal.");
        System.out.println("5: Hexadecimal to Decimal.");
        System.out.println("6: Hexadecimal to Binary.");
    }   

    public int getSelection() {
        System.out.println("Type in a choice number.");
        int input = kb.nextInt();
        choice = input;
        Decimal dec = new Decimal(pw);
        switch(choice){
        case 1: dec.inputDecBin(); dec.decToBin(); break;
        case 2: dec.inputDecHex(); dec.decToHex(); break;

        }
        return 0;
    }
}

以下是Decimal class

import java.util.Scanner;
import java.io.*;

public class Decimal{
    private PrintWriter pw;
    public Decimal(PrintWriter pw){
        this.pw = pw;
    }
    Scanner kb = new Scanner(System.in);
    int inDec;  
    int num;
    int rem;//remainder




    //Allows user to type in a positive decimal number
    public void inputDecBin(){
        System.out.println("Enter a positive decimal number. I will convert into a binary number.");
        inDec = kb.nextInt();
        num = inDec;
    }

    //converts decimal into binary
    public void decToBin(){
        if (num < 0){
            System.out.println("Error. You should put a positive decimal number.");
        }
        else if (num == 0){
            System.out.println("0000 0000 0000 0000 0000 0000 0000");
        }
        else{
        int binary[] = new int[32];
        for(int j=0; num>0; j++){
            binary[j] = num%2;
            num = num/2;
        } 
        for(int j=31; j>=0; j--){
            System.out.print(binary[j]);
                if(j%4==0)
                    System.out.print(" ");
        }
        System.out.print("\n");
        }
    }


    //Allows user to type in a positive decimal number
    public void inputDecHex(){
        System.out.println("Enter a positive decimal number. I will convert into a hexadecimal number.");
        inDec = kb.nextInt();
        num = inDec;
    }

    //converts decimal into hexadecimal
    public void decToHex(){
         String str=""; 
         char hex[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

         if(num < 0)
             System.out.println("Error. You should put a positive decimal number.");
         else if(num==0)
             System.out.println("0");
         while(num>0)
         {
           rem=num%16; 
           str=hex[rem]+str; 
           num=num/16;
         }
         System.out.println(str);
    }

}

1 个答案:

答案 0 :(得分:0)

这是hacky解决方案

首先,抓住stdout

final PrintStream stdout=System.out;

然后有你自己的作家,并记住覆盖所有必需的方法(即print(f:float):void),我刚刚在你的代码中实现了print(x:string):voidprintln(x:String):void

PrintStream ps=new PrintStream(new File("/home/<<taget_file>>")){
        @Override public void println(String x){
            stdout.println(x);//std-out
            super.println(x); //file
        }
        @Override public void print(String x){
            stdout.print(x);
            super.print(x);
        }
    };

最后将stdout重定向到您的作者(注册)

System.setOut(ps);

一个例子

public static void main(String[] args)throws Exception{
    final PrintStream stdout=System.out;
    PrintStream ps=new PrintStream(new File("~/__test.txt")){
        @Override public void println(String x){
            stdout.println(x); super.println(x);
        }
        @Override public void print(String x){
            stdout.print(x); super.print(x);
        }
    };
    System.setOut(ps);
    System.out.println("Hi dude.");
}

您也可以使用相同的解决方案设置stderr

再次记住,在你的作家中覆盖你喜欢的所有方法(printwrite方法),例如样本。