Context Open file : Context « Core Class « Android






Context Open file

    
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import android.content.Context;

class Common {
  public static String ReadFileData(Context context, String fileName) {

    String strValue = null;

    File file = context.getFileStreamPath(fileName);
    if (file.exists()) {
      StringBuilder sb = new StringBuilder();
      BufferedReader bufferReader = null;
      try {
        // ?????????????buffer??
        bufferReader = new BufferedReader(new InputStreamReader(
            context.openFileInput(fileName)));
        String strLine = null;
        while ((strLine = bufferReader.readLine()) != null) {
          sb.append(strLine);
        }

      } catch (Exception e) {
        e.printStackTrace();
      } finally {
        try {
          bufferReader.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      strValue = sb.toString();
    }

    return strValue;
  }
}

   
    
    
    
  








Related examples in the same category

1.Context.MODE_WORLD_READABLE
2.Returns whether the context is running on the android emulator.
3.check Internet for Context
4.add Link
5.performing common form field validation tasks
6.Reads all text from asset with specified path.
7.Context Delete File
8.Context.openFileOutput