1.点击按钮将assets文件夹中的内容复制到SD卡中。
private void copyToSD() {
// 检测SD卡是否挂载 Environment.MEDIA_MOUNTED 表示被挂载
// Environment.getExternalStorageState() 将返回sd卡的状态
if(! Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
Toast.makeText(this, "不能读取sd卡", Toast.LENGTH_LONG).show();
return;
}
// 获取sd卡的根位置
String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
Log.e("sdPath" , sdPath);
// 文件复制
String path = sdPath + File.separator + "info" ;
String fileName = "notepad.txt";
// 判断写入的路径是否存在
File pathFile = new File(path);
if(!pathFile.exists() || !pathFile.isDirectory()) {
pathFile.mkdirs(); //创建的是文件目录
}
byte[] readContent = null;
// 读取数据源,得到一个byte[]
try {
InputStream is = getAssets().open("java_notepad");
ByteArrayOutputStream bos = new ByteArrayOutputStream(); //减少打开文件的次数
byte[] cache = new byte[1024];
int count ;
while((count = is.read(cache, 0, 1024)) != -1) {
bos.write(cache, 0, count); // 写入内存
}
readContent = bos.toByteArray(); // 将内存数据转入byte数组
is.close();
bos.close();
} catch (Exception e1) {
e1.printStackTrace();
Toast.makeText(this, "读取数据源失败!", Toast.LENGTH_LONG).show();
return;
}
// 创建一个输出流
try {
if(null == readContent || readContent.length == 0) {
Toast.makeText(this, "数据源没有数据!", Toast.LENGTH_LONG).show();
return;
}
FileOutputStream fos = new FileOutputStream(path + File.separator + fileName);
fos.write(readContent);
fos.close();
Toast.makeText(this, "复制成功!", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "复制到sd卡失败!", Toast.LENGTH_LONG).show();
return;
}
}
2.向SD卡中写入数据
private void writeToSD() {
// 判断是否挂载
if(!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
Toast.makeText(this, "不能读取sd卡", Toast.LENGTH_LONG).show();
return;
}
// 得到sd卡的根路径
String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
// 写入数据
String path = sdPath + File.separator + "info";
String fileName = "temp.txt";
// 判断写入的路径是否存在
File pathFile = new File(path);
if(!pathFile.exists() || !pathFile.isDirectory()) {
pathFile.mkdirs();
}
// 写入
try {
FileWriter fw = new FileWriter(path + File.separator + fileName); //因为要直接写入字符,所以可以用writer
fw.write("Hello\n");
fw.write("张老师,你咋还不来内!我们都想念你。。。。");
fw.close();
Toast.makeText(this, "写入成功!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, "写入数据到sd卡失败!", Toast.LENGTH_LONG).show();
return;
}
}
3.从SD卡中读取数据
private void readFromSD() {
// 读取sd卡数据 ==> OutputStream ==> byte[]
// 判断是否挂载
if(!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
Toast.makeText(this, "不能读取sd卡", Toast.LENGTH_LONG).show();
return;
}
// 得到sd卡的根路径
String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
// 读取数据
String fileName = sdPath + File.separator + "info" + File.separator + "temp.txt";
File file = new File(fileName);
if(!file.exists() || !file.isFile() ) {
Toast.makeText(this, "不能读取sd卡文件!" , Toast.LENGTH_LONG).show();
return;
}
// 读取数据源,得到一个byte[]
byte[] readContent = null;
try {
InputStream is = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] cache = new byte[1024];
int count ;
while((count = is.read(cache, 0, 1024)) != -1) {
bos.write(cache, 0, count); // 写入内存
}
readContent = bos.toByteArray(); // 将内存数据转入byte数组
is.close();
bos.close();
} catch (Exception e1) {
e1.printStackTrace();
Toast.makeText(this, "读取数据源失败!", Toast.LENGTH_LONG).show();
return;
}
TextView tv = (TextView) findViewById(R.id.show);
if(null == readContent || readContent.length == 0) {
tv.setText("没有读取数据!");
}
else {
try {
tv.setText(new String(readContent , "utf-8"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
对SD卡操作的一些封装方法:
public class SDTool {
// 成员常量
public static final String SD_PATH = Environment.getExternalStorageDirectory().getAbsolutePath();
// 判断sd卡是否可用
public static boolean isAvailable() {
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ;
}
// 读取数据,读出数据放到byte数组里面。
public static byte[] readFile(String path , String fileName) {
if(!isAvailable()) {
return null;
}
// 判断文件是否存在
File file = new File(SD_PATH + File.separator + path + File.separator + fileName);
if(!file.exists() || !file.isFile()) {
return null;
}
// 读取文件内容
byte[] content = null;
try {
InputStream is = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] temp = new byte[1024];
int size ;
while((size = is.read(temp, 0, 1024)) != -1) {
bos.write(temp, 0, size);
}
content = bos.toByteArray();
bos.close();
is.close();
return content;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
// 写入数据
public static boolean writeFile(String path , String fileName , String content , boolean isAppend) {
if(!isAvailable()) {
return false;
}
// 判断路径是否存在
File file = new File(SD_PATH + File.separator + path);
if(!file.exists() || !file.isDirectory()) {
file.mkdirs();
}
// 写入数据
try {
FileWriter fw = new FileWriter(SD_PATH + File.separator + path + File.separator + fileName , isAppend) ;
fw.write(content);
fw.close();
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
public static boolean writeFile(String path , String fileName , byte[] content , boolean isAppend) {
if(!isAvailable()) {
return false;
}
// 判断路径是否存在
File file = new File(SD_PATH + File.separator + path);
if(!file.exists() || !file.isDirectory()) {
file.mkdirs();
}
// 写入数据
try {
FileOutputStream fw = new FileOutputStream(SD_PATH + File.separator + path + File.separator + fileName , isAppend) ;
fw.write(content);
fw.close();
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
public static boolean writeFile(String path , String fileName , Object content , boolean isAppend) {
return writeFile(path, fileName, content.toString() , isAppend);
}
// 删除文件或目录
public static boolean deleteFile(String fileName) {
return new File(SD_PATH + File.separator + fileName).delete();
}
}