File存储
之前有些过关于file存储的博客,这里仅作为补充,下面引入一下我之前的博客。
内部存储
外部存储
检测SD卡
内部存储
我们所说的内部存储是指手机内部卡的存储,存储方式有两种,
一种是通过file=openFileOutput()获得,将数据存储在data/data/+包名+files下面。
一种是通过getCacheDir()来获得存储路径,将数据写在data/data/+包名+cache下面,第二种方式会有一个最后修改时间,就好像我们加载的新闻关了网之后当时能看,第二天或者是什么时候就不能看了,就是因为缓存在cache内,这个路径下的东西可能会自动删除。
使用方法(只是获取的方式不同,路径不同)
方式一
FileInputStream is=openFileInput("hellocache");
//路径:data/data/+包名+files
方式二
File file=new File(getCacheDir(),"hei.txt");
//路径:data/data/+包名+cache
外部存储
关于外部存储大部分人认为是对SD卡的操作,现在我也还没太搞清楚,因为在真机上运行的时候它会加载在手机内存中就是我们常见的\storage\emulated\0路径下。
关于外部存储的路径我们通过Environment.getExternalStorageDirectory()来获得
使用方法
File file=new File(Environment.getExternalStorageDirectory(),"hellosdcard");
具体实例
关于具体例子大家可以参考代码自己去实现一下。
Activity
package com.example.sharedpreferencedemo;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private EditText met;
private TextView mtv;
private Button mbtn_read;
private Button mbtn_write;
private Button mbtn_write_cache_file;
private Button mbtn_read_cache_file;
private Button mbtn_write_cache;
private Button mbtn_read_cache;
private Button mbtn_read_sdcard;
private Button mbtn_write_sdcard;
private Button mbtn_write_sd;
private SharedPreferences preference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findviewid();
setlistener();
}
private void setlistener() {
mbtn_read.setOnClickListener(this);
mbtn_write.setOnClickListener(this);
mbtn_write_cache_file.setOnClickListener(this);
mbtn_read_cache_file.setOnClickListener(this);
mbtn_write_cache.setOnClickListener(this);
mbtn_read_cache.setOnClickListener(this);
mbtn_read_sdcard.setOnClickListener(this);
mbtn_write_sdcard.setOnClickListener(this);
}
private void findviewid() {
met=(EditText) findViewById(R.id.et_write);
mtv=(TextView) findViewById(R.id.tv_read);
mbtn_read=(Button) findViewById(R.id.bt_read);
mbtn_write=(Button) findViewById(R.id.bt_write);
mbtn_write_cache_file=(Button) findViewById(R.id.bt_write_cache_file);
mbtn_read_cache_file=(Button) findViewById(R.id.bt_read_cache_file);
mbtn_write_cache=(Button) findViewById(R.id.bt_write_cache);
mbtn_read_cache=(Button) findViewById(R.id.bt_read_cache);
mbtn_read_sdcard=(Button) findViewById(R.id.bt_read_sdcard);
mbtn_write_sdcard=(Button) findViewById(R.id.bt_write_sdcard);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_read:
readsharedpreference();
break;
case R.id.bt_write:
writesharedpreference();
break;
case R.id.bt_write_cache_file:
writecachefile();
break;
case R.id.bt_read_cache_file:
reacachefile();
break;
case R.id.bt_write_cache:
writecache();
break;
case R.id.bt_read_cache:
readfromcache();
break;
case R.id.bt_read_sdcard:
readsdcard();
break;
case R.id.bt_write_sdcard:
writesdcard();
break;
default:
break;
}
}
private void writesdcard() {
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File file=new File(Environment.getExternalStorageDirectory(),"hellosdcard");
Log.d("路径", Environment.getExternalStorageDirectory().getAbsolutePath());
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
FileOutputStream oscache=new FileOutputStream(file);
PrintWriter writecache=new PrintWriter(new OutputStreamWriter(oscache));
oscache.write("你好cache".getBytes());
oscache.flush();
oscache.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void readsdcard() {
File file=new File(Environment.getExternalStorageDirectory(),"hellosdcard");
try {
FileInputStream is=new FileInputStream(file);
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String s=br.readLine();
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
while(s!=null){
s=br.readLine();
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
mtv.setText(s);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void readfromcache() {
File file=new File(getCacheDir(),"hei.txt");
try {
FileInputStream is=new FileInputStream(file);
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String s=br.readLine();
while(s!=null){
s=br.readLine();
mtv.setText(s);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void writecache() {
File file=new File(getCacheDir(),"hei.txt");
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
FileOutputStream oscache=new FileOutputStream(file);
PrintWriter writecache=new PrintWriter(new OutputStreamWriter(oscache));
writecache.write("你好cache");
writecache.flush();
writecache.close();
oscache.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void reacachefile() {
try {
FileInputStream is=openFileInput("hellocache");
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String line=br.readLine();
while(line!=null){
line=br.readLine();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void writecachefile() {
try {
FileOutputStream os=openFileOutput("hellocache", MODE_PRIVATE);
PrintWriter pintwrite=new PrintWriter(new OutputStreamWriter(os));
pintwrite.write("你好缓存");
pintwrite.flush();
pintwrite.close();
os.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void writesharedpreference() {
preference=getPreferences(MODE_PRIVATE);
Editor editor=preference.edit();
editor.putString("edittext", met.getText().toString());
editor.commit();
}
private void readsharedpreference() {
readsharedpreference();
preference=getPreferences(MODE_PRIVATE);
String s=preference.getString("edittext", "空");
mtv.setText(s);
}
}
备注
在上面的图片中我们所看到的legacy文件夹下是我们的手机内存的存储文件;sdcard1是我们外部SD卡。