SCID: L0030
1. Save the data to a private file:
String FILE_NAME = "hello_file";String string = "hello world!";FileOutputStream fos = this.openFileOutput(FILE_NAME, Context.MODE_PRIVATE);fos.write(string.getBytes());fos.close();
(1) openFileOutput opens a file output stream. this is the Context object related to the application. The first parameter is FILE_NAME, which specifies the file name. The second parameter indicates the opening method, Context. MODE_PRIVATE indicates that the file is only visible to the application. Other applications cannot operate on the file. the optional types of the second parameter are as follows:
MODE_APPEND: if the file already exists, write the data instead of wiping the end of the existing file.
MODE_PRIVATE: default mode. The created file can only be called by the application, that is, Private. Other programs cannot view and change the configuration content.
MODE_WORLD_READABLE: allows all other applications to read and create files.
MODE_WORLD_WRITEABLE: allows all other applications to write, access, and create files.
The openFileOutput statement is equivalent to the following statement.
File file = new File (FILE_PATH, FILE_NAME );
File. createNewFile ();
FileOutputStream fos = new FileOutputStream (file );
Here, FILE_PATH is the full path name of the file, such as/data/com. L0030/
(2) Call the write () operation to write data to a file.
(3) remember to close the file stream after writing the data!
2. Read private file data:
String FILE_NAME = "hello_file"; FileInputStream Fi = this. openFileInput (FILE_NAME); byte [] buffer = new byte [1024]; int len = 0; ByteArrayOutputStream fos = new ByteArrayOutputStream (); while (len = Fi. read (buffer ))! =-1) {fos. write (buffer, 0, len);} byte [] data = fos. toByteArray (); // obtain the binary data of the file. close (); fos. close ();
(1) openFileInput () opens an input stream. this is the Context object related to the application.
The openFileInput statement is equivalent to the following statement:
File file = new File (FILE_PATH, FILE_NAME );
In = new FileInputStream (file );
Here, FILE_PATH is the full path name of the file, such as/data/com. L0030/
(2) read data cyclically, read a maximum of 1024 bytes at a time, and put the read data into a byte output stream.
(3) Call the toByteArray () function to obtain the output data of the entire file.
(4) do not forget to close the input stream and output stream!
3. Read static files:
InputStream ins = getResources().openRawResource(R.raw.my_file_name);...
(1) Save the files to be operated to the res/raw/directory of the Project. open the file through openRawResource () API. Other operations are the same as reading the private file data stream.
4. extended storage file operations:
Android devices support shared extended storage, which can store data on these devices. Generally, these devices are SD cards!
boolean externalStorageAvailable = false;boolean externalStorageWriteable = false;String state = Environment.getExternalStorageState();if (Environment.MEDIA_MOUNTED.equals(state)) {// We can read and write the mediaexternalStorageAvailable = mExternalStorageWriteable = true;} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {// We can only read the mediaexternalStorageAvailable = true;externalStorageWriteable = false;} else {// Something else is wrong. It may be one of many other states, but all we need// to know is we can neither read nor writeexternalStorageAvailable = externalStorageWriteable = false;}if (externalStorageAvailable && externalStorageWriteable) {File file = new File(Environment.getExternalStorageDirectory(), filename);FileOutputStream outStream = new FileOutputStream(file);outStream.write(content.getBytes());outStream.close();}
(1) Environment. getExternalStorageState () obtains the status of the current extended storage. Before file operations, you must check whether the device exists and perform operations properly!
(2) Environment. getExternalStorageDirectory () obtains the root directory of the storage File, and creates a File object by the Directory and File name. For Android systems of API L8 or above, you can use getExternalFilesDir () to directly obtain public directories that can be seen by end users! Of course, using getExternalStorageDirectory () can achieve this effect!
(3) create a FileOutputStream object associated with the File and write the content data in binary mode to the File named filename.
(4) Close the output stream.