打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
存储获取数据(SDCard)
关于设备数据的存储和获取。 
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);
}
}
 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
10. 声音模式管理(AudioManager)
Android开发中的MVC设计模式
Tip:如何指定浏览器下载并保存动态生成的数据时对话框里的默认文件名
在Android中实现文件读写 - 狂徒的 - ITeye技术网站
C# 读写文本文件乱码解决方案
Toast.makeText() 的使用方法
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服