I'm reading stdin using a scanner. The scanner stops reading if I send ^D (end of tranmission char) via command line.
Now let's say that I want the scanner to stop if the last line read is "exit".
I'm trying to send this ^D signal to stdin but the scanner does not stop reading.
My attempt so far, this is the read loop:
while (scanner.hasNextLine())
{
try
{
String nextLine = scanner.nextLine();
action.accept(nextLine);
}
catch (Throwable th)
{
onError.accept(th);
}
}
and action:
if (line.equals("exit"))
{
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out)))
{
writer.write("\u0004");
}
catch (IOException e)
{
e.printStackTrace();
}
}
else
{
System.out.println(line);
}