0

I would like to know how to write all lines from the java output in a .txt file.
I've done some tests so far but I don't seem to be able to find the solution :/
Here is a small code, if you could show me with this one, it would be greatly appreciated :
The code shown below asks the user what to write in a .txt file but I want it to write all the printed lines in a .txt file without asking the user anything. Thank you

package test;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;


public class Test {

    public static void main(String[] args)throws Exception {
        System.out.println("Hello");
        System.out.println("Hi");
        System.out.println("Hola");
        System.out.println("Bonjour");
        System.out.println("Hallo");
        System.out.println("Hej");
        System.out.println("Alo");
        System.out.println("Ciao");
        writeOutput();

    }
    public static void writeOutput() throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String lineFromInput = in.readLine();
        PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
        System.setOut(out);
        out.println(lineFromInput);
        out.close();
    }

}
Arsen Davtyan
  • 1,891
  • 8
  • 23
  • 40
Adriann
  • 73
  • 9

2 Answers2

1

Use directly PrintStream to write the String values.

public static void main(String[] args)throws Exception {          
    PrintStream printStream = new PrintStream(new File("output.txt"));
    // hook for closing the stream 
    Runtime.getRuntime().addShutdownHook(new Thread(printStream::close));
     // writing
    write(printStream,"Hello", "Hi", "Hola", "Bonjour", "Hallo", "Hej",
     "Alo","Ciao");    
     // writing again
    write(printStream, "A new String", "And again another one...");        
}


public static void write(PrintStream printStream, String... values) throws Exception {

  try{                 
     for (String value : values){
       printStream.println(value);
     }
     printStream.flush();
   }
    catch (Exception e){
       // handling exception
   }       
 }

}
davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • Oh wow, thanks , but is there any way to make it with a separate method ? – Adriann Dec 13 '16 at 21:24
  • You are welcome. Of course. But why ? Is that method should take parameters ? For example a vargs of String to write in the file. – davidxxx Dec 13 '16 at 21:25
  • BTW: I'd recommend doing `Runtime.getRuntime().addShutdownHook(new Thread(out::close));` instead of manually closing the `PrintStream` at the end of the `main`, since this also deals with multiple threads/exceptions not caused by the file not being found thrown before the end of the main. – fabian Dec 13 '16 at 21:27
  • What if there is other methods that print something in the main method, how would that text go in a .txt file ? – Adriann Dec 13 '16 at 21:32
  • @fabian interesting idea. I didn't know it. It is rarely used. Am I wrong ? – davidxxx Dec 13 '16 at 21:38
  • @Adriann In this case, you could indeed extract a method to print. I updated the code – davidxxx Dec 13 '16 at 21:50
0

java test.Test > somefile.txt

Lew Bloch
  • 3,364
  • 1
  • 16
  • 10