How to write to System.in?

Hi-There is a System.in InputStream waiting using .read().How can I write text to this in stream from a program? Something like, "System.in.write()"?Thanks.
[184 byte] By [jp_in_chainsa] at [2007-11-27 5:24:38]
# 1
You write to an output stream. You read from an input stream.
tsitha at 2007-7-12 14:44:02 > top of Java-index,Java Essentials,Java Programming...
# 2
thru the keyboard?
bsampieria at 2007-7-12 14:44:02 > top of Java-index,Java Essentials,Java Programming...
# 3

This is the problem:

Program A is a Reader, its waiting using a reader.read() around System.in. We need to write autotest for it. Another program, a Writer, should be able to write to this Reader, that is, write to it as if the user is punching text.

We want to "pump" text into the input stream via another program, not via the keyboard.

jp_in_chainsa at 2007-7-12 14:44:02 > top of Java-index,Java Essentials,Java Programming...
# 4
What os are you using? It should be easy on the shell level to pipe the output of one program to be the input of a second program, not?
Hippolytea at 2007-7-12 14:44:02 > top of Java-index,Java Essentials,Java Programming...
# 5
I think what you are looking for is redirecting the output of a program into another program's input. You can do that using pipes.
Dalzhima at 2007-7-12 14:44:02 > top of Java-index,Java Essentials,Java Programming...
# 6
Pipes? In Java?How do I write a pipe in Java?
jp_in_chainsa at 2007-7-12 14:44:02 > top of Java-index,Java Essentials,Java Programming...
# 7
You don't, you use them on the command line, like this:testdata.txt > java MyClassOr something like that.
hunter9000a at 2007-7-12 14:44:02 > top of Java-index,Java Essentials,Java Programming...
# 8

No. It's a shell thing. For a demo, here are two simple programs:

import java.io.*;

public class Reading {

public static void main(String[] args) throws IOException {

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

String line;

while ((line = in.readLine()) != null)

System.out.format("Reading sees [%s]%n", line);

}

}

and

public class Writing {

public static void main(String[] args) {

for (int i = 0; i < 10; ++i)

System.out.format("This is line %d%n", i);

}

}

Once compiled, you run them from the command line by:

% java Writing | java Reading

... or however you shell expressed piping the output from one process to be the input of another.

Try that!

Hippolytea at 2007-7-12 14:44:02 > top of Java-index,Java Essentials,Java Programming...
# 9
I understand. But how would you automate the testing of a function thats expecting a user input. The Writer program is the user, and it has to write text to the Reader.
jp_in_chainsa at 2007-7-12 14:44:02 > top of Java-index,Java Essentials,Java Programming...
# 10

Hunter is right, too. You could first redirect the writer's output to a file:

$java Writing > w.txt

Then run the reader using that input:

$java Reading < w.txt

Sometimes this doesn't work -- say the programs have to run for a long time, and

then you'll have to run them concurrently, with the pipe.

UNIX guys do this all the time.

Hippolytea at 2007-7-12 14:44:02 > top of Java-index,Java Essentials,Java Programming...
# 11

I would use a text file as the input source, but that may not be the best solution depending on how you read the data in your code. Add a line to the file that is the user's first line of input:

Hi, I'm a user.

This is my second input line

etc...

Message was edited by:

hunter9000

hunter9000a at 2007-7-12 14:44:02 > top of Java-index,Java Essentials,Java Programming...
# 12
You guys ever heard of JUnit testing?Guys, I am trying to Drive my console application using JUnit. It should be able to punch in text dynamically.
jp_in_chainsa at 2007-7-12 14:44:02 > top of Java-index,Java Essentials,Java Programming...
# 13
And as far as testing goes, you should be able to test everything but the presentation layer without going through that layer, right?
Hippolytea at 2007-7-12 14:44:02 > top of Java-index,Java Essentials,Java Programming...
# 14

Try something like this (Running on WinXP w/ Cygwin, via bash):

Test.java:

import java.io.*;

import java.util.*;

public class Test{

public static void main(String[] args) {

System.out.print("Type 'ok' to continue: ");

Scanner scanner = new Scanner(System.in);

String next = scanner.next();

if (next.equals("ok")) {

System.out.println("\n Yay...");

} else {

System.out.println("\n Oh poo...");

}

}

}

test.txt (Just the text "ok" with a newline following):

ok

test2.txt (Just the text "boo" with a newline following):

boo

kevin.wallace@MOKANSASC2DA77 ~/java$ java Test < test.txt

Type 'ok' to continue:

Yay...

kevin.wallace@MOKANSASC2DA77 ~/java$ java Test < test2.txt

Type 'ok' to continue:

Oh poo...

I don't know what the equivalent without Cygwin's bash shell would look like.

kevjavaa at 2007-7-12 14:44:02 > top of Java-index,Java Essentials,Java Programming...
# 15

> You guys ever heard of JUnit testing?

>

> Guys, I am trying to Drive my console application

> using JUnit. It should be able to punch in text

> dynamically.

To automate the tests in JUnit, you could point your Reader to a FileInputStream with your test file, but then you wouldn't be testing the console input part, just what you do once you have the input. <shrug>

hunter9000a at 2007-7-21 21:28:24 > top of Java-index,Java Essentials,Java Programming...
# 16
You can programmatically redirect System.in/out/err with System.setIn/Out/Err. Consult the API.
Hippolytea at 2007-7-21 21:28:24 > top of Java-index,Java Essentials,Java Programming...
# 17
> You can programmatically redirect System.in/out/err> with System.setIn/Out/Err. Consult the API.There you go, point System.in to a FileInputStream and read away.
hunter9000a at 2007-7-21 21:28:24 > top of Java-index,Java Essentials,Java Programming...
# 18

> Guys, I am trying to Drive my console application

> using JUnit. It should be able to punch in text

> dynamically.

Maybe if you passed in your InputStream object to the console app part, and used a driver to instantiate it (I haven't tested this)...

public class Myapp {

InputStream in = null;

public Myapp() {

this.in = System.in;

doConsoleThing();

}

public Myapp(InputStream in) {

this.in = in;

doConsoleThing();

}

public static void main(String[] args) {

Myapp app = new Myapp();

}

public void doConsoleThing() {

// Do your nifty console thing...

}

Your JUnit test would do something like:

public void testMyapp() {

FileInputStream fis = new FileInputStream( new File("test.txt") );

Myapp app = new Myapp(fis);

}

kevjavaa at 2007-7-21 21:28:24 > top of Java-index,Java Essentials,Java Programming...