I have used Internal Storage which store file in Application package directory that can't be accessible by not rooted device.
Here the class which can create, read and delete the file
public class ReadWriteJsonFileUtils {
Activity activity;
Context context;
public ReadWriteJsonFileUtils(Context context) {
this.context = context;
}
public void createJsonFileData(String filename, String mJsonResponse) {
try {
File checkFile = new File(context.getApplicationInfo().dataDir + "/new_directory_name/");
if (!checkFile.exists()) {
checkFile.mkdir();
}
FileWriter file = new FileWriter(checkFile.getAbsolutePath() + "/" + filename);
file.write(mJsonResponse);
file.flush();
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public String readJsonFileData(String filename) {
try {
File f = new File(context.getApplicationInfo().dataDir + "/new_directory_name/" + filename);
if (!f.exists()) {
onNoResult();
return null;
}
FileInputStream is = new FileInputStream(f);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
return new String(buffer);
} catch (IOException e) {
e.printStackTrace();
}
onNoResult();
return null;
}
public void deleteFile() {
File f = new File(context.getApplicationInfo().dataDir + "/new_directory_name/");
File[] files = f.listFiles();
for (File fInDir : files) {
fInDir.delete();
}
}
public void deleteFile(String fileName) {
File f = new File(context.getApplicationInfo().dataDir + "/new_directory_name/" + fileName);
if (f.exists()) {
f.delete();
}
}
}
You can create, read and delete the file by calling ReadWriteJsonFileUtils class methods as follows:
For creating file:
try {
new ReadWriteJsonFileUtils(context).createJsonFileData(file_name, data);
} catch (Exception e) {
e.printStackTrace();
}
For reading file:
String jsonString = new ReadWriteJsonFileUtils(context).readJsonFileData(file_name);
For deleting single file
new ReadWriteJsonFileUtils(context).deleteFile(file_name);
For deleting all file
new ReadWriteJsonFileUtils(context).deleteFile();