简体   繁体   English

如何使用 InputStream 或 DataInputStream 读取带有 System.in 的字符串

[英]How can I read a String with System.in using a InputStream or DataInputStream

I am trying to read a String with System.in using an InputStream o DataInputStream, maybe I can use BufferedInputStream, but I don't know how to use it, I was searching for bu I don't understand how does it works, I am trying to dos something like this.我正在尝试使用 InputStream o DataInputStream 使用 System.in 读取字符串,也许我可以使用 BufferedInputStream,但我不知道如何使用它,我正在寻找 bu 我不明白它是如何工作的,我我正在尝试做这样的事情。

import java.io.*;
public class Exer10 {
    public static void main(String[] args) throws IOException {
        InputStream is = System.in;
        DataInputStream dis = new DataInputStream(is);
        try {
            while (true){
                dis.readChar();
            }
        } catch (EOFException e){
            
        }
    }
}

The problem here is that I am in loop in the System.in because the method "readChar" is in loop, but if I put the "dis.readChar()" in another position, this only returns me one byte, can you hel me please?这里的问题是我在 System.in 中循环,因为方法“readChar”在循环中,但是如果我将“dis.readChar()”放在另一个 position 中,这只返回一个字节,你能帮帮我吗请问我?

The solution I found is that I can put it in a array of bytes, but this don't solve anything, because if i do this, the file have to be always of the same length, and this length can't be moved.我找到的解决方案是我可以将它放在一个字节数组中,但这并不能解决任何问题,因为如果我这样做,文件必须始终具有相同的长度,并且这个长度不能移动。 Something like this:像这样的东西:

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Exer10 {
    public static void main(String[] args) throws IOException {
        InputStream is = System.in;
        DataInputStream dis = new DataInputStream(is);
        byte[] bytes = new byte[10];
        dis.read(bytes);
    }
}

readChar will only returns one byte if there is one.如果有一个字节,readChar 将只返回一个字节。 The way you could solve it is the following:您可以解决的方法如下:

  1. check if there is some data available in the stream ( available should return a non-null number of bytes)检查 stream 中是否有一些数据可用(可用应返回非空字节数)
  2. fill a new byte array (later converted as String) with the existing content: with read(byte[] bytes)用现有内容填充一个新的字节数组(后来转换为字符串):用 read(byte[] bytes)

Then you can use the extracted data:)然后您可以使用提取的数据:)

    public static void main(String[] args) {
        InputStream is = System.in;
        DataInputStream dis = new DataInputStream(is);
        try {
            while (true) {
                int count = dis.available();
                if (count > 0) {
                    // Create byte array
                    byte[] bytes = new byte[count];
    
                    // Read data into byte array
                    int bytesCount = dis.read(bytes);
    
                    System.out.println("Result: "+ new String(bytes));
                }
            }
        } catch (IOException e) {
            System.out.println(e);
        }
    }

sources:来源:

javadoc: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/DataInputStream.html example: https://www.geeksforgeeks.org/datainputstream-read-method-in-java-with-examples/#:~:text=read(byte%5B%5D%20b)%20method,data%20is%20available%20to%20read . javadoc: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/DataInputStream.html example: https://www.geeksforgeeks.org/datainputstream-read -method-in-java-with-examples/#:~:text=read(byte%5B%5D%20b)%20method,data%20is%20available%20to%20read

Easier implementation would indeed be with a Scanner:使用 Scanner 确实更容易实现:

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            System.out.println("input: "+sc.nextLine());            
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM