1

I want to save all the text on the terminal as a output on a text file. For the output there can be a function to be used instead of System.out.println()

    class OutSave
{
    String output;
    public static void main()
    {
        output="";
        print("Print this");
        print("And save this");
    }
    public static print(String p)
    {
        output=output+"\n"+p;
        System.out.println(p);
    }
}

Something like this but I cannot figure this out for the inputs supplied by the user.

Thanks in Advance

3 Answers 3

1

Here is a simple example of how to write a log file of what you are printing.

class OutSave
{

    File output = new File("output.txt");

    public static void main()
    {
        print("Print this");
        print("And save this");
    }

    public static print(String str)
    {
        System.out.println(str);
        writeToFile(output, str);
    }

    public void writeToFile(File file, String str) {
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        bw.write(str);
        bw.close();
    }
}

Edit: Log user input (ints)

    Boolean running = true;
    System.out.println("Enter a Number: ");

    while (true) {
        while (!sc.hasNextInt()) {
            System.out.println("Try again, Enter a Number: "); //invalid entry
            sc.nextLine();
        }
        WriteToFile(output, Integer.toString(sc.nextInt()));
    }

You could call this code from a method then change the running boolean to exit the loop when you are done as this will hang your program in the input loop if you do not exit. Might be a good idea to thread this too.

Sign up to request clarification or add additional context in comments.

4 Comments

But what about the inputs given by the user @Daedric
print("Enter Number"); int n=sc.nextInt();. Terminal Output- Enter Number. 10. File output- Enter Number
Use system.in for user inputs.
How will it help in getting the input to a file @Daedric
1

The usual way of doing this doesn't require doing anything inside your Java program, and works for any program, regardless of the language it was originally written in.

From a command line, in most shells (bash, C shell, MS-DOS, etc.), run:

$program > outfile.txt

Where $program is the name of the executable.

This is commonly known as redirection.

2 Comments

But I am supposed to make it automatic @Paul Brinkley
In general, making this automatic is simple; put the command with the redirection in a shell script. ...if you're saying you're arbitrarily required to make this happen within the Java program itself, then it depends on what you're allowed to do with any System.out.print calls already in it. If you can change them, then you're likely implementing logging, in which case you should look for logging packages such as log4j. Alternately, you might replace those streams using System.setOut(). But you'll need to supply more information about what you're doing.
0

you can use File writer check the link : http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileWriter.html

2 Comments

A link to a potential solution is always welcome, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. Take into account that being barely more than a link to an external site is a possible reason as to Why and how are some answers deleted?
@Krim_0 the link is a 404

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.