Chinaunix首页 | 论坛 | 博客
  • 博客访问: 896112
  • 博文数量: 322
  • 博客积分: 6688
  • 博客等级: 准将
  • 技术积分: 3626
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-19 11:26
文章分类

全部博文(322)

文章存档

2013年(5)

2012年(66)

2011年(87)

2010年(164)

分类: Java

2010-12-31 11:13:33

数据访问

DataActivity

Java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package org.wp.file;  
 
import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileOutputStream;  
import java.io.InputStream;  
import org.wp.service.FileService;  
import android.app.Activity;  
import android.os.Bundle;  
import android.os.Environment;  
import android.util.Log;  
import android.view.View;  
import android.widget.Button;  
import android.widget.EditText;  
import android.widget.TextView;  
import android.widget.Toast;  
 
/** 
 *  
 * Activity还提供了getCacheDir()和getFilesDir()方法: 
 * getCacheDir()方法用于获取/data/data//cache目录 
 * getFilesDir()方法用于获取/data/data//files目录 
 *  
 *  
 *  
 *  
 *  
 *  
 * DOS下创建SDCard 
 * 进入Android SDK安装路径的tools目录 
 * 文件后缀可以随便取 
 * 建议使用.img 
 * mksdcard 2048M D:\AndroidTool\sdcard.img 
 *  
 * @author wp 
 * 
 */  
 
public class DataActivity extends Activity {  
    private static final String TAG = "DataActivity";  
    private EditText filenameText;  
    private EditText contentText;  
    private TextView resultView;  
    private Button saveButton;  
    private Button readButton;  
 
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        filenameText = (EditText) this.findViewById(R.id.filename);  
        contentText = (EditText) this.findViewById(R.id.content);  
        resultView = (TextView) this.findViewById(R.id.result);  
        saveButton = (Button) this.findViewById(R.id.save);  
        readButton = (Button) this.findViewById(R.id.read);  
        saveButton.setOnClickListener(listener);  
        readButton.setOnClickListener(listener);  
    }  
 
    private View.OnClickListener listener = new View.OnClickListener() {  
        @Override  
        public void onClick(View arg0) {  
            Button button = (Button) arg0;  
            String filename = filenameText.getText().toString();  
            File file = new File(Environment.getExternalStorageDirectory(),  
                    filename);  
            // File file = new File("/sdcard" + filename);  
            // Environment.getExternalStorageDirectory();  
 
            switch (button.getId()) {  
            case R.id.save:  
                // SDCard存在,并且可以进行读写  
                if (Environment.getExternalStorageState().equals(  
                        Environment.MEDIA_MOUNTED)) {  
                    int resId = R.string.success;  
                    String content = contentText.getText().toString();  
                    try {  
                        FileOutputStream outStream = new FileOutputStream(file);  
                        FileService.save(outStream, content);  
                    } catch (Exception e) {  
                        Log.e(TAG, e.toString());  
                        resId = R.string.error;  
                    }  
                    Toast.makeText(DataActivity.this, resId, Toast.LENGTH_LONG)  
                            .show();  
                } else {  
                    Toast.makeText(DataActivity.this, "SDCard不存在或者写保护",  
                            Toast.LENGTH_LONG).show();  
                }  
                break;  
            case R.id.read:  
                try {  
                    InputStream inStream = new FileInputStream(file);  
                    String text = FileService.read(inStream);  
                    resultView.setText(text);  
                } catch (Exception e) {  
                    Log.e(TAG, e.toString());  
                    Toast.makeText(DataActivity.this, "读取失败",  
                                    Toast.LENGTH_LONG).show();  
                }  
                break;  
            }  
        }  
    };  
}

FileService

Java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package org.wp.service;  
 
import java.io.ByteArrayOutputStream;  
import java.io.InputStream;  
import java.io.OutputStream;  
 
public class FileService {  
 
    /** 
     * 保存数据 
     *  
     * @param outStream 
     * @param content 
     * @throws Exception 
     */  
    public static void save(OutputStream outStream, String content)  
            throws Exception {  
        outStream.write(content.getBytes());  
        outStream.close();  
    }  
 
    /** 
     * 读取数据 
     *  
     * @param inStream 
     * @return 
     * @throws Exception 
     */  
    public static String read(InputStream inStream) throws Exception {  
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
        byte[] buffer = new byte[1024];  
        int len = -1;  
        while ((len = inStream.read(buffer)) != -1) {  
            outStream.write(buffer, 0, len);  
        }  
        byte[] data = outStream.toByteArray();  
        outStream.close();  
        inStream.close();  
        return new String(data);  
    }  
}

main.xml

Java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
 version="1.0" encoding="utf-8"?>  
 xmlns:android=""  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    >  
     xmlns:android=""  
        android:orientation="vertical"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        >  
            
            android:layout_width="wrap_content"   
            android:layout_height="wrap_content"   
            android:text="@string/filename"  
            android:id="@+id/filenameLable"  
        />  
          
            android:layout_width="200px"  
            android:layout_height="wrap_content"  
            android:layout_alignTop="@id/filenameLable"  
            android:layout_toRightOf="@id/filenameLable"  
            android:layout_marginLeft="10px"  
            android:text="wp.txt"  
            android:id="@+id/filename"  
            />  
    >  
      
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="@string/content"  
        />  
      
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:minLines="3"  
        android:id="@+id/content"  
        />  
     xmlns:android=""  
        android:orientation="vertical"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        >  
           
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="@string/save"  
            android:id="@+id/save"  
            />  
          
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="@string/read"  
            android:layout_toRightOf="@id/save"  
            android:layout_alignTop="@id/save"  
            android:id="@+id/read"  
            />     
    >  
      
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:id="@+id/result"  
        />  
>

strings.xml

Xml代码

1
2
3
4
5
6
7
8
9
10
11
 version="1.0" encoding="utf-8"?>  
>  
     name="hello">Hello World, DataActivity!>  
     name="app_name">数据保存>  
     name="filename">文件名称>  
     name="content">文件内容>  
     name="save">保存>  
     name="read">读取文件内容>  
     name="success">保存成功>  
     name="error">保存失败>  
>

AndroidManifest.xml

Xml代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 version="1.0" encoding="utf-8"?>  
 xmlns:android=""  
    package="org.wp.file" android:versionCode="1" android:versionName="1.0">  
     android:icon="@drawable/icon" android:label="@string/app_name">  
         android:name="android.test.runner" />  
         android:name=".DataActivity" android:label="@string/app_name">  
            >  
                 android:name="android.intent.action.MAIN" />  
                 android:name="android.intent.category.LAUNCHER" />  
            >  
        >  
    >  
     android:minSdkVersion="7" />  
     android:name="android.test.InstrumentationTestRunner"  
        android:targetPackage="org.wp.file" android:label="Tests for My App" />  
      
     android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />  
      
     android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
>
阅读(677) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~