简体   繁体   English

如何从android中的文本文件中提取子字符串?

[英]How to extract a substring from a text file in android?

I want to know how can I do to extract a substring from a text file in android: 我想知道如何从android中的文本文件中提取子字符串:

the text file is as shown below : 文本文件如下所示:

[start_delimeter]

0|1|text1

0|1|text2

0|1|text3

0|1|text4

[end_delimiter]

I want to extract strings between start_delimeter and end_delimiter. 我想在start_delimeter和end_delimiter之间提取字符串。

How can I do that in java language/ android ?? 我怎么能在java语言/ android中做到这一点?

Sequence: 序列:

  • First you need to put your text file in the assets directory (ex filename.txt ) 首先,您需要将您的文本文件放在assets目录中(ex filename.txt
  • Read the file (using a BufferedReader ) till you read the start delimiter 读取文件(使用BufferedReader ),直到读取开始分隔符
  • Read each line from the file and process it using Pattern and Matcher till the end delimiter is found (or with String.split("|") ) 从文件中读取每一行并使用PatternMatcher处理它,直到找到结束分隔符(或者使用String.split("|")

And this is the code: 这是代码:

    // first get the file from the assets
    InputStream is = context.getAssets().open("filename.txt");

    try {
        // start reading (line by line)
        BufferedReader r = new BufferedReader(new InputStreamReader(is));

        // wait for the start delimiter
        String line;
        while ((line = r.readLine()) != null) 
            if (line.equals("[start_delimiter]"))
                break;

        // this is the pattern for the data "int|int|String":
        Pattern p = Pattern.compile("\\|(\\d+)\\|(\\d+)\\|(.+)");

        // read it line by line...
        while ((line = r.readLine()) != null) {
            // ... till the end comes (end delimiter)
            if (line.equals("[end_delimiter]"))
                break; //done

            // if the data matches the pattern...
            Matcher m = p.matcher(line);
            if (m.matches()) {
                // ... handle it!
                int first   = Integer.parseInt(m.group(1));
                int second  = Integer.parseInt(m.group(2));
                String text = m.group(3);

                //...
            }
        }

    } finally {
        is.close();
    }

I want to extract strings between start_delimeter and end_delimiter. 我想在start_delimeter和end_delimiter之间提取字符串。

How can I do that in java language/ android ?? 我怎么能在java语言/ android中做到这一点?

Read file line by line into String. 将文件逐行读入String。 then 然后

str.subString(startIndex,endIndex);

Simply put, I would read the file line-by-line with a BufferedReader. 简单地说,我会用BufferedReader逐行读取文件。 Once the starting delimiter is found evaluate each subsequent line to see if it's the ending delimiter. 找到起始分隔符后,评估每个后续行以查看它是否为结束分隔符。 If it isn't the delimiter, write the string to a collection. 如果它不是分隔符,则将字符串写入集合。 Once the ending delimiter is reached, stop reading and close the reader. 到达结束分隔符后,停止阅读并关闭阅读器。

BufferedReader example BufferedReader示例

Use a BufferedReader and read the file line by line. 使用BufferedReader并逐行读取文件。

BufferedReader r = ...;
//drop until we hit the delimiter
while (!r.readLine().equals("[start_delimeter]"));
String line;
while (!(line=r.readLine()).equals("[end_delimiter]")) {
    //do something with "line" here
}

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

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