Data storage method 1 in Android: File Format
Summary in Android, there are five data storage methods. Make a note today. How to store data in the form of files.
1. design the layout in activity_main.xml
2. In MainActivity. class, obtain the user-input file name and file information and save the file.
public class MainActivity extends Activity { private Button saveBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); saveBtn = (Button)findViewById(R.id.fileSaveBtn); saveBtn.setOnClickListener(new saveFileListener()); }}
/*** If this class does not need to be inherited, add a final keyword to terminate it. */Private final class saveFileListener implements OnClickListener {@ Overridepublic void onClick (View v) {EditText fileNameText = (EditText) findViewById (R. id. inputName); EditText fileContentText = (EditText) findViewById (R. id. inputContext); String fileName = fileNameText. getText (). toString (); String fileContent = fileContentText. getText (). toString (); FileService fileService = new FileService (getApplicationContext (); try {// save it to fileService on the phone's memory. saveFilePrivate (fileName, fileContent);} catch (Throwable e) {Toast. makeText (MainActivity. this, R. string. fail, Toast. LENGTH_LONG ). show (); e. printStackTrace ();}
}}
3. On the business layer, FileSerivice processes how to save the file content.
Public class FileService {private Context context; // generate the constructor public FileService (Context context) {this. context = context;}/*** save file information * @ param fileName file name * @ param fileContent file content * @ throws Throwable */public void saveFilePrivate (String fileName, String fileContent) throws Exception {// use the output stream object to output the data we need. // Specify the location where the file to be exported is openFileOutput (only file names are accepted, but file paths are not accepted. // specify the operation mode of the file and perform the [append mode] action ], [overwrite mode] // The file has an access permission control. For the owner, the access permission is for other users. // Private operation mode: The file created can only be accessed by this application, other applications cannot access the file. // The File Created in private mode will overwrite the original file content. // It is saved in the files folder where the current application package is located by default. FileOutputStream outputStream = context.openFileOutput(lmfcast.txt, Context. MODE_PRIVATE); // append method // FileOutputStream outputStream = context.openFileOutput(lmfcast.txt, Context. MODE_APPEND); write (fileContent, outputStream);} private void write (String fileContent, FileOutputStream outputStream) throws IOException {outputStream. write (fileContent. getBytes (); outputStream. close ();}}
4. Run the source code:
Summary:
1. The file storage method adopts java IO stream technology.
2. file access modes, including private and append.
3. file operations can be divided into private, apend, read, write, read + write.