Android实现SD卡和实现内存文件存储的做法基本是一样的。只是取得文件路径的方法不一样,基本上跟java的文件操作是一致的,下面是程序的代码。配置取的位置和实现有所不同,其他是一样的,下面是代码:
main.xml:
01 |
<? xml version = "1.0" encoding = "utf-8" ?> |
02 |
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
03 |
android:orientation = "vertical" |
04 |
android:layout_width = "fill_parent" |
05 |
android:layout_height = "fill_parent" > |
06 |
< EditText android:id = "@+id/edit1" |
07 |
android:layout_width = "fill_parent" |
08 |
android:layout_height = "wrap_content" |
10 |
< Button android:id = "@+id/write" |
11 |
android:layout_width = "wrap_content" |
12 |
android:layout_height = "wrap_content" |
13 |
android:text = "@string/write" /> |
14 |
< EditText android:id = "@+id/edit2" |
15 |
android:layout_width = "fill_parent" |
16 |
android:layout_height = "wrap_content" |
17 |
android:editable = "false" |
18 |
android:cursorVisible = "false" |
20 |
< Button android:id = "@+id/read" |
21 |
android:layout_width = "wrap_content" |
22 |
android:layout_height = "wrap_content" |
23 |
android:text = "@string/read" /> |
在AndroidManifest清单文件中要对SD卡读取进行支持。代码如下:
1 |
< uses-permission android:name = "android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> |
2 |
< uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE" /> |
在String.xml中添加两个内容:
1 |
< string name = "read" >读取</ string > |
2 |
lt;string name="write">写入</ string > |
下面是文件实现的代码:
03 |
import java.io.FileInputStream; |
04 |
import java.io.FileOutputStream; |
05 |
import java.io.IOException; |
06 |
import java.io.PrintStream; |
08 |
import android.app.Activity; |
09 |
import android.os.Bundle; |
10 |
import android.view.View; |
11 |
import android.view.View.OnClickListener; |
12 |
import android.widget.Button; |
13 |
import android.widget.EditText; |
15 |
public class FileTestActivity extends Activity { |
17 |
final String FILE = "jason.bin" ; |
20 |
public void onCreate(Bundle savedInstanceState) { |
21 |
super .onCreate(savedInstanceState); |
22 |
setContentView(R.layout.main); |
24 |
Button read = (Button) findViewById(R.id.read); |
25 |
Button write = (Button) findViewById(R.id.write); |
27 |
final EditText edit1 = (EditText) findViewById(R.id.edit1); |
28 |
final EditText edit2 = (EditText) findViewById(R.id.edit2); |
31 |
write.setOnClickListener( new OnClickListener() { |
34 |
public void onClick(View v) { |
35 |
write(edit1.getText().toString()); |
40 |
read.setOnClickListener( new OnClickListener() { |
43 |
public void onClick(View v) { |
44 |
edit2.setText(read()); |
50 |
private String read() { |
52 |
FileInputStream fis = openFileInput(FILE); |
53 |
byte [] buffer = new byte [ 1024 ]; |
55 |
StringBuilder sb = new StringBuilder( "" ); |
56 |
while ((hasRead = fis.read(buffer)) > 0 ) { |
57 |
sb.append( new String(buffer, 0 , hasRead)); |
60 |
} catch (IOException e) { |
66 |
private void write(String content) |
71 |
FileOutputStream fos = openFileOutput(FILE, MODE_APPEND); |
73 |
PrintStream ps = new PrintStream(fos); |
下面是sd卡存储实现的代码:
03 |
import java.io.BufferedReader; |
05 |
import java.io.FileInputStream; |
06 |
import java.io.FileNotFoundException; |
07 |
import java.io.IOException; |
08 |
import java.io.InputStreamReader; |
09 |
import java.io.RandomAccessFile; |
10 |
import java.net.ContentHandler; |
12 |
import android.app.Activity; |
13 |
import android.os.Bundle; |
14 |
import android.os.Environment; |
15 |
import android.view.View; |
16 |
import android.view.View.OnClickListener; |
17 |
import android.widget.Button; |
18 |
import android.widget.EditText; |
20 |
public class SDCardTestActivity extends Activity { |
22 |
final String FILE = "/jason.bin" ; |
25 |
public void onCreate(Bundle savedInstanceState) { |
26 |
super .onCreate(savedInstanceState); |
27 |
setContentView(R.layout.main); |
29 |
Button read = (Button) findViewById(R.id.read); |
30 |
Button write = (Button) findViewById(R.id.write); |
32 |
final EditText edit1 = (EditText) findViewById(R.id.edit1); |
33 |
final EditText edit2 = (EditText) findViewById(R.id.edit2); |
35 |
write.setOnClickListener( new OnClickListener() { |
37 |
public void onClick(View source) { |
39 |
write(edit1.getText().toString()); |
44 |
read.setOnClickListener( new OnClickListener() { |
46 |
public void onClick(View v) { |
48 |
edit2.setText(read()); |
53 |
private String read() { |
57 |
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { |
59 |
File sdDirFile = Environment.getExternalStorageDirectory(); |
61 |
FileInputStream fis = new FileInputStream(sdDirFile.getCanonicalPath()+ FILE); |
63 |
BufferedReader br = new BufferedReader( new InputStreamReader(fis)); |
65 |
StringBuilder sb = new StringBuilder( "" ); |
67 |
while ((line =br.readLine())!= null ) { |
72 |
} catch (FileNotFoundException e) { |
74 |
} catch (IOException e) { |
80 |
private void write(String context){ |
82 |
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ |
83 |
File sdDir = Environment.getExternalStorageDirectory(); |
84 |
File targetFile = new File(sdDir.getCanonicalPath()+ FILE); |
85 |
RandomAccessFile raf = new RandomAccessFile(targetFile, "rw" ); |
86 |
raf.seek(targetFile.length()); |
87 |
raf.write(context.getBytes()); |
90 |
} catch (Exception e) { |
转自:http://blog.csdn.net/jasonchris/article/details/7619468