android读写SD卡封装的类

时间:2023-03-09 16:34:34
android读写SD卡封装的类

参考了网上的一些资源代码,FileUtils.java:

package com.example.test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import android.os.Environment; public class FileUtils {
private String SDCardRoot;
private String SDStateString; /**
* ----------------注意权限的添加----------------
*/
public FileUtils() {
// 得到当前外部存储设备的目录
SDCardRoot = Environment.getExternalStorageDirectory()
.getAbsolutePath() + File.separator;
SDStateString = Environment.getExternalStorageState();// 获取扩展SD卡设备状态
} /**
* 在SD卡上创建文件
*
* @param fileName
* @param dir
* 目录路径
* @return
* @throws IOException
*/
public File createFileInSDCard(String fileName, String dir)
throws IOException {
File file = new File(SDCardRoot + dir + File.separator + fileName);
System.out.println("file---->" + file);
file.createNewFile();
return file;
} /**
* 在SD卡上创建目录
*
* @param dir
* 目录路径,相当于文件夹
* @return
*/
public File creatSDDir(String dir) {
File dirFile = new File(SDCardRoot + dir + File.separator);
// System.out.println(dirFile.mkdirs());
if (!dirFile.exists()) {
dirFile.mkdirs();
}
return dirFile;
} /**
* 判断SD卡上的文件夹是否存在
*
* @param fileName
* 文件名称
* @param path
* 目录路径
* @return
*/
public boolean isFileExist(String fileName, String path) {
File file = new File(SDCardRoot + path + File.separator + fileName);
return file.exists();
} /**
* 将一个字节数组数据写入到SD卡中
*
* @param dir
* 目录
* @param fileName
* @param bytes
* @return
*/
public boolean writeSDCard(String dir, String fileName, byte[] bytes) {
if (bytes == null) {
return false;
}
OutputStream outputStream = null;
if (SDStateString.equals(android.os.Environment.MEDIA_MOUNTED)) {
// File file = null;
creatSDDir(dir);
try {
File file = createFileInSDCard(fileName, dir);
outputStream = new BufferedOutputStream(new FileOutputStream(
file));
outputStream.write(bytes);
outputStream.flush();
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} }
return false;
} /**
* 将一个InputStream里面的数据写入到SD卡中
*/
public File write2SDFromInput(String path, String fileName,
InputStream input) { File file = null;
OutputStream output = null;
try {
creatSDDir(path);
file = createFileInSDCard(fileName, path);
output = new FileOutputStream(file);
byte buffer[] = new byte[4 * 1024];
int temp;
while ((temp = input.read(buffer)) != -1) {
output.write(buffer, 0, temp);
}
output.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
output.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return file;
} /**
* 读取文件
*
* @param dir
* 所在目录
* @param fileName
* @return
*/
public String loadData(String dir, String fileName) {
File file = new File(SDCardRoot + dir + File.separator + fileName);
if (!file.exists()) {
return null;
}
InputStream inputStream = null;
try {
inputStream = new BufferedInputStream(new FileInputStream(file));
byte[] data = new byte[inputStream.available()];
inputStream.read(data);
return new String(data);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO: handle exception
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
} /**2015-7-30 by chen tong
* 使用FileWriter在文件末尾添加内容
*/
public void appendContent(File file, String content) {
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file, true);
byte[] enter = new byte[2];
enter[0] = 0x0d;
enter[1] = 0x0a;// 用于输入换行符的字节码
String finalString = new String(enter);// 将该字节码转化为字符串类型
content = content + finalString;
outputStream.write(content.getBytes());
outputStream.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
}

调用就很简单了:

FileUtils fileUtils = new FileUtils();
System.out.println(fileUtils.loadData("GPS信息", "test.txt"));//前面是目录文件夹名称,后面是文件的名称
if (!fileUtils.isFileExist("Bluetooth温度数据", "")) {
fileUtils.creatSDDir("Bluetooth温度数据");
}
try {
File file = fileUtils.createFileInSDCard("数据1.txt", "Bluetooth温度数据");//在sd下创建"Bluetooth温度数据"文件夹,并创建数据1.txt文件
fileUtils.appendContent(file, "ss");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

********************************************************************************************分割线******************************************************************************************************************

一开始以为需要使用二维数组的方式按照一定的格式写书数据到文本中,但是后来想了下,不需要合并两个一数组了,下面看代码,我是在java项目中写了,很简单:

import java.io.File;
import java.io.FileWriter;
import java.io.IOException; public class ValueWrite { public static void main(String[] args) {
// TODO Auto-generated method stub
double[] aa = {1.52,2.36,3.52,4.21};
double[] bb = {2.81,4.35,6.32,8.96};
int n = aa.length;
File file = new File("/media/stroe_room/桌面/test.txt");
try {
FileWriter out = new FileWriter(file);
for (int i = 0; i < n; i++) {
out.write(aa[i]+"\t"+bb[i]+"\r\n");//“\t”表示的是TAB,而“\r\n”表示的是回车换行
}
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } }

最终的效果图:

android读写SD卡封装的类