Thursday, 20 February 2014

Reading text from files in android - example

Here is working sample how to read text per line from file/files

 //find all files from folder /assets/txt/  
 String[] elements;
  
 try {  
   elements = getAssets().list("txt");  
 } catch (IOException e) {  
   e.printStackTrace();  
 }  

 //for every files read text per line  
 for (String fileName : elements) {  
   Log.d("xxx", "File: " + fileName);  
   try {  
     InputStream open = getAssets().open("txt/" + fileName);  
     InputStreamReader inputStreamReader = new InputStreamReader(open);  
     BufferedReader bufferedReader = new BufferedReader(inputStreamReader);  
     String line = "";  
     while ((line = bufferedReader.readLine()) != null) {  
       Log.d("xxx", line);     
     }  
   } catch (IOException e) {  
     e.printStackTrace();  
   }  
 }