2015-09-06 1 views
1

Я начинаю, и у меня проблема с отправкой выходов в файл csci.txt. Я могу видеть все выходы в моей консоли, и он отлично работает (я думаю), но как я могу напечатать точные результаты в файле csci.txt?Как распечатать все выходы в txt-файле

Адрес Driver class. main()

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

} 

ответ

0

Здесь Hacky раствор

первый, возьмите stdout

final PrintStream stdout=System.out; 

затем у вас есть собственный писатель и ПОМНИТЕ, чтобы переопределить все необходимые методы (т. print(f:float):void) здесь я только что внедрил print(x:string):void и println(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 тоже с тем же раствором.

ЗАПОМНИТЕ снова, переопределите все методы, которые вам нравятся (print и write методов) в вашем писателе, как образец.

 Смежные вопросы

  • Нет связанных вопросов^_^