Android 文件存储详解(SD卡或手机内存存储)
2012-06-10 07:48:59 来源:WEB开发网package gongzibai.co.cc;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Ex6Activity extends Activity {
/** Called when the activity is first created. */
final String FILE = "jason.bin";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button read = (Button) findViewById(R.id.read);
Button write = (Button) findViewById(R.id.write);
final EditText edit1 = (EditText) findViewById(R.id.edit1);
final EditText edit2 = (EditText) findViewById(R.id.edit2);
write.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
write(edit1.getText().toString());
edit1.setText("");
}
private void write(String string) {
// TODO Auto-generated method stub
try {
FileOutputStream fos = openFileOutput(FILE, MODE_APPEND);
PrintStream ps = new PrintStream(fos);
ps.println(string);
ps.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
});
read.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
edit2.setText(read());
}
private CharSequence read() {
// TODO Auto-generated method stub
try {
FileInputStream fis = openFileInput(FILE);
byte[] buffer = new byte[1024];
int hasRead = 0;
StringBuilder sb = new StringBuilder("");
while ((hasRead = fis.read(buffer)) > 0) {
sb.append(new String(buffer, 0, hasRead));
}
return sb.toString();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
});
}
}
----------------创建文件到SD卡中
File file = new File("/sdcard/json.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
-------------------存储在SD卡中数据
package gongzibai.co.cc;
import java.io.File;
import java.io.FileInputStream;
- ››Android 当修改一些代码时,使用什么编译命令可以最...
- ››Android 如何添加一个apk使模拟器和真机都编译进去...
- ››Android 修改Camera拍照的默认保存路径
- ››Android 如何修改默认输入法
- ››android开发中finish()和System.exit(0)的区别
- ››Android手势识别简单封装类
- ››android中查看项目数字证书的两种方法
- ››Android中获取IMEI码的办法
- ››android 相机报错 setParameters failed
- ››Android重启运用程序的代码
- ››Android为ListView的Item设置不同的布局
- ››android bitmap与base64字符串的互相转换