You can save and read files in the internal storage as private files which only your app can see except for other apps with root privileges.
The absolute path of this folder is data\data\[your app package]\files
The class Context provides some methods which help you to make operations such as:
openFileInput(String name)
Open a private file associated with this Context’s application package for readingopenFileOutput(String name, int mode)
Open a private file associated with this Context’s application package for writinggetFilesDir()
Gets the absolute path to the filesystem directory where your internal files are savedgetDir()
Creates (or opens an existing) directory within your internal storage spacedeleteFile()
Deletes a file saved on the internal storagefileList()
Returns an array of files currently saved by your application
and you don’t need to have special permissions in order to use these methods.
For example the following code taken from Using the Internal Storage creates a file containing the string “hello world!”
try { String FILENAME = "hello_file"; String string = "hello world!"; FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); fos.write(string.getBytes()); fos.close(); } catch (IOException e) { Log.e("ERROR", e.toString()); }
where at the line 5, in place of MODE_PRIVATE you can use MODE_APPEND, other constants such as MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE are deprecated.
The following code reads the same file and shows a message with the text “hello world!”
try { String FILENAME = "hello_file"; byte[] bytes = new byte[1024]; FileInputStream fis = openFileInput(FILENAME); fis.read(bytes); fis.close(); String string = new String(bytes); Toast.makeText(getApplicationContext(), string, Toast.LENGTH_SHORT).show(); } catch (IOException e) { Log.e("ERROR", e.toString()); }
But these examples have a limitation: they can’t be used in sub folders, in other words you can’t create a tree structure of directories and files.
If you need to create files in subdirectories you must use the standard technique of java, for example the following code creates a file inside the folder “sub”
try { String FILENAME = "hello_file"; String FOLDERNAME = "sub"; String string = "hello world!"; Context context = getApplicationContext(); String folder = context.getFilesDir().getAbsolutePath() + File.separator + FOLDERNAME; File subFolder = new File(folder); if (!subFolder.exists()) { subFolder.mkdirs(); } FileOutputStream outputStream = new FileOutputStream(new File(subFolder, FILENAME)); outputStream.write(string.getBytes()); outputStream.close(); } catch (FileNotFoundException e) { Log.e("ERROR", e.toString()); } catch (IOException e) { Log.e("ERROR", e.toString()); }
and the following code reads the same file
try { String FILENAME = "hello_file"; String FOLDERNAME = "sub"; byte[] bytes = new byte[1024]; Context context = getApplicationContext(); String folder = context.getFilesDir().getAbsolutePath() + File.separator + FOLDERNAME; File subFolder = new File(folder); FileInputStream outputStream = new FileInputStream(new File(subFolder, FILENAME)); outputStream.read(bytes); outputStream.close(); String string = new String(bytes); Toast.makeText(getApplicationContext(), string, Toast.LENGTH_SHORT).show(); } catch (FileNotFoundException e) { Log.e("ERROR", e.toString()); } catch (IOException e) { Log.e(TAG, e.toString()); }
Leave a Reply