import android.content.Context;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class FileUtils {
private static String LOCAL_FILE_PATH;
private static FileUtils fileUtils;
private static Context context;
private Object lock;
// 私有构造
private FileUtils() {
}
// 单例
public static FileUtils getInstance() {
if (null == fileUtils) {
fileUtils = new FileUtils();
}
return fileUtils;
}
// 首先调用 init() 函数
public void init(Context mContext) {
context = mContext;
lock = new Object();
LOCAL_FILE_PATH = context.getFilesDir().getPath() + "/";
}
// 创建文件
public void create(String name) {
// 判断文件夹是否有了
File fileDirectory = new File(LOCAL_FILE_PATH);
if (!fileDirectory.exists()) {
fileDirectory.mkdirs();
}
// 判断是否有这个文件
File file = new File(context.getFilesDir().getPath() + "/" + name);
try {
if (!file.exists()) {
file.createNewFile();
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
/**
* 写入文件
* name : 文件名字
* str:写入的字符串
*/
public void write(String name, String str) {
synchronized (lock) {
// 判断文件夹是否有了
File fileDirectory = new File(LOCAL_FILE_PATH);
if (!fileDirectory.exists()) {
fileDirectory.mkdirs();
}
// 判断是否有这个文件
File file = new File(context.getFilesDir().getPath() + "/" + name);
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(file, true); // true 代表写完一条接着写,反之清空之前的数据再写入
if (null != fileOutputStream) {
fileOutputStream.write(str.getBytes());
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
} finally {
try {
if (null != fileOutputStream) {
fileOutputStream.flush();
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
}
/**
* 读取文件
* name : 文件名字
*/
public String read(String name) {
File fileDirectory = new File(LOCAL_FILE_PATH);
if (!fileDirectory.exists()) {
fileDirectory.mkdirs();
}
// 判断是否有这个文件
File file = new File(context.getFilesDir().getPath() + "/" + name);
FileInputStream inputStream = null;
BufferedReader bufferedReader = null;
StringBuilder stringBuilder = new StringBuilder();
if (file.exists()) {
try {
inputStream = context.openFileInput(name);
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while (null != (line = bufferedReader.readLine())) {
stringBuilder.append(line);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
} finally {
try {
if (null != bufferedReader) {
bufferedReader.close();
}
if (null != inputStream) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
return stringBuilder.toString();
}
/**
* 文件的压缩
* 1.compressFilePath:要压缩文件的路径
* 2.name:要压缩文件的名字
* 3.toLocalPath:把文件压缩到哪里
*/
public void compress(String compressFilePath, String name, String toLocalPath) throws IOException {
Boolean isCompress = false;
File file = new File(compressFilePath); // 判断要压缩的文件路径是否存在
File compress = new File(toLocalPath); // 判断你压缩后的文件路径是否存在
if (!compress.exists()) {
compress.mkdirs();
}
// 压缩前的文件可能是.txt 文件,我们根据点来分割提取点之前的部分
String str[] = name.split("\\.");
String compressName = str[0] + ".zip";
// 对文件压缩前进行判断,这个文件之前是否已经被压缩过
File[] files = compress.listFiles();
if (null != files) {
for (File f : files) {
if (f.getName().endsWith(compressName)) {
isCompress = true;
}
}
}
// 压缩处理
if (!isCompress) {
ZipOutputStream zipOutputStream = null;
FileInputStream fileInputStream = null;
try {
zipOutputStream = new ZipOutputStream(new FileOutputStream(toLocalPath + compressName));
if (null != zipOutputStream) {
if (file.isFile()) {
ZipEntry zipEntry = new ZipEntry(name);
fileInputStream = new FileInputStream(file);
zipOutputStream.putNextEntry(zipEntry);
int len;
byte[] bytes = new byte[5000];
while ((len = fileInputStream.read(bytes)) != -1) {
zipOutputStream.write(bytes, 0, len);
}
zipOutputStream.closeEntry();
}
} else {
return;
}
} catch (IOException e) {
e.printStackTrace();
System.out.println(e.getMessage());
} finally {
if (null != zipOutputStream) {
zipOutputStream.close();
}
if (null != fileInputStream) {
fileInputStream.close();
}
}
}
}
}
Android --- 文件的创建、写入、读取、压缩操作示例代码
最新推荐文章于 2024-06-10 23:10:52 发布