如何搜索.txt文件并检索数据 [英] How to search a .txt file and retrieve data

查看:127
本文介绍了如何搜索.txt文件并检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

制作一个Android应用程序,其中用户将获得一个ID,然后单击提交。使用该ID,它将搜索文本文件,如果找到它将在单独的文本视图中显示内容。



txt文件看起来像这样,每个内容由一个分隔的逗号(,)



Making a android application where user would an ID, then click submit. Using that id it will search the text file and if found it would display the content in separate textviews.

txt file would look like this, with each content separated by a comma (",")

541,BoB,05-18-2014
483,Phil,01-19-1992
971,Komarov,10-30-1858





文本文件位于res / raw / players .txt



为按钮设置onClick处理程序。我不确定应该使用什么类型的阅读器以及如何搜索其内容。在我成功完成搜索功能之后,我将向文件中添加内容。到目前为止,我有,





The text file is located in the res/raw/players.txt

Have an onClick handler for the button. I'm a little unsure of what type of reader I should use and how to search its contents. After I do successfully get the search to function I will be adding content to file. So far I have,

try {
        Resources res = getResources();
        InputStream ins = res.openRawResource(R.raw.players);
        byte[] b = new byte[ins.available()];
        ins.read(); 

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.print("error, cant read");
    }

推荐答案





它更好选择XML或JSON,因为有很多库可用..



如果你还需要去找txt文件...



然后你可以使用StringTokenizer或BufferedReader。



将其读入字符串后使用split提取内容或数据。 />


Hi,

Its better to go for XML or JSON because many libraries are available..

If yo still need to go for txt file...

Then you can go for using StringTokenizer or BufferedReader.

After reading it in to string use "split" extract the content or data.

BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
   // do stuff
}
br.close();







您还可以使用OpenCSV库来处理CSV文件..







谢谢,

Ullas Krishnan




You can also use OpenCSV library for working with CSV files..



Thanks,
Ullas Krishnan


你可以逐行读取输入流:

You can read the inputstream line by line:
BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
String line = null;
while ((line = reader.readLine()) != null) {
    // TODO:

}
reader.close();


这篇关于如何搜索.txt文件并检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆