6

I have an utility class named 'MyClass'. The class has two methods to read/write some data into phone's internal memory. I am new to android, Please follow below code.

public class MyClass  {
  public void ConfWrite() {
    try {
      BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(new 
                           File(getFilesDir()+File.separator+"MyFile.txt")));
      bufferedWriter.write("lalit poptani");
      bufferedWriter.close();
    } catch (Exception e1) {
        e1.printStackTrace();
    }
  }
}

while executing ConfWrite method, it fails

please provide a better solution to solve this

thanks in advance

Riskhan
  • 4,434
  • 12
  • 50
  • 76

2 Answers2

23

You can Read/ Write your File in data/data/package_name/files Folder by,

To Write

BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(new 
                            File(getFilesDir()+File.separator+"MyFile.txt")));
bufferedWriter.write("lalit poptani");
bufferedWriter.close();

To Read

 BufferedReader bufferedReader = new BufferedReader(new FileReader(new 
                           File(getFilesDir()+File.separator+"MyFile.txt")));
 String read;
 StringBuilder builder = new StringBuilder("");

 while((read = bufferedReader.readLine()) != null){
        builder.append(read);
      }
 Log.d("Output", builder.toString());
 bufferedReader.close();
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • In your reading code, can i read a line of text without using string builder – Riskhan Feb 16 '12 at 07:11
  • 2
    @krish yeh,use bufferedReader.readLine() and it will give you whole line. – MKJParekh Feb 16 '12 at 07:17
  • @Soni Is it possible to open a file with readwrite mode, ie update the values without using a temporary file – Riskhan Feb 16 '12 at 07:20
  • @krish I think the java class BufferedWriter will open it up in write mode default...you want to read the file need to use BufferedReader, but I dont understand why you want to open same file in two mode..cause then you will not get read/write java functions of that 2 class,btw yes you can open single file in two modes(I think) – MKJParekh Feb 16 '12 at 07:26
  • @LalitPoptani I edited with your code, but it fails while executing, shows an exception NullPointerException in getFilesDir(). I executed as follows MyClass myclass = new MyClass(); myclass.ConfWrite(); – Riskhan Feb 16 '12 at 07:28
  • @Soni, I am new to Java/Android, basically C++ programmer. In C++, a file update (read/write) without using a temporary file – Riskhan Feb 16 '12 at 07:31
  • @LalitPoptani I tried your answer code and it work fine but I would know where MyFile.txt is stored? – nawara Jun 15 '13 at 12:42
  • @nawara path is given in first line of my answer!! – Lalit Poptani Jun 15 '13 at 17:40
-1

public static void WriteFile(String strWrite) {
        String strFileName = "Agilanbu.txt"; // file name
        File myFile = new File("sdcard/Agilanbu"); // file path 
        if (!myFile.exists()) { // directory is exist or not
            myFile.mkdirs();    // if not create new
            Log.e("DataStoreSD 0 ", myFile.toString());
        } else {
            myFile = new File("sdcard/Agilanbu");
            Log.e("DataStoreSD 1 ", myFile.toString());
        }

        try {
            File Notefile = new File(myFile, strFileName); 
            FileWriter writer = new FileWriter(Notefile); // set file path & name to write
            writer.append("\n" + strWrite + "\n"); // write string
            writer.flush();
            writer.close();
            Log.e("DataStoreSD 2 ", myFile.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String readfile(File myFile, String strFileName) {
        String line = null;
        try {
            FileInputStream fileInputStream = new FileInputStream(new File(myFile + "/" + strFileName)); // set file path & name to read
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream); // create input steam reader 
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            StringBuilder stringBuilder = new StringBuilder();
            while ((line = bufferedReader.readLine()) != null) { // read line by line
                stringBuilder.append(line + System.getProperty("line.separator")); // append the readed text line by line
            }
            fileInputStream.close();
            line = stringBuilder.toString(); // finially the whole date into an single string
            bufferedReader.close();
            Log.e("DataStoreSD 3.1 ", line);
        } catch (FileNotFoundException ex) {
            Log.e("DataStoreSD 3.2 ", ex.getMessage());
        } catch (IOException ex) {
            Log.e("DataStoreSD 3.3 ", ex.getMessage());
        }
        return line;
    }

use this code to write ---  WriteFile(json); // json is a string type
use this code to read  ---  File myFile = new File("sdcard/Agilanbu");
                            String strObj = readfile(myFile, "Agilanbu.txt");

// you can put it in seperate class and just call it where ever you need.(for that only its in static)
// happie coding :)
Agilanbu
  • 2,747
  • 2
  • 28
  • 33