public class FileUtil {
/**
* 删除文件
*
* @param context
* 程序上下文
* @param fileName
* 文件名,要在系统内保持唯一
* @return boolean 存储成功的标志
*/
public static boolean deleteFile(Context context, String fileName) {
return context.deleteFile(fileName);
}
/**
* 文件是否存在
*
* @param context
* @param fileName
* @return
*/
public static boolean exists(Context context, String fileName) {
return new File(context.getFilesDir(), fileName).exists();
}
/**
* 存储文本数据
*
* @param context
* 程序上下文
* @param fileName
* 文件名,要在系统内保持唯一
* @param content
* 文本内容
* @return boolean 存储成功的标志
*/
public static boolean writeFile(Context context, String fileName,
String content) {
boolean success = false;
FileOutputStream fos = null;
try {
fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
byte[] byteContent = content.getBytes();
fos.write(byteContent);
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null)
fos.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
return success;
}
/**
* 存储文本数据
*
* @param context
* 程序上下文
* @param fileName
* 文件名,要在系统内保持唯一
* @param content
* 文本内容
* @return boolean 存储成功的标志
*/
public static boolean writeFile(String filePath, String content) {
boolean success = false;
FileOutputStream fos = null;
try {
fos = new FileOutputStream(filePath);
byte[] byteContent = content.getBytes();
fos.write(byteContent);
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null)
fos.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
return success;
}
/**
* 文件
*
* @param srcFile
* @param dstFile
*/
public static void copy(File srcFile, File dstFile) {
try {
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(dstFile);
byte[] buffer = new byte[1024];
int length = 0;
while ((length = fis.read(buffer)) != -1) {
fos.write(buffer, 0, length);
}
fis.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 读取文本数据
*
* @param context
* 程序上下文
* @param fileName
* 文件名
* @return String, 读取到的文本内容,失败返回null
*/
public static String readFile(Context context, String fileName) {
if (!exists(context, fileName)) {
return null;
}
FileInputStream fis = null;
String content = null;
try {
fis = context.openFileInput(fileName);
if (fis != null) {
byte[] buffer = new byte[1024];
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
while (true) {
int readLength = fis.read(buffer);
if (readLength == -1)
break;
arrayOutputStream.write(buffer, 0, readLength);
}
fis.close();
arrayOutputStream.close();
content = new String(arrayOutputStream.toByteArray());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
content = null;
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
return content;
}
/**
* 读取文本数据
*
* @param context
* 程序上下文
* @param fileName
* 文件名
* @return String, 读取到的文本内容,失败返回null
*/
public static String readFile(String filePath) {
if (filePath == null || !new File(filePath).exists()) {
return null;
}
FileInputStream fis = null;
String content = null;
try {
fis = new FileInputStream(filePath);
if (fis != null) {
byte[] buffer = new byte[1024];
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
while (true) {
int readLength = fis.read(buffer);
if (readLength == -1)
break;
arrayOutputStream.write(buffer, 0, readLength);
}
fis.close();
arrayOutputStream.close();
content = new String(arrayOutputStream.toByteArray());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
content = null;
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
return content;
}
/**
* 读取文本数据
*
* @param context
* 程序上下文
* @param fileName
* 文件名
* @return String, 读取到的文本内容,失败返回null
*/
public static String readAssets(Context context, String fileName) {
InputStream is = null;
String content = null;
try {
is = context.getAssets().open(fileName);
if (is != null) {
byte[] buffer = new byte[1024];
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
while (true) {
int readLength = is.read(buffer);
if (readLength == -1)
break;
arrayOutputStream.write(buffer, 0, readLength);
}
is.close();
arrayOutputStream.close();
content = new String(arrayOutputStream.toByteArray());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
content = null;
} finally {
try {
if (is != null)
is.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
return content;
}
/**
* 存储List对象
*
* @param context
* 程序上下文
* @param fileName
* 文件名,要在系统内保持唯一
* @param list
* 对象数组集合,对象必须实现Parcelable
* @return boolean 存储成功的标志
*/
public static boolean writeParcelableList(Context context, String fileName,
List<Parcelable> list) {
boolean success = false;
FileOutputStream fos = null;
try {
if (list instanceof List) {
fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
Parcel parcel = Parcel.obtain();
parcel.writeList(list);
byte[] data = parcel.marshall();
fos.write(data);
success = true;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
return success;
}
/**
* 读取数据对象
*
* @param context
* 程序上下文
* @param fileName
* 文件名
* @return List, 读取到的对象数组,失败返回null
*/
@SuppressWarnings("unchecked")
public static List<Parcelable> readParcelableList(Context context,
String fileName, ClassLoader classLoader) {
List<Parcelable> results = null;
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
try {
fis = context.openFileInput(fileName);
if (fis != null) {
bos = new ByteArrayOutputStream();
byte[] b = new byte[4096];
int bytesRead;
while ((bytesRead = fis.read(b)) != -1) {
bos.write(b, 0, bytesRead);
}
byte[] data = bos.toByteArray();
Parcel parcel = Parcel.obtain();
parcel.unmarshall(data, 0, data.length);
parcel.setDataPosition(0);
results = parcel.readArrayList(classLoader);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
results = null;
} finally {
if (fis != null)
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
if (bos != null)
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return results;
}
public static boolean saveSerializable(Context context, String fileName,
Serializable data) {
boolean success = false;
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(context.openFileOutput(fileName,
Context.MODE_PRIVATE));
oos.writeObject(data);
success = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return success;
}
public static Serializable readSerialLizable(Context context,
String fileName) {
Serializable data = null;
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(context.openFileInput(fileName));
data = (Serializable) ois.readObject();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return data;
}
/**
* 从assets里边读取字符串
*
* @param context
* @param fileName
* @return
*/
public static String getFromAssets(Context context, String fileName) {
try {
InputStreamReader inputReader = new InputStreamReader(context
.getResources().getAssets().open(fileName));
BufferedReader bufReader = new BufferedReader(inputReader);
String line = "";
String Result = "";
while ((line = bufReader.readLine()) != null)
Result += line;
return Result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 复制文件
*
* @param srcFile
* @param dstFile
* @return
*/
public static boolean copy(String srcFile, String dstFile) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
File dst = new File(dstFile);
if (!dst.getParentFile().exists()) {
dst.getParentFile().mkdirs();
}
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(dstFile);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
}
分享到:
相关推荐
Java文件工具类
Android上传文件工具类
ZIP格式压缩文件工具类
分片上传文件工具类,对应文章为:https://blog.csdn.net/y534560449/article/details
创建文件工具类 包含创建文件夹 文件 临时文件等
java上传文件工具类 很全面
文件工具类java操作文件工具类java操作文件工具类java操作文件工具类java操作文件工具类
读取properties文件工具类。使用时直接将工具类放到Java项目中,调用读取properties文件的方法即可。
每个程序员都应该有一套自己的工具类,这个工具类提供很全的java文件操作方法
C# 文件工具类 实际项目使用 久经考验 文件读写 复制 删除 创建文件夹 判断文件扩展名等等
使用 net.lingala.zip4j包实现相关操作,相对于JDK自带的zip操作类,其不仅支持密码操作,更加简单方便,且不依赖其他类库。
大家可以不用在为操作文件发愁了! 小弟觉得这个工具类还不是很完善,高手在帮忙完善一下,别忘了共享出来哦!
文件操作工具类,包含生成保存,复制,删除,读取,获取文件名,获取文件列表等等,只有你想不到,没有你找不到的Android端工具类,复制到项目中可直接使用
很强大的最底层的socket文件上传功能
这是一个C#操作XML文件的工具类,基本上你要用的都给你写好了,直接使用
实现文件的创建、删除、复制、压缩、解压以及目录的创建、删除、复制、压缩解压等功能
项目中添加该工具类,用一下语句调用 FTPUtil ftpUtil = new FTPUtil("172.16.1.41",21,"admin","123456"); ftpUtil.downLoadFile("20190913.txt","D:\\ftplocal\\20190913.txt"); ftpUtil.uploadFile&#...
下载文件工具类,支持post下载,上传文件等,。。。。。。