Operational Systems & Java
it never will be broken like:
12345
something else here
67890
Even if Sun didn t write about it explicitly, we can see that it is synchronized or at
least behaves like synchronized that is the same for our real life.
How can I pass a string to the command line(DOS) ? Also i want to capture the
output given by the command line in a string.
Answer: Try this out:
// works for DOS
String cmds[] = new String[2];
cmds[0] = "dir"; // replace with "ls" on UNIX
cmds[1] = "c:"; // replace with "/" on UNIX
// execute the command
Process pro = Runtime.getRuntime().exec(cmds);
// wait until it s done executing
pro.waitFor();
// what did the process output from the Input pipe back to
// this process (okay, who named this stuff)?
InputStream out = pro.getInputStream();
// output it (really slowly)
int i;
while ((i = out.read()) != 1) System.out.println((char) i);
How can I take a program that runs in a DOS shell and send the text that comes
from the shell program into a Java program where it can analyzed, etc.?
Answer: From a command line, use a pipe (with the "|" symbol):
c:\> dosprogram | java JavaProgram
In the Java program, read the text from System.in:
public static void main(String[] args) throws IOException {
int nLines = 0;
BufferedReader in =
new BufferedReader(
new InputStreamReader( System.in));
for (;;) {
String line = in.readLine();
if (line == null)
break;
nLines++;
System.out.println(nLines + ":" + line);
}
}
file:///F|/a_jsite/350_tips/os_win_linux.htm (3 of 8) [2001 07 08 11:24:58]
footer
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved