Reputation: 669
I wanted to create something like a remote control for the command line in Windows.
For this I am using a scanner, but...
The problem is, when I read the whole line with nextLine() from the stream, the prompt will be missing (becouse is is printed, but not in a line) - and when I read the next word with next(), the line break is missing and you will lose the overview. However, some information is even missing.
package com;
import java.io.IOException;
import java.util.Scanner;
public class StdinCmd extends Thread {
public void run() {
try {
execute();
} catch (IOException e) {
e.printStackTrace();
}
}
public void execute() throws IOException {
Scanner reader = new Scanner(MainClient.getProcess().getInputStream()); // <- getting the stream
StdoutSocket stdoutSocket = new StdoutSocket();
while (true) {
while (reader.hasNext()) {
stdoutSocket.executeNext(reader.next()); // <- send it to the socket (the controller). This is what will be displayed at the end.
}
}
}
}
I attached a screenshot of how it should look like, and how it looks at the end:
http://www.mediafire.com/?jma31ezg8ansfal
I hope you can help me and I gave you enough information!
Upvotes: 1
Views: 1667
Reputation: 801
Personally I really don't like scanner so much. If you want to read input line's from a user and send it through a socket.. Who not then just use a BufferedReader with System.in? Read a line and send it through the socket.
BufferedReader br = new BUfferedReader(new InputStreamReader(System.in));
String line = null;
while((line = br.readLine()) != null){
OutSocket.send(line); // or how you send it..
}
~Foorack
Upvotes: 1
Reputation: 347204
Don't use Scanner
or BufferedReader
, but instead read directly from the InputStream
...
InputStream is = null;
try {
is = MainClient.getProcess().getInputStream();
int in = -1;
while ((in = is.read()) != -1) {
System.out.print(((char)in));
}
} catch (IOException exp) {
exp.printStackTrace();
} finally {
try {
is.close();
} catch (Exception exp) {
}
}
Upvotes: 1