如何从.txt文件中读取并在文本视图中显示

时间:2011-07-06 20:39:21

标签: java android xml

我试图让我的Android应用程序读取.txt文件并在文本视图中显示一个随机行,

我到目前为止获得的代码片段是

{       
    try
    { 
        java.util.ArrayList<String> lines = new java.util.ArrayList<String>();
        FileReader fileReader; 
        BufferedReader reader; 
        fileReader = new FileReader("MyStrings.txt"); 
        reader = new BufferedReader(fileReader); 

        String lineIn = reader.readLine(); //This String will hold the data brought in fron the text file.
        do
        {
        System.out.println(lineIn);
        lineIn = reader.readLine();
        }while(lineIn != null);
        reader.close();//This step is not necessary but is advised
        ArrayList<String> x = new ArrayList<String>();
        Random rand = new Random();

        //This line gets your question
        //TextView t1v = (TextView) findViewById(R.id.factslist);
        //TextView tv = (x.get(rand.nextInt(x.size()-1))); 
        TextView t = (TextView) findViewById(R.id.factslist); 

我通过教程找到了这个,但我不能让它显示随机线。有什么想法吗? (那里有一些随机的东西没用过)

XML文件

      <TextView
android:gravity="center_vertical|center_horizontal"
android:id="@+id/factslist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textStyle="bold"
android.layout_column="15" 
      android:layout_centerVertical="true"
  android:layout_centerHorizontal="true"/>

2 个答案:

答案 0 :(得分:2)

涉及两个步骤

//Give your range for random line numbers.
int min = 1;
int max = 10;

Random r = new Random();
int someRandomNo = r.nextInt(max - min + 1) + min;

String strTextToDisplay = getStreamTextByLine("MyStrings.txt", someRandomNo);

TextView t = (TextView) findViewById(R.id.factslist);

t.setText(strTextToDisplay);

下面是获取置于assets文件夹中的文本文件的特定行号字符串的有用函数。

private String getStreamTextByLine(String fileName, int lineNumber) {
            String strOut = "";
            String line = "";
            int counter = 1;
            AssetManager assetManager = getAssets();
            try {
                InputStream in = assetManager.open(fileName);
                if (in != null) {
                    InputStreamReader input = new InputStreamReader(in);
                    BufferedReader buffreader = new BufferedReader(input);
                    while ((line = buffreader.readLine()) != null) {
                        if (counter == lineNumber) {
                            strOut = line;
                        }
                        counter++;
                    }
                    in.close();
                } else {
                    Log.e("Input Stream Problem",
                            "Input stream of text file is null");
                }
            } catch (Exception e) {
                Log.e("0003:Error in get stream", e.getMessage());
            }
            return strOut;
        }

答案 1 :(得分:0)

根据我的经验,您需要使用输入流,流读取器和缓冲读取器来读取android中的文件。它不像从命令行运行java那么简单。这是我的应用程序的代码摘录,我根据您的使用情况进行了大量修改。祝你好运。

此代码假定随机数在文件范围内。如果没有,它将返回null。确保RandNum在文件范围内的最佳方法是查找文件中的行数(google,如果您不知道如何操作)并执行模运算,例如:

int RandNum = //gererate random number code
RandNum = RandNum % linecount + 1;
然后

RandNum将是介于1和文件中行数之间的数字。

然后我将其传递给我们的函数:

import java.io.BufferedReader;  
import java.io.File;  
import java.io.FileInputStream;  
import java.io.IOException;  
import java.io.InputStreamReader;

public String[] GetRandLine (File file, int RandNum){

try{
    FileInputStream fis = openFileInput(String.valueOf(file));          
    InputStreamReader isr = new InputStreamReader (fis, "UTF8");
    BufferedReader in = new BufferedReader (isr);

        int i = 1;
        String YourResult;

        while ((YourResult=in.readLine()) != null) { //makes sure line is not null
               if (i == RandNum) return YourResult;
               i++;
        }
        in.close();
        isr.close();
        fis.close();
                return null; //should never get here hypothetically
    }
    catch (IOException e){return null;} //Stream could not be opened
}