Riskhan
Riskhan

Reputation: 4470

Writing/Reading Files to/from Android phone's internal memory

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

Upvotes: 6

Views: 37829

Answers (2)

Agilarasan anbu
Agilarasan anbu

Reputation: 2805

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 :)

Upvotes: -1

Lalit Poptani
Lalit Poptani

Reputation: 67286

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();

Upvotes: 23

Related Questions