Friday, February 3, 2017

How to read file from asset in Android?

In my case consider fileName as “myfile.txt”
 public String ReadFromAssetfile(String fileName) {  
     StringBuilder returnString = new StringBuilder();  
     InputStream fIn = null;  
     InputStreamReader isr = null;  
     BufferedReader input = null;  
     try {  
       fIn = getResources().getAssets()  
           .open(fileName, Context.MODE_WORLD_READABLE);  
       isr = new InputStreamReader(fIn);  
       input = new BufferedReader(isr);  
       String line = "";  
       while ((line = input.readLine()) != null) {  
         returnString.append(line);  
       }  
     } catch (Exception e) {  
       e.getMessage();  
     } finally {  
       try {  
         if (isr != null)  
           isr.close();  
         if (fIn != null)  
           fIn.close();  
         if (input != null)  
           input.close();  
       } catch (Exception e2) {  
         e2.getMessage();  
       }  
     }  
     return returnString.toString();  
   }  

No comments :

Post a Comment