安卓文件IO部分,东抄西抄来的

package app.share.io;


import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.http.util.EncodingUtils;
import android.content.Context;
import android.os.Environment;
import app.share.App;


/**对文件IO的扩展和包装的类,记得在权限文件中设置读写SD卡权限*/
public class FileIO {
/**编码常量*/
public static final String CHARSET_UTF_8 = "UTF-8";
/**编码常量*/
public static final String CHARSET_UTF_16 = "UTF-16";
/**分隔符常量*/
public static final String STRING_SEPARATOR = "/";
/**
* 文件路径固定到 /data/data/app_name/files的文件读取,需要使用context参数
* @param fileName 文件名即可(不需要全路径,不要包含路径分割符)
* @param charSet 文件编码格式,可选用本类的CHARSET_开头的字符串常量
* @param context "内容",通常可由this.getBaseContext()获得
*/
public static String readFileData(String fileName,String charSet,Context context) 
{
String result = "";
FileInputStream in = null ;
try {
in = context.openFileInput(fileName);
int length = in.available() ; //最大4G ??内存估计不够
byte[] buffer = new byte[length];
in.read(buffer);
result = EncodingUtils.getString(buffer,charSet);
} catch (Exception e) {
App.Print("openFileInput()失败:"+e.getMessage());
}
finally
{
if(in != null)
try {
in.close() ;
} catch (Exception e) {
// TODO Auto-generated catch block
App.Print("in.close()失败");
}
}
return result;
}

/**
* 文件路径固定到 /data/data/app_name/files的文件读取,需要使用context参数
* @param fileName 文件名即可(不需要全路径,不要包含路径分割符)
* @param charSet 文件编码格式,可选用本类的CHARSET_开头的字符串常量
* @param context "内容",通常可由this.getBaseContext()获得
*/
public static DataInputStream readFileData(String fileName,Context context) 
{
DataInputStream input = null ;
FileInputStream in = null ;
try {
in = context.openFileInput(fileName);
input = new DataInputStream(in);
} catch (Exception e) {
App.Print("openFileInput()失败:"+e.getMessage());
}

return input;
}


/**
* 文件路径固定到 /data/data/app_name/files的文件读取,需要使用context参数
* @param fileName 文件名即可(不需要全路径,不要包含路径分割符)
* @param text 文件内容
* @param mode 详解如下,使用时加和
* @Mode详解 Context.MODE_APPEND 追加
* @Mode详解 Context.MODE_PRIVATE 私有(默认)
* @Mode详解 Context.MODE_WORLD_READABLE 扩展读
* @Mode详解 Context.MODE_WORLD_WRITEABLE 扩展写
* @Mode详解 Context.MODE_MULTI_PROCESS  未知
* @Mode详解 Context.MODE_ENABLE_WRITE_AHEAD_LOGGING 未知
* @param context "内容",通常可由this.getBaseContext()获得
*/
public static void writeFileData(String fileName, String text,int mode,Context context)
{
FileOutputStream out = null;
try {
out = context.openFileOutput(fileName,
mode);
byte[] bytes = text.getBytes();
out.write(bytes);


} catch (Exception e) {
App.Print("openFileOutput()失败:"+e.getMessage());
} finally {
if (out != null)
try {
out.close();
} catch (Exception e) {
App.Print("out.close()失败");
}
}


}

/**
* @param filePath 文件全路径
* @param charSet 文件编码格式,可选用本类的CHARSET_开头的字符串常量
* @return
*/
public static String readFileData(String filePath,String charSet) 
{
String result = null ;
FileInputStream in = null ;

File file = new File(filePath);
if(!file.exists())
{
return null ;
}

try 
{
in = new FileInputStream(filePath);
int length = in.available() ;
byte[] buffer = new byte[length];
in.read(buffer);
result = EncodingUtils.getString(buffer, charSet);
} catch (Exception e){
App.Print("openFileInput()失败:"+e.getMessage());
}
finally
{
if(in != null)
try {
in.close() ;
} catch (Exception e) {
// TODO Auto-generated catch block
App.Print("in.close()失败");
}
}
return result;
}

/**
* @param filePath 文件全路径
* @param text 文件内容
* @param append 指示是否增加
*/
public static void writeFileData(String filePath,String text,boolean append) 
{
File file = new File(filePath);
if(!file.exists())
try 
{
file.createNewFile() ;

catch (Exception e)
{
App.Print("创建新文件失败:"+e.getMessage());
return ;
}

FileOutputStream out = null;
try {
out = new FileOutputStream(filePath,append);
byte[] bytes = text.getBytes();
out.write(bytes);


} catch (Exception e) {
App.Print("FileOutputStream()失败:"+e.getMessage());
} finally {
if (out != null)
try 
{
out.close();
}
catch (Exception e) 
{
App.Print("out.close()失败:"+e.getMessage());
}
}
}

/**
* 根据输入流,字符编码返回字符串,返回前流将被关闭
* @param openStream 输入流
* @return 返回字符串
*/
public static String readFileData(InputStream openStream,String charSet) {


String result = "";
try {
int length = openStream.available() ;
byte[] buffer = new byte[length];
openStream.read(buffer);
result = EncodingUtils.getString(buffer, charSet);
} catch (Exception e) {
// TODO Auto-generated catch block
App.Print("openFileInput()失败");
}
finally
{
if(openStream != null)
try {
openStream.close() ;
} catch (Exception e) {
// TODO Auto-generated catch block
App.Print("in.close()失败");
}
}
return result;
}

/**
* 返回SD路径,后面有加分隔符,例如"/mnt/sdcard/"
* @param testWriteAble 指定是否检查可写性
* @return 没有SD卡或其不可读或不可写,返回null
*/
public static String getSDPath(boolean testWriteAble)
{
File sdDir = null ;
sdDir = Environment.getExternalStorageDirectory() ;
if(sdDir == null )
return null ;
boolean sdCardExist = sdDir.exists() && sdDir.canRead() ;
if(testWriteAble)
sdCardExist = sdCardExist && sdDir.canWrite() ;
if(sdCardExist)
{
sdDir = Environment.getExternalStorageDirectory();
return sdDir.toString() + STRING_SEPARATOR ;
}
else
return null ;
}

/**
* 返回程序Data路径,后面有加分隔符,例如"/data/data/app_name/files/"
* @param context 可用this.getBaseContext()获得
* @return 返回null代表获取失败
*/
public static String getDataPath(Context context)
{
File sdDir = null ;
try {
sdDir = context.getApplicationContext().getFilesDir();
} catch (Exception e) {
return null ;
}
if(sdDir == null )
return null ;
return sdDir.toString() +STRING_SEPARATOR ;
}

/**
* 返回程序cache路径,后面有加分隔符,例如"/data/data/app_name/cache/"
* @param context 可用this.getBaseContext()获得
* @return 返回null代表获取失败
*/
public static String getCachePath(Context context)
{
File cacheDir = null ;
try {
cacheDir = context.getApplicationContext().getCacheDir();
} catch (Exception e) {
return null ;
}
if(cacheDir == null )
return null ;
return cacheDir.toString() +STRING_SEPARATOR ;
}

/**
* 检测文件是否存在
* @param filePath 文件全路径
* @return true :文件存在,否则返回false
*/
public static boolean Exist(String filePath)
{
File file = new File(filePath);
return file.exists();
}

/**
* 检测文件是否可读
* @param filePath 文件全路径
* @return true :文件可读,否则返回false
*/
public static boolean canRead(String filePath)
{
File file = new File(filePath);
return file.canRead();
}

/**
* 检测文件是否可写
* @param filePath 文件全路径
* @return true :文件可写,否则返回false
*/
public static boolean canWrite(String filePath)
{
File file = new File(filePath);
return file.canWrite();
}

/**
* 从源字符串中获取指定名称的值
* @param source 文件全路径
* @param name :名
* @return 返回null代表找不到
*/
public static String getValue(String source,String name)
{
String[] strs =  source.split("\n");
String str = null ;
for(int i = 0 ; i < strs.length ; i ++)
{
if(strs[i] == null || strs[i].length() <= 0)
continue ;
if(strs[i].contains(name))
try {
str = strs[i].substring(strs[i].indexOf("=")+1,strs[i].length() - 1);
} catch (Exception e) {
App.Print("substring失败:"+e.getMessage());
}
}
return str ;
}
class ConfigData
{
public String name = null ;
public String value = null ;
}
/**
* 将指定的name/value集合写入source中,并指定是否进行覆盖检测
* @param source
* @param name
* @param value
* @param forceTest
* @return 源
*/
public static String setValue(String source, String name, String value,boolean forceTest)
{
if(forceTest)
{
String temp = getValue(source, name);
if(temp == null)
{
source +=(name + "="+ value +"\n") ;
return source ;
}
else
{
String[] strs = source.split("\n");
source = "";
for(int i = 0 ; i < strs.length ; i++)
{
if(strs[i].indexOf(name) >= 0)
{
strs[i] = strs[i].replaceFirst(temp, value);
}
source += (strs[i]+"\n" );
}
return source ;
}
}
else
{
source +=(name + "="+ value +"\n") ;
return source ;
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值