-2

I am working on java project where i need to get data from console. Following are my code and output case.

 public static void main(String[] args) {       
    String test= "Hii test";
    System.out.println(test);
}

Now in this program the output will be print "Hii test" now I want to get this data from console without using test variable. Please suggest how I get this data from console?

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
santosh
  • 435
  • 1
  • 7
  • 24
  • 5
    What do you mean "get this data from console"? You just printed it, why do you need to get it again? – Andy Turner Sep 15 '22 at 09:14
  • 6
    What are you trying to do that you need to do this instead of using the `test` variable? Are you sure this is not an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)? – Federico klez Culloca Sep 15 '22 at 09:14
  • Does this link help you? https://stackoverflow.com/questions/4005378/console-writeline-and-system-out-println – Ali Sep 15 '22 at 09:16
  • 1
    Probably this answers your question: https://stackoverflow.com/questions/8708342/redirect-console-output-to-string-in-java – Rafael Sep 15 '22 at 09:16
  • Its just an example which I want to do. Actually my problem is that i am using LoggingTransferListener in aws sdk v2 to show progress details and I want to show that progress in progress bar so I have only two ways either use custom implementation of LoggingTransferListener or read logging details from console and show progress bar according to it. – santosh Sep 15 '22 at 09:26
  • 1
    Then use a custom implementation of `LoggingTransferListener`. It makes much more sense. – Federico klez Culloca Sep 15 '22 at 09:29
  • Yes I am trying but takes more timing to understand the code of LoggingTransferListener and I need to deliver it early. – santosh Sep 15 '22 at 09:37
  • Does this answer your question? [Redirect console output to string in Java](https://stackoverflow.com/questions/8708342/redirect-console-output-to-string-in-java) – Progman Sep 15 '22 at 13:50
  • Related: [Reading already-written text from console](https://stackoverflow.com/questions/8190663/reading-already-written-text-from-console) (but no solution still) – Nor.Z May 12 '23 at 15:51

2 Answers2

1

Getting data from the console isn't practical. Typically the console isn't considered an input medium. It would be best to grab the data before you output the data to the console into a variable, print it to the console, and then use it.

String test= "Hii test";
String newVariable = test;
System.out.println(test);
// use newVariable however you wish to use it.

Of course, my example is very simplistic however, I think you get the idea. you typically don't get data from the console.

Dale
  • 1,613
  • 4
  • 22
  • 42
0

You can proxy the output stream like so:

public static void main(String[] args) {

    final StringBuilder consoleData = new StringBuilder();

    final OutputStream proxyStream = new OutputStream() {
        final PrintStream originalStream = System.out;

        @Override
        public void write(int b) {
            consoleData.append((char) b);
            originalStream.write(b);
        }
    };

    System.setOut(new PrintStream(proxyStream, true));

    System.out.println("Some text");
    System.out.println(consoleData);
}

The output:

Some text
Some text
Ciprian
  • 11
  • 1