2012-12-19 46 views
-1

我已经设置了类似下面解析文件/ Android的

Title - Welcome to the Dibb 
Date - 13/03/11 
Information - Hello and welcome to our website. 

Title - Welcome to student room 
Date - 06/05/11 
Information - Hello and welcome to the student room. We are a online forum that allows previous and current students to ask questions. 

一个文本文件,我需要解析这个文本文件,并保存之类的标题行,日线,其余的将被保存为信息。我知道如何读取文件并将整个文件保存为字符串,但我一直在获取选择信息。

CODE

这是我用来读取文本文件中的代码

helloTxt.setText(readTxt()); 



} 

private String readTxt() { 

    InputStream inputStream = getResources().openRawResource(R.raw.pages); 

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 

    int i; 
    try { 
     i = inputStream.read(); 
     while (i != -1) { 
      byteArrayOutputStream.write(i); 
      i = inputStream.read(); 
     } 
     inputStream.close(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    String str = byteArrayOutputStream.toString(); 

    return str; 

} 
+0

看起来好像你可以在第一次之后分开。有什么问题吗? –

+0

感谢您的建议,但我需要整行,所以我不能在 - –

+0

后溢出“但我需要整行,所以我不能在后面溢出 - ”如果您需要全行,您将如何提取字段? –

回答

1
一行

BufferedReader br = new BufferedReader(new FileReader(file)); 
String line; 
while ((line = br.readLine()) != null) { 
    // process the line. 
} 
br.close(); 

读取文件中的行如果你能保证每行有作为最大的一个-那么你可以使用以下模式。

String[] tokens = line.split("\s-\s"); 

对于此行

标题 - 欢迎迪布

它会给你

tokens[0] = "Title"; 
tokens[1] = "Welcome to the Dibb"; 
+0

破碎而脆弱的解决方案。如果标题或日期包含'-',那么这将完全打破。 –

+0

@MathiasSchwarz,到目前为止它没有。 –

+0

关于行处理:直到第一个' - '才读取更好吗?将字符串和rtrim/ltrim分成两部分? – xsl

0

我尝试写一些类,它可以帮助你接近你的问题

import java.io.IOException; 
import java.io.InputStream; 
import java.util.ArrayList; 
import java.util.List; 

public class Test4 { 

    private List<Information> parser(String data) { 

     List<Information> informations = new ArrayList<Information>(); 
     String blocks[] = data.split("\n\r"); 

     for(String block : blocks) { 
      String[] lines = block.split("\n"); 
      Information information = new Information(); 
      information.setTitle((lines[0].split("-"))[1].trim()); 
      information.setDate((lines[1].split("-"))[1].trim()); 
      information.setInfo((lines[2].split("-"))[1].trim()); 
      informations.add(information); 
     } 

     return informations; 
    } 

    private void runner() throws IOException { 
     InputStream inputStream = getClass().getResourceAsStream("input.txt"); 
     String input = ""; 
     int cc; 
     while((cc = inputStream.read()) != -1) { 
      input += (char) cc; 
     } 

     List<Information> informations = parser(input); 
     for(Information information : informations) { 
      System.out.println(information); 
     } 

    } 

    /** 
    * @param args 
    * @throws IOException 
    */ 
    public static void main(String[] args) throws IOException { 
     Test4 test4 = new Test4(); 
     test4.runner(); 
    } 

    class Information { 

     private String title; 

     private String date; 

     private String info; 

     public String getTitle() { 
      return title; 
     } 

     public void setTitle(String title) { 
      this.title = title; 
     } 

     public String getDate() { 
      return date; 
     } 

     public void setDate(String date) { 
      this.date = date; 
     } 

     public String getInfo() { 
      return info; 
     } 

     public void setInfo(String info) { 
      this.info = info; 
     } 

     @Override 
     public String toString() { 
      return "Information [" + (title != null ? "title=" + title + ", " : "") 
        + (date != null ? "date=" + date + ", " : "") + (info != null ? "info=" + info : "") + "]"; 
     } 

    } 

} 
+0

谢谢,我没有使用这个确切的代码,但它有很多帮助,我有现在完成了我需要的一半:D –

+0

不管。如果你满意就接受答案并投票。谢谢。你的代码还有其他问题吗? –