Showing posts with label File I/O. Show all posts
Showing posts with label File I/O. Show all posts

Tuesday, 13 August 2013

Read file from SD card in Android


Use this function to read files from SD card. Pass the path of the file with file name 
  -  ("/TestFolder/TestFile.text")

public static BufferedReader readFileFromSdCard(String fileName){
        File sdcard = Environment.getExternalStorageDirectory();
        File file=new File(sdcard,fileName);
              BufferedReader myReader = null;
        try {
            if (file.exists()){
            FileInputStream iStream =  new FileInputStream(file);
             myReader = new BufferedReader(
                    new InputStreamReader(iStream));
             Log.i("File", fileName+" Exists ");
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            Log.e("File", fileName+" Not found");
            e.printStackTrace();
        }
        return myReader;

    }

Tuesday, 31 January 2012

Read file from Asset Folder in Android

In android application sometime we need to store files in our asset folder .I have used this method in many of my application and it works perfectly .To read a file from the Asset folder you just need to call this method  .


public static String ReadFromAssetFile(Context context,String filename) {

       String content = "";

      InputStream inputStream = null;

       try {
            inputStream = context.getAssets().open(filenames);
            BufferedReader readerRead1 = new BufferedReader
(new InputStreamReader(inputStream));
 String line="";
while((line=readerRead1.readLine())!=null){
content+=line.trim();
 }
} catch (IOException e1) {
// TODO Auto-generated catch block
return content;
 }
   return content;

}  


Enjoy.... 
Do post your doubts, queries or suggestions in this blog.