代码如下所示:
- package com.mzz.utils;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import org.apache.http.util.EncodingUtils;
- import android.os.Environment;
- public class FileUtil {
- private File file = null;
- private final String SDCARD_PATH = Environment.getExternalStorageDirectory() + "/";
- /**
- * 根据目录和文件名创建文件
- * @param dir 如果为空代表在SDCARD跟目录创建新文件
- * @param fileName 创建文件的文件名
- * @return -1表示文件已存在 0表示创建失败 1表示创建成功
- */
- public int createNewFile(String dir , String fileName) {
- if(dir.length() != 0) {
- File path = new File(SDCARD_PATH + dir);
- if(!path.exists()) {
- file = new File(SDCARD_PATH + dir);
- //创建目录
- if(file.mkdirs()) {
- //将file重新赋值为带文件名的
- file = new File(SDCARD_PATH + dir + "/" + fileName);
- }
- }
- else {
- file = new File(SDCARD_PATH + dir + "/" + fileName);
- }
- }
- else {
- file = new File(SDCARD_PATH + fileName);
- }
- if(file.exists()) {
- return -1;
- }
- else {
- try {
- if(file.createNewFile()) {
- return 1;
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- return 0;
- }
- //往指定目录、指定文件中写入String对象
- public void writeString2File(String dir , String fileName, String src) {
- FileOutputStream out = null;
- if(createNewFile(dir,fileName) != 0)
- {
- try {
- out = new FileOutputStream(file);
- out.write(src.getBytes());
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally {
- try {
- if(createNewFile(dir,fileName) != 0)
- out.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- //读指定目录、指定文件中的String数据
- public String readFile(String dir, String fileName) {
- FileInputStream input = null;
- byte[] buffer = new byte[1024 * 10];//10K
- if(createNewFile(dir, fileName) != 0) {
- try {
- input = new FileInputStream(file);
- input.read(buffer);
- return EncodingUtils.getString(buffer, "UTF-8");
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- return null;
- }
- }
package com.mzz.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.http.util.EncodingUtils;
import android.os.Environment;
public class FileUtil {
private File file = null;
private final String SDCARD_PATH = Environment.getExternalStorageDirectory() + "/";
/**
* 根据目录和文件名创建文件
* @param dir 如果为空代表在SDCARD跟目录创建新文件
* @param fileName 创建文件的文件名
* @return -1表示文件已存在 0表示创建失败 1表示创建成功
*/
public int createNewFile(String dir , String fileName) {
if(dir.length() != 0) {
File path = new File(SDCARD_PATH + dir);
if(!path.exists()) {
file = new File(SDCARD_PATH + dir);
//创建目录
if(file.mkdirs()) {
//将file重新赋值为带文件名的
file = new File(SDCARD_PATH + dir + "/" + fileName);
}
}
else {
file = new File(SDCARD_PATH + dir + "/" + fileName);
}
}
else {
file = new File(SDCARD_PATH + fileName);
}
if(file.exists()) {
return -1;
}
else {
try {
if(file.createNewFile()) {
return 1;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return 0;
}
//往指定目录、指定文件中写入String对象
public void writeString2File(String dir , String fileName, String src) {
FileOutputStream out = null;
if(createNewFile(dir,fileName) != 0)
{
try {
out = new FileOutputStream(file);
out.write(src.getBytes());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if(createNewFile(dir,fileName) != 0)
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//读指定目录、指定文件中的String数据
public String readFile(String dir, String fileName) {
FileInputStream input = null;
byte[] buffer = new byte[1024 * 10];//10K
if(createNewFile(dir, fileName) != 0) {
try {
input = new FileInputStream(file);
input.read(buffer);
return EncodingUtils.getString(buffer, "UTF-8");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
}