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卡封装的类的更多相关文章
- Android 读写SD卡的文件
今天介绍一下Android 读写SD卡的文件,要读写SD卡上的文件,首先需要判断是否存在SD卡,方法: Environment.getExternalStorageState().equals(Env ...
- android 读写sd卡的权限设置
原文:android 读写sd卡的权限设置 在Android中,要模拟SD卡,要首先使用adb的mksdcard命令来建立SD卡的镜像,如何建立,大家上网查一下吧,应该很容易找到,这里不说这个问题. ...
- Android读写SD卡
SD卡的读写是我们在开发Android 应用程序过程中最常见的操作.下面介绍SD卡的读写操作方式: 1. 获取SD卡的根目录 String sdCardRoot = Environment.getEx ...
- android 读写SD卡文件
参考: http://www.oschina.net/code/snippet_176897_7336#11699 写文件: private void SavedToText(Context cont ...
- android学习笔记47——读写SD卡上的文件
读写SD卡上的文件 通过Context的openFileInput.openFileOutput来打开文件输入流.输出流时,程序打开的都是应用程序的数据文件夹里的文件,其存储的文件大小可能都比较有限- ...
- Android 常见SD卡操作
目录 Android 常见SD卡操作 Android 常见SD卡操作 参考 https://blog.csdn.net/mad1989/article/details/37568667. [0.] E ...
- Android 检测SD卡应用
Android 检测SD卡应用 // Environment.MEDIA_MOUNTED // sd卡在手机上正常使用状态 // ...
- android 获取sd卡根目录
dir:/storage/emulated/0 也就是 sdcard目录 ====== android 获取sd卡根目录 public String getSDPath(){ File ...
- Android获取SD卡路径及SDCard内存的方法
这篇文章主要介绍了Android获取SD卡路径及SDCard内存的方法,较为详细的分析了Android针对SD卡操作所涉及的类及其具体函数功能,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了A ...
随机推荐
- python 基础 字典生成式
dict1 = {1:2,3:4,6:7,9:10} print dict((v,k) for k,v in dict.items()) 结果 {2:1.4:3,10:9,7:6} res = [{' ...
- 浅析C#中的事件
讲过了委托,不得不讲事件. 事件基于委托,为委托提供了一种发布/订阅机制. 在发生其他类或对象关注的事情时,类或对象可通过事件通知它们.发送(或引发)事件的类称为“发行者”,接收(或处理)事件的类称为 ...
- UE4简单实现描边高亮效果
材质文件下载地址: 链接:https://pan.baidu.com/s/10HUmXR_YNMOTF-Cg4ybuUg 提取码:m1my 1. 将材质文件放到Content目录中 2. 在项目中添加 ...
- Windows form UI skinEngine的使用方法
1.安装SkinEngine(这里安装的是3.4.7) 链接: https://pan.baidu.com/s/1-kZ5KgYclshWc17jbuke5w 提取码: bp7n 复制这段内容后打开百 ...
- 交叉编译Spice-gtk
Fedora环境 编译环境 操作系统: 64位 Fedora23 下载源文件 spice-gtk.spice-protocol 安装依赖 $ sudo yum install -y dh-autore ...
- PAT1098【插入排序+堆排序】
简单插入排序: 简单插入排序的核心思想: 把一条这么个难看的序列默认分为两个排好序的和未排好序的两个部分: 所以一开始排好序的只有一个a[0](好看的只有一个),难看的有N(数组长度)-1个a[1,n ...
- Spring基本原理模拟(IoC部分)
package ioc; import java.io.File; import java.lang.reflect.Method; import java.util.Collections; imp ...
- java实例练习——基于TCP/IP协议的多客户端通信
先说一下大概的思路: 应用多线程来实现服务器与多客户端之间的通信 1.服务器端创建ServerSocket,循环调用accept()等待客户端连接: 2.客户端创建一个Socket并请求与服务器端连接 ...
- C#多进程并行
为了并行执行多个任务,可以启动多个进程(并行数). 下面提供两种方法,总任务数10,最大并行数4. 一.方法1 using System; using System.Collections.Gener ...
- 洛谷P1282 多米诺骨牌
P1282 多米诺骨牌 题目描述 多米诺骨牌有上下2个方块组成,每个方块中有1~6个点.现有排成行的 上方块中点数之和记为S1,下方块中点数之和记为S2,它们的差为|S1-S2|.例如在图8-1中,S ...