参考了网上的一些资源代码,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. linux命令-tar工具详解

    把文件和目录打成一个包 文件打包 [root@wangshaojun ~]# tar -cvf 1.tar 1.txt 123 234 ///-c创建 -v可视化 -f file放最后面1.txt12 ...

  2. elasticsearch2.x插件之一:bigdesk

    bigdesk是elasticsearch的一个集群监控工具,可以通过它来查看es集群的各种状态,如:cpu.内存使用情况,索引数据.搜索情况,http连接数等. 可用项目git地址:https:// ...

  3. 【总结整理】JQuery基础学习---事件篇

    jQuery鼠标事件之click与dbclick事件 用交互操作中,最简单直接的操作就是点击操作.jQuery提供了两个方法一个是click方法用于监听用户单击操作,另一个方法是dbclick方法用于 ...

  4. linux docket

    什么是 Docker Docker 最初是 dotCloud 公司创始人 Solomon Hykes 在法国期间发起的一个公司内部项目,它是基于 dotCloud 公司多年云服务技术的一次革新,并于  ...

  5. SQL标量值函数:返回汉字拼音首拼

    今天遇到一个需求,客户要求在系统客户端选择客户的时候,可以用拼音首拼去快速过滤选择,此时我们在客户表里面加多一个拼音首拼字段CustPY来记录,字段加好了,我们要把所有客户名称的拼音简拼都更新到Cus ...

  6. jquery事件之事件

    事件名 说明 语法 (events 事件类型,data数据,handler 事件处理函数,selector 选择器) blur() 获得失去鼠标光标焦点事件 jQueryObject.blur( [ ...

  7. CEPH安装教程(下)

    创建 CEPH 文件系统 创建存储池 # ceph osd pool create cephfs_data 64 # ceph osd pool create cephfs_metadata 64 创 ...

  8. 【转载】Hyperledger学习小结

    Hyperledger学习小结 自学Hyperledger Composer也有段时间了,是时候对所学的知识总结一下了.因为没有实际项目参与的话,差不多也就到此为止了.后续可能会去了解一下以太坊的技术 ...

  9. ssh-ssh整合(Struts2+Sprinig+hibernate)

    在之前呢我们已经讲解过ssm以及ssm2的整合开发,今天我们进行ssh的整合,在之前已经有一篇整合ssh的文章,那是基于注解开发的,今天讲解的为基于配置文件注入方式进行开发.思路:spring管理hi ...

  10. JMeter - 实时结果 - InfluxDB和Grafana - 第1部分 - 基本设置

    概述: 在本文中,我将解释如何使用JMeter + InfluxDB + Grafana获得实时性能测试结果. 请注意,此主题太大,无法涵盖一篇文章中的所有内容.所以,我试图提供与TestAutoma ...