是否可以从doc或txt文件中创建数据库?

时间:2015-01-17 20:14:04

标签: database file text import

我对Android来说太新了!

我想在主要活动中使用一列按钮创建一个非常简单的应用程序,每个按钮都会打开一个新活动,并且它们中包含一些长文本。 它只是一些简单的应用程序。 所以,如果有可能与哪些代码?我能做什么步骤呢? 请明确因为我不太了解我想学习一些简单的任务  如果可能的话,告诉如何导入文本文件? 或者我应该只使用string.xml,这是唯一正确的方法吗?

1 个答案:

答案 0 :(得分:1)

听起来你的最佳解决方案如下。我假设文本是静态的,你已经在txt文件中使用了它。

将文本文件存储在应用程序中的路径类似于res/raw/textfile.txt

要访问这些文件,请使用类似于以下内容的代码。 R.raw.textfile确定将打开哪个文件,因此请确保根据用户在主要活动中选择的项目进行更改。

// Call the LoadText method and pass it the resourceId
LoadText(R.raw.textfile);

public void LoadText(int resourceId) {
    // The InputStream opens the resourceId and sends it to the buffer
    InputStream is = this.getResources().openRawResource(resourceId);
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    String readLine = null;
    StringBuilder result = new StringBuilder();

    try {
        while ((readLine = br.readLine()) != null) {
            result.append(readLine);
        }

    // Close the InputStream and BufferedReader
    is.close();
    br.close();

    //Do something with result.toString(); (set a text view's content to result.toString())

    } catch (IOException e) {
        e.printStackTrace();
    }
}
相关问题