Click here to Skip to main content
15,889,878 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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


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,

Java
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");
    }
Posted
Comments
PIEBALDconsult 2-Nov-14 13:14pm    
You would likely have an easier time of it if you used XML rather than unstructured text.
Member 11200256 2-Nov-14 13:25pm    
of course it would be. Put thats how the assignment is required. :)

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.

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



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



Thanks,
Ullas Krishnan
 
Share this answer
 
You can read the inputstream line by line:
Java
BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
String line = null;
while ((line = reader.readLine()) != null) {
    // TODO:

}
reader.close();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900