常用的IO输入输出流

package com.org.sharedprefer.util;


import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;


public class FileUtil {


private static final String TAG = "main";
private static final String FILE_PATH = "wuxianedu";

/**
* 获取SD卡绝对路径
*/
public static String getEnvironmentPath(){
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File file = Environment.getExternalStorageDirectory();
//绝对路径
return file.getAbsolutePath();
}
return "";
}

/**
* 写入SD卡数据
* @param fileName
* @param str
*/
public static void writeFile(String fileName, String str){
String strPath = getEnvironmentPath();
//判断strPath是否为空
if(!TextUtils.isEmpty(strPath)){
//创建根目录
File fileStr = new File(strPath + File.separator + FILE_PATH);
Log.e(TAG, "fileStr=="+fileStr);
//根目录是否存在
if(!fileStr.exists()){
fileStr.mkdirs();
}
//文件的绝对路径
strPath = fileStr + File.separator + fileName;
Log.e(TAG, "writeFile strPath=="+strPath);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(strPath);
fos.write(str.getBytes());
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
close(fos);
}
}

}
/**
* 读取SD卡数据
* @param fileName
*/
public static void readFile(String fileName){
String strPath = getEnvironmentPath();
//判断strPath是否为空
if(!TextUtils.isEmpty(strPath)){
//文件的绝对路径
strPath = strPath + File.separator + FILE_PATH + File.separator + fileName;
Log.e(TAG, "readFile strPath=="+strPath);
FileInputStream fis = null;
try {
fis = new FileInputStream(strPath);
byte[] bytes = new byte[1024];
int count = 0;
while((count = fis.read(bytes)) != -1){
String str= new String(bytes, 0, count);
Log.e(TAG, "str=="+str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
close(fis);
}
}

}

/**
* 写入字符串
* @param context
* @param fileName
*/
public static void writeString(Context context, String fileName, String str){
FileOutputStream fos = null;
try {
fos = context.openFileOutput(fileName, context.MODE_PRIVATE);
fos.write(str.getBytes());
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
close(fos);
}
}

/**
* 读取字符串
* @param context
* @param fileName
*/
public static void readString(Context context,String fileName){
FileInputStream fis = null;
try {
fis = context.openFileInput(fileName);
byte[] bytes = new byte[1024];
int count = 0;
while((count = fis.read(bytes)) != -1){
String str= new String(bytes, 0, count);
Log.e(TAG, "str=="+str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
close(fis);
}
}

/**
* 关闭流
* @param closeable
*/
public static void close(Closeable closeable){
try {
if(null != closeable){
closeable.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值