[英]How to read from .txt file and show in Text View
我试图让我的 android 应用程序读取 a.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"/>
有两个步骤涉及
//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);
下面是有用的 function 获取放置在资产文件夹中的文本文件的特定行号的字符串。
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;
}
根据我的经验,您需要使用输入 Stream、Stream 读取器和缓冲读取器来读取 android 中的文件。 它不像从命令行运行 java 那样简单。 这是我的一个应用程序的代码摘录,我为您的使用进行了大量修改。 祝你好运。
此代码假定随机数在文件的 scope 内。 如果没有,它将返回null
。 确保RandNum
在文件的 scope 范围内的最佳方法是查找文件中的行数(如果您不知道该怎么做,请用 Google 搜索)并执行模运算,例如:
int RandNum = //gererate random number code
RandNum = RandNum % linecount + 1;
RandNum
将是一个介于 1 和文件中的行数之间的数字。
然后我将它传递给我们的 function:
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.