-1

i tried the following code but it gets stuck on String lineFromInput = in.readLine(); line and without this line , the txt file is empty.

the code i used is :

    try
    {
        File file = new File("out.txt"); //Your file
        FileOutputStream fos = new FileOutputStream(file);
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String lineFromInput = in.readLine();
        PrintStream ps = new PrintStream(fos);
        System.setOut(ps);
        ps.println(lineFromInput);
        //  System.out.println("This goes to out.txt");
    }
    catch(IOException e)
    {
    System.out.println("Error during reading/writing");
    }
  • 2
    What do you mean *"it gets stuck"*? It's waiting for you to type a line of text, and will continue when you press Enter *(because you're reading from `System.in`)*. – Andreas Jul 06 '17 at 03:58
  • 1
    What is the code you are using to run the process which is producing 1000+ lines of data within the Console (if this is indeed what you are doing)? I believe what you are really looking for is perhaps something like: `BufferedReader in = new BufferedReader(new InputStreamReader(myProcess.getInputStream()));` instead of: `System.in`. Of course there is a wee bit more code involved with this but if this is what you're trying to do then that information can be supplied. – DevilsHnd - 退職した Jul 06 '17 at 04:38

1 Answers1

0

As Andreas said, it waits for a Standard Input to write to file. You should really use log4j for this purpose.

See how to make log4j to write to the console as well for more details.

artplastika
  • 1,972
  • 2
  • 19
  • 38