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
原创粉丝点击