关于设备数据的存储和获取。
sd卡绝对路径会因为版本变化为改变,不建议写绝对路径,这涉及到版本的兼容性问题。
SD卡的判断问题 一SD卡是否存在 二 .SD卡是否允许读写数据
//MainActivity
public class MainActivity extends ActionBarActivity {
TextView tv;
EditText filename;
EditText filecontent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bnfilenameone=(Button) this.findViewById(R.id.bnfilenameone);
Button bnfilenametwo=(Button) this.findViewById(R.id.bnfilenametwo);
Button bnfilecontentone=(Button) this.findViewById(R.id.bnfilecontentone);
Button bnfilecontenttwo=(Button) this.findViewById(R.id.bnfilecontenttwo);
filename=(EditText) this.findViewById(R.id.filename);
filecontent=(EditText) this.findViewById(R.id.filecontent);
tv=(TextView) this.findViewById(R.id.tv);
bnfilenameone.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
String fileName=filename.getText().toString();
String fileContent=filecontent.getText().toString();
try {
FileService fileService=new FileService(getApplicationContext());
fileService.save(fileName,fileContent);
Toast.makeText(getApplicationContext(), R.string.filesuccess, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), R.string.filefail, Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
});
bnfilecontentone.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
String fileName=filename.getText().toString();
try {
FileService fileService =new FileService(getApplicationContext());
String text=fileService.read(fileName);
tv.setText(text);
Toast.makeText(getApplicationContext(), R.string.filereadsuccess, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), R.string.filefail, Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
});
bnfilenametwo.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
String fileName=filename.getText().toString();
String fileContent=filecontent.getText().toString();
try {
FileService fileService=new FileService(getApplicationContext());
//判断SD卡是否存在,是否允许读写数据
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
fileService.saveToSDCard(fileName,fileContent);
Toast.makeText(getApplicationContext(), R.string.filesuccessSDCard, Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), R.string.SDCardError, Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
Toast.makeText(getApplicationContext(), R.string.filefailSACard, Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
});
bnfilecontenttwo.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
String fileName=filename.getText().toString();
try {
FileService fileService=new FileService(getApplicationContext());
String con=fileService.readFromSDCard(fileName);
tv.setText(con);
Toast.makeText(getApplicationContext(), R.string.filereadsecsuccess, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), R.string.filereadfail, Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
});
}
}
package com.example.service;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import android.content.Context;
public class FileService {
private Context context;
public FileService(Context context) {
this.context = context;
}
/**
* 文件保存到内部默认位置
* @param fileName 文件名称
* @param fileContent 文件内容
* @throws IOException
*/
public void save(String fileName, String fileContent) throws IOException {
FileOutputStream outStream=context.openFileOutput(fileName, Context.MODE_PRIVATE);
outStream.write(fileContent.getBytes());
outStream.close();
}
/**
* 读取内部默认文件下的内容
* @return
* @throws IOException
*/
public String read(String fileName) throws IOException {
FileInputStream inStream=context.openFileInput(fileName);
byte[] buffer=new byte[1024];
int len=0;
ByteArrayOutputStream data=new ByteArrayOutputStream();
while((len=inStream.read(buffer))!=-1){
data.write( buffer,0,len);
}
byte[]buf=data.toByteArray();
data.close();
return new String(buf);
}
/**
* 保存文件到SDCard
* @param fileName 文件名称
* @param fileContent 文件内容
* @throws IOException
*/
public void saveToSDCard(String fileName, String fileContent) throws IOException {
//File file=new File(new File("/mnt/sdcard"),fileName); //第一个参数,保存的目录, 第二个参数,文件名称
File file=new File(Environment.getExternalStorageDirectory(),fileName);
FileOutputStream outStream=new FileOutputStream(file);
outStream.write(fileContent.getBytes());
outStream.close();
}
/**
* 读取SDCard内容
* @param fileName
* @return
* @throws IOException
*/
public String readFromSDCard(String fileName) throws IOException {
FileInputStream inStream=context.openFileInput(fileName);
byte[] buffer=new byte[1024];
int len=0;
ByteArrayOutputStream outStream=new ByteArrayOutputStream();
while((len=inStream.read(buffer))!=-1){
outStream.write(buffer,0,len);
}
byte[] res=outStream.toByteArray();
outStream.close();
return new String(res);
}
}