参考了网上的一些资源代码,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. 5.JasperReports学习笔记5-其它数据生成动态的报表(WEB)

    转自:http://www.blogjava.net/vjame/archive/2013/10/12/404908.html 一.空数据(Empty Datasources) 就是说JRXML文件里 ...

  2. win7 eclipse连接hadoop2.x开发环境搭建

    环境: hadoop-2.3.0-cdh5.1.0 centos 6.5 x64 win7 eclipse4.3 0. 前提条件 ,jdk,maven要安装好. 1.下载hadoop,用winRAR解 ...

  3. sklearn有关参数

    from sklearn import datasets from sklearn.linear_model import LinearRegression import matplotlib.pyp ...

  4. 在VM12中安装ubuntu系统下的VMTOOLS

    转载自http://www.jb51.net/article/97387.htm 一.下载Ubuntu镜像: Ubuntu官网下载地址 二.创建虚拟机 打开VMware Workstation,点击创 ...

  5. Linux命令使用

    命令行创建设置用户密码 $ sudo useradd -m -r username $ cat "username:password" | sudo chpasswd -m 查询u ...

  6. poj2763(lca / RMQ + 线段树)

    题目链接: http://poj.org/problem?id=2763 题意: 第一行输入 n, q, s 分别为树的顶点个数, 询问/修改个数, 初始位置. 接下来 n - 1 行形如 x, y, ...

  7. 2017-10-2 清北刷题冲刺班a.m

    一道图论神题 (god) Time Limit:1000ms   Memory Limit:128MB 题目描述 LYK有一张无向图G={V,E},这张无向图有n个点m条边组成.并且这是一张带权图,只 ...

  8. [Xcode 实际操作]四、常用控件-(11)UIDatePicker日期时间选择器

    目录:[Swift]Xcode实际操作 本文将演示日期拾取器的使用. 使用日期拾取器,可以快速设置和选择日期与时间. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] ...

  9. git 把文件从 版本管理中移除 andorid版本

    刚学git时,一股脑吧所有文件全部加到版本管理中,现在做Android开发,这样做就有很大的问题了,gen  和bin  文件夹下的文件是编译生成的,最好不要加到版本管理中,最好加入到.gitigno ...

  10. Docker容器构建过程的安全性分析

    来源:嘶吼专业版 ID:Pro4hou DevOps概念的流行跟近些年微服务架构的兴起有很大关系,DevOps是Dev(Development)和Ops(Operations)的结合,Dev负责开发, ...