【问题标题】:Is it possible to take a string and put it in the System.in?是否可以获取一个字符串并将其放入 System.in 中?
【发布时间】:2012-05-25 18:11:36
【问题描述】:

我想知道是否有办法获取String - 比如说:

String str = "blabla";

然后做:

System.in.setText(str);   

我知道这不起作用 - 我想知道是否有办法做到这一点。然后发送相同的字符串。就像您在控制台中写入并按 Enter 一样。

这是一个带有服务器套接字的程序,我正在尝试通过端口发送String,以便其他应用程序知道如何处理它。

编辑: 当用户在 Textfield 中写入时,我找到了一种将输入流重定向到 Textfield 的方法,它通过 System.in 发送它。

import java.io.*;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class TextfieldInputStream extends InputStream implements DocumentListener {

    private JTextField tf;
    private String str = null;
    private int pos = 0;

    public TextfieldInputStream(JTextField jtf) {
        tf = jtf;
    }

    @Override
    public int read() {
        //test if the available input has reached its end
        //and the EOS should be returned 
        if(str != null && pos == str.length()){
            str = null;
            //this is supposed to return -1 on "end of stream"
            //but I'm having a hard time locating the constant
            return java.io.StreamTokenizer.TT_EOF;
        }
        //no input available, block until more is available because that's
        //the behavior specified in the Javadocs
        while (str == null || pos >= str.length()) {
            try {
                //according to the docs read() should block until new input is available
                synchronized (this) {
                    this.wait();
                }
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
        //read an additional character, return it and increment the index
        return str.charAt(pos++);
    }

    public void changedUpdate(DocumentEvent e) {
        // TODO Auto-generated method stub
    }

    public void insertUpdate(DocumentEvent e){
        str = tf.getText() + "\n";
        pos = 0;
        synchronized (this) {
            //maybe this should only notify() as multiple threads may
            //be waiting for input and they would now race for input
            this.notifyAll();
        }
    }

    public void removeUpdate(DocumentEvent e){
        // TODO Auto-generated method stub
    }
}

【问题讨论】:

  • 好吧,它是一个带有服务器套接字的程序,我试图通过端口发送一个字符串。以便其他应用程序知道如何处理它
  • 我很困惑,如果您尝试通过端口发送字符串,您不应该使用 Socket,而不是 System.in?
  • @jtahlborn 很棒的评论。我是“按照(原始)规范进行编码”,但提到端口可能是一个完全不同的问题和方法。
  • 第一个应用程序初始化一个服务器套接字,然后第二个是一个连接到它的客户端。这部分工作正常。我似乎无法以程序方式发送我需要使用控制台的任何命令(字符串),这是我提出的这个问题。如果你想要我,我可以向你展示服务器代码和客户端代码
  • 显示的代码越多越好。您一直在说“控制台”,并且还谈论套接字(2 个非常不同、不相关的东西)。这一切如何组合在一起非常令人困惑。

标签: java string input io port


【解决方案1】:
package test.t100.t007;

import java.io.ByteArrayInputStream;
import java.util.Scanner;

public class SystemIn {

    public static void main(String[] args) {
        String str = "blabla";
        ByteArrayInputStream bais = new ByteArrayInputStream(str.getBytes());
        System.setIn(bais);
        // We might use the ByteArrayInputStream here, but going with System.in..
        Scanner scanner = new Scanner(System.in);
        String input = scanner.next();
        System.out.println(input);
    }
}

【讨论】:

    【解决方案2】:

    您可以使用PipedInputStream/PipedOutputStream 对。然后您可以使用System.setIn() 方法将PipedInputStream 设置为System.in。最后,您可以写信给您的PipedOutputStream,并在 System.in 中提供结果。

    【讨论】:

      猜你喜欢
      • 2013-08-03
      • 1970-01-01
      • 2011-12-21
      • 2015-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多