【发布时间】:2014-08-04 17:04:43
【问题描述】:
朋友们
我正在开发一个 android 应用程序,该文本文件应该生成一些数字。在此之后,应一一应用程序调用该号码。
例如:
9876452125, 9876452135, 9876452115,
大多数文本文件有 8 个数字,用“,”和换行符“\n”分隔
现在我想逐行读取该文件。
我的读取文件和存储到数组的代码是:
public void read(String fname)
{
BufferedReader br = null;
try
{
StringBuffer output = new StringBuffer();
String fpath = "/sdcard/" + fname + ".txt";
br = new BufferedReader(new FileReader(fpath));
String line = null;
int index = 0;
String[][] num = new String[15][10];
List<String[]> collection = new ArrayList<String[]>();
while ((line = br.readLine()) != null)
{
if (index < num.length)
{
output.append(line);
// output.append("\n");
num[index] = line.split(",");
if (num.length > 0)
{
collection.add(num[index]);
}
}
Toast.makeText(getApplicationContext(), "" + collection, 5000)
.show();
index++;
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
现在我的问题是当我将集合打印到 Toast 时,它会显示一些随机字符串。不知道为什么??
是否有人对如何逐行读取文件并存储到数组有正确的想法或示例代码。
感谢分配。
【问题讨论】:
标签: android file filereader phone-number stringbuffer