Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.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_Android_Regex_Split - Fatal编程技术网

Java 按拆分时,拆分跳过半个内容@

Java 按拆分时,拆分跳过半个内容@,java,android,regex,split,Java,Android,Regex,Split,我有一个android应用程序,它从文本文件中请求数据。当我试图分离数据并初始化类时,我遇到了严重的问题,因为分割后的数据有一半丢失了。数据源的代码为: indicators = new ArrayList<Indicator>(); try { InputStream fileStream = getResources().openRawResource( R.raw.indicators);

我有一个android应用程序,它从文本文件中请求数据。当我试图分离数据并初始化类时,我遇到了严重的问题,因为分割后的数据有一半丢失了。数据源的代码为:

indicators = new ArrayList<Indicator>();
    try {
        InputStream fileStream = getResources().openRawResource(
                            R.raw.indicators);
        int fileLen = fileStream.available();
        // Read the entire resource into a local byte buffer.
        byte[] fileBuffer = new byte[fileLen];
        fileStream.read(fileBuffer);
        fileStream.close();
        displayText = new String(fileBuffer);
        String[] counts = displayText.split("@");
        for(String i: counts) {
            String[] temps = i.split(";");
            indicators.add(new Indicator(temps[0],temps[1],temps[2]));
            }
        } catch (IOException e) {
          // exception handling
        }
    for(Indicator i: indicators) Log.v("indicator", i.toString());
执行此过程后,以下日志文件会显示大量缺少的内容:

文件似乎会跳过其他条目,因此,我的整个软件都一团糟。下面的源文件是:


我无法通过发送命令来解决问题。不过,我找到了一种解决这个问题的方法,即使用BufferedReader类在中读取打开的字符串。这似乎解决了问题。

出于好奇,在开始解析“counts”数组之前,“counts”数组的大小是多少?是否有任何原因导致您没有逐行阅读文件
,因为除了最后一行之外,所有
@
都出现在行尾。无论如何,尝试使用
System.getProperty(“line.separator”)
在新行上拆分。数组的计数也是244。。。当打印指示器时,我得到双倍的结果,这意味着日志工作不正常或存在一些底层软件。引用:请注意,虽然InputStream的某些实现将返回流中的总字节数,但许多实现不会返回。使用此方法的返回值来分配用于保存此流中所有数据的缓冲区是永远不正确的。”(强调添加)虽然我接受输入流的前提和它可能提供的问题。问题不在于检索文件。问题开始于字符串与我看到的内容分离。我逐行打印检索到的文本文件。效果很好
    public class Indicator {
    private String name;
    private String isoCode;
    private String topic;

    public Indicator(String topic, String isoCode,String name) {
        this.name = name;
        this.isoCode = isoCode;
        this.topic = topic;
    }
    public String getName() {
        return this.name;
    }
    public String getIsoCode() {
        return this.isoCode;
    }
    public String getTopic() {
        return this.topic;
    }
    public String toString() {
        return (this.topic + "," + this.isoCode + "," + this.name);
    }
}