FileManager

package com.kale.utils;

import java.io.BufferedReader;
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.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream; import android.content.Context;
import android.os.Environment; /**
* @author:Jack Tony
* @tips :
* <!-- 读写SD卡的权限 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
* @date :2014-6-30
*/
public class FileManager {
private String SD_PATH;
private String fileName;
private String dirName; public String getSD_PATH() {
return SD_PATH;
} /**
* @param permission 是否添加了权限
*/
public FileManager(boolean permission) {
//得到当前外部存储设备的目录
SD_PATH = Environment.getExternalStorageDirectory() + "/";
} /**
* 在SD卡上创建文件
*
* @throws IOException
*/
public File creatSDFile(String dirName ,String fileName) throws IOException {
this.fileName = fileName;
this.dirName = dirName;
File file = new File(creatSDDir(dirName), fileName);
return file;
} public String getFileWholePath() throws IOException{
if (dirName == null || fileName == null) {
return SD_PATH + dirName + "/" + fileName;
}
else {
throw new IOException("can't find dirName or fileName");
} } /**
* 在SD卡上创建目录
*
* @param dirName
*/
public File creatSDDir(String dirName) {
File dir = new File(SD_PATH + dirName);
dir.mkdir();
return dir;
} /**
* 判断SD卡上的文件夹是否存在
* kale,file.txt->kale/file.txt
*/
public boolean isFileExist(String dirName ,String fileName){
File file = new File(SD_PATH +dirName+"/"+ fileName);
return file.exists();
} /**
* 将一个InputStream里面的数据写入到SD卡中
* example:kale,file.txt,inputStream->kale/file.txt
* InputStream is = new ByteArrayInputStream( "kale data".getBytes() );
manager.saveToSdCard("dir02", "kale.txt", is );
*/
public File saveToSDcard(String dirName,String fileName,InputStream input){
File file = null;
OutputStream output = null;
try{
//创建目录
creatSDDir(dirName);
//建立文件
file = creatSDFile(dirName ,fileName);
output = new FileOutputStream(file);
byte buffer [] = new byte[5 * 1024];
//int count = 0;
while((input.read(buffer)) != -1){
//String str = new String(buffer,0,count);
//str = new String(str.getBytes("iso-8859-1"),"utf-8");
/*System.out.println("---------File Manager----start--------");
System.out.println(str);
System.out.println("---------File Manager-----end-------");*/
output.write(buffer);
}
output.flush();
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
output.close();
}
catch(Exception e){
e.printStackTrace();
}
}
return file;
} /**
* @param dirName
* @param fileName
* @return 从sd卡中读取文件的字符,返回String
* @throws Exception
*/
public String readFromSDcard(String dirName,String fileName) throws FileNotFoundException {
try {
File file = new File(SD_PATH +dirName+"/"+ fileName);
FileInputStream fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
StringBuilder sb = new StringBuilder("");
String line = null;
//循环读取文件内容
while((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
return sb.toString().trim();
}
catch(FileNotFoundException e) {
e.printStackTrace();
throw new FileNotFoundException("no such file");
}
catch (Exception e) {
e.printStackTrace();
}
return null;
} /////////////////////// 直接读取包名文件夹下的文件,而不是SD卡中的 /////////////////////
/**
* @param str
* 将string写入到data/data/包名/files中
* example:write(file.txt ,data)
*/
public static void write(Context mContext,String fileName,String str) {
try {
/**
* MODE_PRIVATE:该文件只能被当前程序读写 MODE_APPEND:用追加方式打开文件,新写入的内容追加到末尾
* MODE_WORLD_READABLE:该文件内容可以被其他程序读
* MODE_WORLD_WRITEABLE:该文件内容可以被其他程序读写
*/
FileOutputStream fos = mContext.openFileOutput(fileName, Context.MODE_PRIVATE | Context.MODE_APPEND);
PrintStream ps = new PrintStream(fos);
// 输出文件的内容
ps.println(str);
ps.close(); } catch (Exception e) {
e.printStackTrace();
}
} /**
* 通过文件名来读取data/data/包名/files下的文件
* example:read("file.txt")
* @return
* 返回null,表示找不到文件,或出现异常
*/
public static String read(Context mContext,String fileName) {
try {
// 打开文件输入流
FileInputStream fis = mContext.openFileInput(fileName);
byte[] buff = new byte[1024];
int hasRead = 0;
StringBuilder sb = new StringBuilder("");
// 读取文件内容
while ((hasRead = fis.read(buff)) > 0) {
sb.append(new String(buff, 0, hasRead));
}
// 关闭文件输入流
fis.close();
return sb.toString();
}
catch(FileNotFoundException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
return null;
} }

用IO流向存储器或SD卡中存入/读取字符的工具类的更多相关文章

  1. 【记录】尝试用android-logging-log4j去实现log输出内容到sd卡中的文件的功能

    [背景] 折腾: [记录]给Android中添加log日志输出到文件 期间,已经试了: [记录]尝试用android中microlog4android实现log输出到文件的功能 但是不好用. 然后就是 ...

  2. Android中使用SQLiteOpenHelper管理SD卡中的数据库

    使用Android中自带的SQLiteOpenHelper可以完成数据库的创建与管理,但有两点局限: (1)数据库创建在内存卡中,大小受限,创建位置位于/data/data/应用程序名/databas ...

  3. 与众不同 windows phone (37) - 8.0 文件系统: StorageFolder, StorageFile, 通过 Uri 引用文件, 获取 SD 卡中的文件

    [源码下载] 与众不同 windows phone (37) - 8.0 文件系统: StorageFolder, StorageFile, 通过 Uri 引用文件, 获取 SD 卡中的文件 作者:w ...

  4. 【Android 界面效果30】Android中ImageSwitcher结合Gallery展示SD卡中的资源图片

    本文主要是写关于ImageSwitcher结合Gallery组件如何展示SDCard中的资源图片,相信大家都看过API Demo 中也有关于这个例子的,但API Demo 中的例子是展示工程中Draw ...

  5. Android获取SD卡中选中图片的路径(URL)

    最近在做一个图片上传的功能,需要提供上传图片在SD卡中的路径,在网上看了些例子,改改调试成功,代码很简单.其布局文件如下: [html]  view plain copy   <?xml ver ...

  6. BitmapUtil【缩放bitmap以及将bitmap保存成图片到SD卡中】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 用于缩放bitmap以及将bitmap保存成图片到SD卡中 效果图 代码分析 bitmapZoomByHeight(Bitmap s ...

  7. Android_(控件)使用Gallery浏览手机上SD卡中图片

    运行截图: (发现后面两张照片是自己自拍,大写的尴尬对图片进行涂鸦了!!!) 程序结构: <?xml version="1.0" encoding="utf-8&q ...

  8. Android--手持PDA读取SD卡中文件

    近两年市场上很多Wince设备都开始转向Android操作系统,最近被迫使用Android开发PDA手持设备.主要功能是扫描登录,拣货,包装,发货几个功能.其中涉及到商品档的时候大概有700左右商品要 ...

  9. android 读取sd卡中的图片

    一.获取读取SD卡的权限 <!--在SDCard中创建与删除文件权限  -->    <uses-permission android:name="android.perm ...

随机推荐

  1. 【LOJ】#2492. 「BJOI2018」二进制

    题解 每次开这样的数据结构题感想都大概是如下两点 1.为什么别人代码长度都是我的1/2???? 2.为什么我运行时间都是他们的两倍???? 简单分析一下,我们关注一个区间是否合法只关注这个区间有多少个 ...

  2. 【GOF23设计模式】--工厂模式

    工厂模式: 实现了创建者调用者的分离 详细分类: 简单工厂模式 工厂方法模式 抽象工厂模式 面向对象设计的基本原则: OCP(开闭原则,Open-Closed Principle):一个软件的实体应当 ...

  3. 【Java】 大话数据结构(14) 排序算法(1) (冒泡排序及其优化)

    本文根据<大话数据结构>一书,实现了Java版的冒泡排序. 更多:数据结构与算法合集 基本概念 基本思想:将相邻的元素两两比较,根据大小关系交换位置,直到完成排序. 对n个数组成的无序数列 ...

  4. 过滤器中处理multipart/form-data头部的post请求request.getParameter(")获取不到参数问题

    如果不是文件类型请求,我们使用request.getParameter("");方法是可以获取到参数内容的,如果是文件类型的请求即请求的头部信息为“multipart/form-d ...

  5. SOA和Web Servcie的区别

    soa(Service-Oriented Architecture)是服务对服务的,web service是服务对客户端的. 都提供服务.  服务接口都是基于开发的.  服务接口和服务的具体实现都是分 ...

  6. 纠结好久的VM虚拟机MAC地址绑定问题

    VM虚拟机(centos)采用桥接的方式访问网络,搭建一个Online Judger 的 web服务端.本想让虚拟机的ip能够固定下来,因此在路由上采用MAC和IP绑定的方式解决. 结果:每次重启虚拟 ...

  7. 【转载】C语言 构建参数个数不固定函数

    深入浅出可变参数函数的使用技巧本文主要介绍可变参数的函数使用,然后分析它的原理,程序员自己如何对它们实现和封装,最后是可能会出现的问题和避免措施. VA函数(variable argument fun ...

  8. jquery开发表格插件项目之知识点累积

    js设置节点的属性与属性值用setAttribute(),获取节点的属性和属性值用getAttribute(). jquery 中使用attr 就可以获取及设置元素属性. 在设置checkbox状态时 ...

  9. 使用 IntraWeb (2) - Hello IntraWeb

    IntraWeb 比我相像中的更贴近 VCL, 传统的非可视组件在这里大都可用(其内部很多复合属性是 TStringList 类型的), 它的诸多可视控件也是从 TControl 继承下来的. 这或许 ...

  10. [原创]WebScarab工具介绍

    [原创]WebScarab工具介绍 一 WebScarab介绍 WebScarab是一个用来分析使用HTTP和HTTPS协议的应用程序框架.其原理很简单,WebScarab可以记录它检测到的会话内容( ...