Data storage method 1 in Android: File Format

Source: Internet
Author: User

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.

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.