Android File data storage

Source: Internet
Author: User

 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6 <EditText
7 android:id="@+id/write_edit"
8 android:layout_width="fill_parent"
9 android:layout_height="wrap_content"
10 android:lines="4"
11 android:gravity="top"
12 />
13 <Button
14 android:layout_width="wrap_content"
15 android:layout_height="wrap_content"
16 android:text="Save"
17 android:onClick="writeFile"
18 />
19 <EditText
20 android:layout_width="fill_parent"
21 android:layout_height="wrap_content"
22 android:lines="4"
23 android:id="@+id/read_edit"
24 android:gravity="top"
25 android:editable="false"
26 android:focusable="false"
27 />
28 <Button
29 android:layout_width="wrap_content"
30 android:layout_height="wrap_content"
31 android:text="Read"
32 android:onClick="readFile"
33 />
34 </LinearLayout>

 

1 package com. turboradio. activity;
2
3 import java. io. FileInputStream;
4 import java. io. FileNotFoundException;
5 import java. io. FileOutputStream;
6 import java. io. IOException;
7
8 import android. app. Activity;
9 import android. OS. Bundle;
10 import android. view. View;
11 import android. widget. EditText;
12
13 public class FileSaveActivity extends Activity {
14 // file name
15 private static final String FILE_NAME = "temp.txt ";
16 private EditText writeEdit;
17 private EditText readEdit;
18 @ Override
19 protected void onCreate (Bundle savedInstanceState ){
20 super. onCreate (savedInstanceState );
21 setContentView (R. layout. file_save );
22 writeEdit = (EditText) findViewById (R. id. write_edit );
23 readEdit = (EditText) findViewById (R. id. read_edit );
24}
25 /**
26 * write files
27 */
28 public void writeFile (View v ){
29 write (writeEdit. getText (). toString ());
30}
31 /**
32 * read files
33 */
34 public void readFile (View v ){
35 readEdit. setText (read ());
36}
37 private String read (){
38 try {
39 FileInputStream FCM = openFileInput (FILE_NAME );
40 byte [] bytes = new byte [1, 1024];
41. read (bytes );
42 return new String (bytes );
43} catch (FileNotFoundException e ){
44 e. printStackTrace ();
45} catch (IOException e ){
46 // TODO Auto-generated catch block
47 e. printStackTrace ();
48}
49 return null;
50}
51 private void write (String content ){
52 try {
53 FileOutputStream fos = openFileOutput (FILE_NAME, MODE_APPEND );
54 fos. write (content. getBytes ());
55 fos. close ();
56} catch (FileNotFoundException e ){
57 e. printStackTrace ();
58} catch (IOException e ){
59 e. printStackTrace ();
60}
61}
62}

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.