android存储方式之文件存储
来源:互联网 发布:金税盘开票软件最新版 编辑:程序博客网 时间:2024/04/30 04:24
xml存储:sharepreferences 存放在shared_prefs文件夹下
case R.id.button_write:
/**
* 以xml格式存储
*/
SharedPreferences sharedPreferences = getSharedPreferences("myshare",MODE_PRIVATE);
// SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("data",mEditText.getText().toString());
editor.commit();
break;
case R.id.button_read:
SharedPreferences preferences = getSharedPreferences("myshare",MODE_PRIVATE);
// SharedPreferences preferences = getPreferences(MODE_PRIVATE);
String s = preferences.getString("data","默认值");
mTextView.setText(s);
break;
文件存储:缓存(内部),本地文件存储(外部)
缓存在cache下:
case R.id.button_writecache:
try {
FileOutputStream outputStream = openFileOutput("hellocache.txt", MODE_PRIVATE);
PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream));
writer.write(mEditText.getText().toString());
writer.flush();
writer.close();
outputStream.close();
break;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void readcache() {
try {
FileInputStream inputStream = openFileInput("hellocache.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String line = br.readLine();
mTextView.setText(line);
br.close();
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void writeCacheDir() {
File file = new File(getCacheDir(),"helloworld.txt");
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
FileOutputStream outputStream = new FileOutputStream(file);
PrintWriter writer = new PrintWriter(outputStream);
writer.write(mEditText.getText().toString());
writer.flush();
writer.close();
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void readCacheDir() {
File fileRead =new File(getCacheDir(),"helloworld");
if(!fileRead.exists()){
try {
fileRead.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(fileRead)));
String data = bufferedReader.readLine();
mTextView.setText(data);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}外部存储在Sdcard下,需要添加权限:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />private void readSDcard() {
File file = new File(Environment.getExternalStorageDirectory(),"hellosdcard");
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String data = bufferedReader.readLine();
mTextView.setText(data);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void writeSDcard() {
File file = new File(Environment.getExternalStorageDirectory(),"hellosdcard.txt");
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
FileOutputStream outputStream = new FileOutputStream(file);
PrintWriter writer = new PrintWriter(outputStream);
writer.write(mEditText.getText().toString());
writer.flush();
writer.close();
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
0 0
- Android 存储方式之文件存储
- Android数据存储方式之:文件存储
- android存储方式之文件存储
- Android之文件存储方式
- android数据存储方式之二 文件存储:内部存储
- Android进阶之路 - 四大存储方式之文件存储
- Android学习之文件存储方式
- Android文件存储方式
- Android的5种数据存储方式之文件存储
- android数据存储方式之二 文件存储:外存储
- Android数据存储方式之——文件存储
- Android数据存储方式之文件存储详解
- Android五大存储方式之File文件存储
- android之存储篇_SharedPreferences存储方式
- android之存储篇_SQLite存储方式
- android之存储篇_SharedPreferences存储方式
- Android存储方式之SharedPreference存储
- Android存储方式之内部存储
- hdu 5001 Walk(概率dp+搜索,矩阵快速幂)
- 【算法】九大排序算法总结
- 数组中未出现的最小正整数
- MATLAB中cell函数
- STC单片机使用RS485下载程序方法
- android存储方式之文件存储
- 面试编程加数据库
- Runtime.getRuntime().exec()进程阻塞问题
- Android button按键点击无反应
- github-july-最长回文子串
- Web Service 的工作原理
- android学习笔记(19)数据库sqlite进阶
- NMCUSTOMDRAW
- Linux下配置用msmtp和mutt发邮件