Android – how to split strings with ‘newline’?

BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str
while ((str =in.readLine()) != null)
{
     items = str.split("\n");
}
in.close();

String (STR) contains data from a text file, such as:

January

February

parade

wait

Each word is on a Ag new line. I want to read the string and separate each word on the new line and store it in the string object array (this will be a variable named 'items')

resolvent:

In fact, bufferedreader.readline has split the input according to line feed

So where are you now:

items=str.split("\n");

You just need to append STR to your array

For example, infile file save:

January
February
March
April
May
June

The following program outputs 6 (the size of the array list created):

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
class Test {
    public static void main (String[] args) {
        try {
            ArrayList<String> itms = new ArrayList<String> ();
            BufferedReader br = new BufferedReader (new FileReader ("infile"));
            String str;
            while ((str = br.readLine()) != null)
                itms.add(str);
            br.close();
            System.out.println (itms.size());
        } catch (Exception e) {
            System.out.println ("Exception: " + e);
        }
    }
}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>