<small id='TmSe9'></small><noframes id='TmSe9'>

      1. <tfoot id='TmSe9'></tfoot>
        <i id='TmSe9'><tr id='TmSe9'><dt id='TmSe9'><q id='TmSe9'><span id='TmSe9'><b id='TmSe9'><form id='TmSe9'><ins id='TmSe9'></ins><ul id='TmSe9'></ul><sub id='TmSe9'></sub></form><legend id='TmSe9'></legend><bdo id='TmSe9'><pre id='TmSe9'><center id='TmSe9'></center></pre></bdo></b><th id='TmSe9'></th></span></q></dt></tr></i><div id='TmSe9'><tfoot id='TmSe9'></tfoot><dl id='TmSe9'><fieldset id='TmSe9'></fieldset></dl></div>

      2. <legend id='TmSe9'><style id='TmSe9'><dir id='TmSe9'><q id='TmSe9'></q></dir></style></legend>
        • <bdo id='TmSe9'></bdo><ul id='TmSe9'></ul>

        将文本文件读入Java中的char数组

        时间:2023-07-27

        <small id='zlWWv'></small><noframes id='zlWWv'>

            <tbody id='zlWWv'></tbody>
            <legend id='zlWWv'><style id='zlWWv'><dir id='zlWWv'><q id='zlWWv'></q></dir></style></legend>
              1. <tfoot id='zlWWv'></tfoot>
                <i id='zlWWv'><tr id='zlWWv'><dt id='zlWWv'><q id='zlWWv'><span id='zlWWv'><b id='zlWWv'><form id='zlWWv'><ins id='zlWWv'></ins><ul id='zlWWv'></ul><sub id='zlWWv'></sub></form><legend id='zlWWv'></legend><bdo id='zlWWv'><pre id='zlWWv'><center id='zlWWv'></center></pre></bdo></b><th id='zlWWv'></th></span></q></dt></tr></i><div id='zlWWv'><tfoot id='zlWWv'></tfoot><dl id='zlWWv'><fieldset id='zlWWv'></fieldset></dl></div>
                  <bdo id='zlWWv'></bdo><ul id='zlWWv'></ul>
                  本文介绍了将文本文件读入Java中的char数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在读取一个文本文件并尝试将它逐个字符地存储到一个数组中.我的方法(如下)预先定义了一个初始长度为 100000 的 char 数组(我的主要问题在这里).因此,如果文件包含的字符少于该数量(如果字符多于该数量,也会出现问题),那么我的数组中有空值.我想避免这种情况.有没有办法预先确定文本文件中存在的字符数量?还是有更好的方法来按字符存储文件?

                  I am reading a text file and trying to store it into an array char by char. My approach (below) pre-defines a char array with an initial length of 100000 (my main issue here). So, if the file contains characters less than that amount (problem also if more characters than that), then there are nulls in my array. I want to avoid that. Is there a way to predetermine the amount of characters present in a text file? Or is there a better approach altogether to store the file char by char?

                  char buf[] = new char[100000];
                  FileReader fr = new FileReader(filename);
                  BufferedReader br = new BufferedReader(fr);
                  br.read(buf);
                  for(int i = 0; i < buf.length; i++)
                  {
                       //Do stuff here
                  }
                  

                  推荐答案

                  使用 ArrayList of Characters 来接受值.之后将 ArrayList 转换为 Array 为

                  Use an ArrayList of Characters to accept the values. After that convert ArrayList to an Array as

                   char[] resultArray = list.toArray(new char[list.size()]);
                  

                  您应该使用 br.readLine() 而不是 br.read(),如下所示:

                  You should use br.readLine() instead of br.read() as follows:

                  String str;
                  while((str = br.readLine())!=null){
                          str.tocharArray();
                  }
                  

                  这篇关于将文本文件读入Java中的char数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Java中的字符与字符串? 下一篇:Java - 将小写转换为大写而不使用 toUppercase()

                  相关文章

                  最新文章

                  <i id='4lCd9'><tr id='4lCd9'><dt id='4lCd9'><q id='4lCd9'><span id='4lCd9'><b id='4lCd9'><form id='4lCd9'><ins id='4lCd9'></ins><ul id='4lCd9'></ul><sub id='4lCd9'></sub></form><legend id='4lCd9'></legend><bdo id='4lCd9'><pre id='4lCd9'><center id='4lCd9'></center></pre></bdo></b><th id='4lCd9'></th></span></q></dt></tr></i><div id='4lCd9'><tfoot id='4lCd9'></tfoot><dl id='4lCd9'><fieldset id='4lCd9'></fieldset></dl></div>

                  • <bdo id='4lCd9'></bdo><ul id='4lCd9'></ul>

                  <small id='4lCd9'></small><noframes id='4lCd9'>

                  1. <legend id='4lCd9'><style id='4lCd9'><dir id='4lCd9'><q id='4lCd9'></q></dir></style></legend>
                    <tfoot id='4lCd9'></tfoot>