Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从inputstream读取时缺少第一个字符_Java_Inputstream - Fatal编程技术网

Java 从inputstream读取时缺少第一个字符

Java 从inputstream读取时缺少第一个字符,java,inputstream,Java,Inputstream,我不明白为什么下面的代码似乎跳过了第一个输入字符 import java.io.*; import java.util.Arrays; public class Input { public static void main (String args[]) { try { echo(System.in); } catch (IOException e) { e.printStackT

我不明白为什么下面的代码似乎跳过了第一个输入字符

import java.io.*;
import java.util.Arrays;

public class Input
{
    public static void main (String args[])
    {
        try {
            echo(System.in);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    static void echo(InputStream stream) throws IOException
    {
        byte[] input = new byte[10];

        char[] inputChar = new char[10];
        int current;
        while ((current = stream.read()) > 0){
            stream.read(input);

            int i = 0;
            while (input[i] != 0) {
                inputChar[i] = (char)input[i];
                if (i < 9) {
                    i++;
                }
                else {
                    break;
                }
            }   
            System.out.println(Arrays.toString(inputChar));
        }
    }
}

如果输入为1234567890,则输出为[2,3,4,5,6,7,8,9,0],inputChar中的最后一个字符似乎为空。但是,如果它没有将任何字节读入输入[9],则应为空字符ASCII 0。如果我给出的输入长度小于10个字符,那么inputChar中剩余的位置将得到空字符。所以我不确定这里发生了什么

当前=流读取>0时{


您在这里有您的第一次阅读,您没有使用它,但它是一个实际的阅读,将提升您在流中的位置。

而current=stream.read>0{


这里有您的第一个读取,您没有使用它,但它是一个实际读取,将提升流中的位置。

循环控件中使用的stream.read读取第一个字符。

循环控件中使用的stream.read读取第一个字符。

我用do whil解决了这个问题e循环而不是while循环

import java.io.*;
import java.util.Arrays;

public class Input
{
    public static void main (String args[])
    {
        try {
            echo(System.in);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    static void echo(InputStream stream) throws IOException
    {
        byte[] input = new byte[10];

        char[] inputChar = new char[10];
        int current;
        while ((current = stream.read()) > 0){
            stream.read(input);

            int i = 0;
            while (input[i] != 0) {
                inputChar[i] = (char)input[i];
                if (i < 9) {
                    i++;
                }
                else {
                    break;
                }
            }   
            System.out.println(Arrays.toString(inputChar));
        }
    }
}
以下是我的完整代码:


我修复了do while循环而不是while循环的问题

以下是我的完整代码:


啊,好的。这很有意义。谢谢。谢谢,完美。这有助于理解为什么我在以后尝试解析XML结果时总是在[row,col]:[1,1]处出现ParseError,好的。这很有意义。谢谢。谢谢,完美。这有助于理解为什么我在以后尝试解析XML结果时总是在[row,col]:[1,1]处出现ParseError