Close streamReading files is also basically the same method.
Tips for reading files: If you want to save a static file in your application during compilation, save the file
Res/raw/directory. You can use openRawResource () to open a file and return an InputStream.
Object, and then you can read and write data.
Code:
Method 1:
FileOutputStream writeStream = null; FileOutputStream fileOutputStream = null; switch (view. getId () {case R. id. button1: // other files created by other programs cannot access File file1 = new File (getFilesDir (), FILE1); try {// write data writeStream = new FileOutputStream (file1); writeStream. write ("haha ". getBytes ();} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} finally {try {if (wr IteStream! = Null) writeStream. close ();} catch (IOException e) {e. printStackTrace ();}}
Method 2:
// Android is used. content, try {/* is set to MODE_PRIVATE and cannot be accessed by other programs. This is the default constructor and can be set to other methods for read/write. */FileOutputStream = openFileOutput (FILE2, Context. MODE_PRIVATE); // write data fileOutputStream. write ("hehe ". getBytes ();} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} finally {try {if (fileOutputStream! = Null) fileOutputStream. close ();} catch (IOException e) {e. printStackTrace ();}}
Store cached files
If you want to cache some files, you can use createTempFile () to create the file. You should use getCacheDir () to open the file.
Tip: Normally, the above files cannot be viewed. To view them, you must use adb. simulator: adb devices to view the device.
~ $ Adb devices
List of devices attached
Emulator-5554 device
Then, go to the Super User and run the corresponding command to view the information.
~ $ Adb-s emulator-5554 shell
#
Note: the internal storage directory of the application is determined by the package name of the application. By default, other programs cannot access the internal storage directory.
Storage path, unless you use readable or writable mode.
References:
Http://developer.android.com/guide/topics/data/data-storage.html
Http://developer.android.com/training/basics/data-storage/files.html