Nikhil V
Nikhil V

Reputation: 27

how to take the output from command prompt and make a text file out of it using language Java

This is what I have found, but in this code it reads line on what you put in, and I don't want that

I am doing a program called Knight's Tour, and I getting output in Command prompt. All I want to do is to read the lines from Command prompt and store it an output file called knight.txt. Can anyone help me out. Thanks.

try
{
    //create a buffered reader that connects to the console, we use it so we can read lines
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

    //read a line from the console
    String lineFromInput = in.readLine();

    //create an print writer for writing to a file
    PrintWriter out = new PrintWriter(new FileWriter("output.txt"));

    //output to the file a line
    out.println(lineFromInput);

    //close the file (VERY IMPORTANT!)
    out.close();
}

catch(IOException e)
{
    System.out.println("Error during reading/writing");
}

Upvotes: 0

Views: 4156

Answers (4)

Srijan
Srijan

Reputation: 1274

I guess what you are doing is writing the output to file using file operations in java but what you want can be done in an easier way as follows - No code is required for this. The output can be redirected by

file > outputfile

This is independent of java.

Upvotes: 0

Wires77
Wires77

Reputation: 351

In the code you posted, just change lineFromInput to whatever string you want to output to the text file.

Upvotes: 0

Vinesh
Vinesh

Reputation: 943

You may look at this example, it shows how write data into an file, if the file exist it show how to append to the file,

public class FileUtil {

  public void writeLinesToFile(String filename,
                               String[] linesToWrite,
                               boolean appendToFile) {

    PrintWriter pw = null;

    try {

      if (appendToFile) {

        //If the file already exists, start writing at the end of it.
        pw = new PrintWriter(new FileWriter(filename, true));

      }
      else {

        pw = new PrintWriter(new FileWriter(filename));
        //this is equal to:
        //pw = new PrintWriter(new FileWriter(filename, false));

      }

      for (int i = 0; i < linesToWrite.length; i++) {

        pw.println(linesToWrite[i]);

      }
      pw.flush();

    }
    catch (IOException e) {
      e.printStackTrace();
    }
    finally {

      //Close the PrintWriter
      if (pw != null)
        pw.close();

    }

  }

  public static void main(String[] args) {
    FileUtil util = new FileUtil();
    util.writeLinesToFile("myfile.txt", new String[] {"Line 1", 
                                                      "Line 2",
                                                      "Line 3"}, true);
  }
} 

Upvotes: 0

user207421
user207421

Reputation: 311047

You don't need Java for that. Just redirect the output of the game to a file:

game > knight.txt

Upvotes: 5

Related Questions