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();
}
}
}
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();
}
}
}