参考了网上的一些资源代码,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卡封装的类的更多相关文章

  1. Android 读写SD卡的文件

    今天介绍一下Android 读写SD卡的文件,要读写SD卡上的文件,首先需要判断是否存在SD卡,方法: Environment.getExternalStorageState().equals(Env ...

  2. android 读写sd卡的权限设置

    原文:android 读写sd卡的权限设置 在Android中,要模拟SD卡,要首先使用adb的mksdcard命令来建立SD卡的镜像,如何建立,大家上网查一下吧,应该很容易找到,这里不说这个问题. ...

  3. Android读写SD卡

    SD卡的读写是我们在开发Android 应用程序过程中最常见的操作.下面介绍SD卡的读写操作方式: 1. 获取SD卡的根目录 String sdCardRoot = Environment.getEx ...

  4. android 读写SD卡文件

    参考: http://www.oschina.net/code/snippet_176897_7336#11699 写文件: private void SavedToText(Context cont ...

  5. android学习笔记47——读写SD卡上的文件

    读写SD卡上的文件 通过Context的openFileInput.openFileOutput来打开文件输入流.输出流时,程序打开的都是应用程序的数据文件夹里的文件,其存储的文件大小可能都比较有限- ...

  6. Android 常见SD卡操作

    目录 Android 常见SD卡操作 Android 常见SD卡操作 参考 https://blog.csdn.net/mad1989/article/details/37568667. [0.] E ...

  7. Android 检测SD卡应用

    Android 检测SD卡应用 //                                    Environment.MEDIA_MOUNTED // sd卡在手机上正常使用状态  // ...

  8. android 获取sd卡根目录

    dir:/storage/emulated/0 也就是 sdcard目录 ====== android 获取sd卡根目录 public String getSDPath(){        File ...

  9. Android获取SD卡路径及SDCard内存的方法

    这篇文章主要介绍了Android获取SD卡路径及SDCard内存的方法,较为详细的分析了Android针对SD卡操作所涉及的类及其具体函数功能,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了A ...

随机推荐

  1. event.keyCode 事件属性

    转自:http://www.runoob.com/jsref/event-key-keycode.html <!DOCTYPE html> <html> <head> ...

  2. 调试opencv调用摄像头程序时碰到的问题

    昨天晚上想把opencv学习笔记整理一下,当跑opencv调用摄像头的程序的时候老是出现Assertion failed (size.width>0 && size.height ...

  3. elasticsearch2.x插件之一:kibana

    介绍: 要说kibana,就不得不先说一下logstash.这里呢,先要讲个故事.故事是开头是这样的,Logstash早期曾经自带了一个特别简单的logstash-web用来查看ES中的数据,其功能太 ...

  4. assert.ifError()

    assert.ifError(value) 如果 value 为真,则抛出 value. 可用于测试回调函数的 error 参数(通俗解释ifError方法断定某个表达式是否false,如果该表达式对 ...

  5. 【转】如何配置EditPlus中Java运行环境,运行Java程序

    如何配置EditPlus中Java运行环境,运行Java程序 http://jingyan.baidu.com/article/86112f13725e2e2736978711.html 分步阅读 E ...

  6. Sharepoint2013商务智能学习笔记之Performancepoint service 配置(九)

    1)配置Performance Service服务 第一步,新建performance service.先在管理中心,系统设置区域点击管理服务器上的服务,确认Performance Service服务 ...

  7. C# 写 LeetCode easy #21 Merge Two Sorted Lists

    21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...

  8. 开源文字识别软件tesseract

    1.下载4.0软件,下一步下一步到成功: 2.安装之后配置环境变量,Path中添加安装路径(默认:C:\Program Files (x86)\Tesseract-OCR) 3.新增语言库的环境变量, ...

  9. ProtoBuf练习

    环境设置 项目地址 https://github.com/silvermagic/ProtoBufDev.git 操作系统 64位 Fedora 24 安装protobuf $ git clone h ...

  10. 洛谷P1480 A/B Problem(高精除高精)

    P1480 A/B Problem 题目描述 输入两个整数a,b,输出它们的商(a<=10^5000,b<=10^9) 输入输出格式 输入格式: 两行,第一行是被除数,第二行是除数. 输出 ...