-1

我有一个文本文件,如下所示

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. 

我需要解析这个文本文件并保存标题行、日期行等内容,其余内容将保存为信息。我知道如何读取文件并将完整文件保存为字符串,但我一直在获取选择信息。

代码

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

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;

}
4

2 回答 2

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");

对于这条线

标题 - 欢迎来到 Dibb

它会给你

tokens[0] = "Title";
tokens[1] = "Welcome to the Dibb";
于 2012-12-19T13:25:56.020 回答
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 : "") + "]";
        }

    }

}
于 2012-12-19T13:51:53.763 回答