0

我们可以将仅名称列表与文件系统中的数据分开吗

Name    Age Sex Address
dani    21  Male    US
ron     34  Male    NY
stella  23  Female  NY
steave  43  Male    US  
[End]

我试图在插入文件时将上述数据中的分隔符设置为'\t'。
我试过了

try 
{
fin = openFileInput("FileSystemDatabase.dat");
isr = new InputStreamReader(fin);
br = new BufferedReader(new InputStreamReader(fin));
while ((line = br.readLine()) != null) 
    {
           if (line.equals("\rName\tAge\tSex\tAddress\n"))
                 break;
    }
Pattern p = Pattern.compile("\r\n\\d\t");
while ((line = br.readLine()) != null) 
    {
           if (line.equals("[End]"))
                   break; 
           Matcher m = p.matcher(line);
           if (m.matches()) 
           {
                   text =m.group(1);
                   tv=new TextView(this);
                   tv.setText(text);

           }
    }
}
catch (Exception e)
{
    e.printStackTrace();
}

但是我没有得到正确的结果来获得正确的列表。有什么简单的方法可以得到它吗?请帮我..

4

1 回答 1

1

试试这个..你只会得到名字:

     while ((line = br.readLine()) != null && (line.equals("Name\t\tAge\tSex\tAddress\n"))) {
            Log.e("String ", "" + line);
            break;
        }

        while ((line = br.readLine()) != null) {
            if (line.equals("[End]"))
                break;
            splitString = line.split(" ");
            Log.e("Name ", "" + splitString[0]);
        }

输出:

06-28 11:30:08.009: E/String(3713): dani 21 男性美国

06-28 11:30:08.009: E/姓名(3713): dani

06-28 11:30:08.009: E/String(3713): ron 34 Male NY

06-28 11:30:08.009: E/姓名(3713): ron

06-28 11:30:08.009: E/String(3713): stella 23 Female NY

06-28 11:30:08.009: E/姓名(3713): stella

06-28 11:30:08.009: E/String(3713): steave 43 男性美国

06-28 11:30:08.009:E/Name(3713):steave

于 2012-06-28T05:56:47.123 回答