Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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 从文件中读取字符_Java_Arrays - Fatal编程技术网

Java 从文件中读取字符

Java 从文件中读取字符,java,arrays,Java,Arrays,我很难理解为什么我的代码不能正常工作。为了测试它,我的txt文件中只有五个字符。我知道字符被放入我创建的数组中,但我不确定它为什么不打印它们。谢谢 catch (IOException exception) { System.err.println(exception); }// catch finally { try { if (fileInput != null)

我很难理解为什么我的代码不能正常工作。为了测试它,我的txt文件中只有五个字符。我知道字符被放入我创建的数组中,但我不确定它为什么不打印它们。谢谢

        catch (IOException exception) {
            System.err.println(exception);
        }// catch

        finally {
            try {
                if (fileInput != null)
                    fileInput.close();
            }// try

            catch (IOException exception) {
                System.err.println("error!" + exception);
            }// catch

        }// finally

    }// main
}// TestCode

在while阅读器循环中有一个for循环。更好地使用:

    int index = 0;
    while ((character = fileInput.read()) != -1) {
        inputArray[index] = (char) character;
        index++;
    }

还要确保不超过输入阵列的大小。如果这个数据需要增长,你可能想考虑使用一个列表类型。

<代码>转换< /代码>服务的目的是什么,如果它所做的一切都是返回<代码> char < /代码>,而你传递的是一个元素<代码> char 数组?有/ /…………意思是那里有很多代码,但为了节省空间,我刚放了retun cIt,最好使用junit或类似的工具来编写测试代码。使用调试器,并逐步完成代码。谢谢,我会记住这一点。我现在只是测试它,我的文件中正好有5个字符。现在,程序就在while循环之后,它永远没有机会创建实例,我不知道为什么