JAVA基础文件读写类
原创
52cxy
04-03 21:18
阅读数:504
支持基本的文件读写、删除等操作,功能比较全面:
import java.util.*;
import java.io.*;
public class FileManage {
public static int PARAMETER_ERROR = -1;
public static int FILE_NOT_EXIST = -2;
public static int READ_ONLY = 1;
public static int READ_WRITE = 2;
/**
* 判断文件是否可写
*/
public static int getReadWrite(String filepath) {
if (filepath == null || filepath.trim().length() == 0) return PARAMETER_ERROR;
File fp = new File(filepath);
if (!fp.exists()) return FILE_NOT_EXIST;
if (fp.canWrite())
return READ_WRITE;
else
return READ_ONLY;
}
/**
* 判断文件是否存在
*/
public static boolean checkFileExist(String filepath) {
if (filepath == null || filepath.trim().length() == 0) return false;
File fp = new File(filepath);
if (!fp.exists()) return false;
if (!fp.isFile()) return false;
return true;
}
/**
* 判断目录是否存在
*/
public static boolean checkFolderExist(String filepath) {
if (filepath == null || filepath.trim().length() == 0) return false;
File fp = new File(filepath);
if (!fp.exists()) return false;
if (!fp.isDirectory()) return false;
return true;
}
/**
* 创建目录
*/
public static boolean createFolder(String folderpath) {
if (folderpath == null || folderpath.trim().length() == 0) return false;
File fp = new File(folderpath);
if (fp.exists()) return false;
return fp.mkdirs();
}
/**
* 删除文件
*/
public static boolean delete(String filepath) {
if (filepath == null || filepath.trim().length() == 0) return false;
File fp = new File(filepath);
if (!fp.exists()) return false;
return fp.delete();
}
/**
* 删除目录
*/
public static boolean deleteFolder(String filepath) {
if (filepath == null || filepath.trim().length() == 0) return false;
File fp = new File(filepath);
return deleteFolder(fp);
}
public static boolean deleteFolder(File file) {
if (file == null) return false;
File[] subfile = file.listFiles();
for (int i = 0; i < subfile.length; i++) {
if (subfile[i].isDirectory()) {
if (!deleteFolder(subfile[i])) return false;
} else if (subfile[i].isFile()) {
if (!subfile[i].delete()) return false;
}
}
return file.delete();
}
/**
* 读取文件内容
*/
public static String readTextFile(String filepath) {
if (filepath == null || filepath.trim().length() == 0) return null;
try {
String contents = null;
RandomAccessFile fp = new RandomAccessFile(filepath, "r");
String strtmp = fp.readLine();
while (strtmp != null) {
if (contents == null)
contents = strtmp;
else
contents = contents + "\n" + strtmp;
strtmp = fp.readLine();
}
fp.close();
return contents;
} catch (Exception e) {
System.out.println("Read File error:" + e.toString());
return null;
}
}
/**
* 读取文件内容
*/
public static byte[] readFile(String filepath) {
if (filepath == null || filepath.trim().length() == 0) return null;
try {
RandomAccessFile fp = new RandomAccessFile(filepath, "r");
byte[] contents = new byte[(int) fp.length()];
fp.read(contents);
fp.close();
return contents;
} catch (Exception e) {
System.out.println("Read File error:" + e.toString());
return null;
}
}
/**
* 读取文件内容
*/
public static byte[] readFile(File filepath) {
if (filepath == null) return null;
try {
RandomAccessFile fp = new RandomAccessFile(filepath, "r");
byte[] contents = new byte[(int) fp.length()];
fp.read(contents);
fp.close();
return contents;
} catch (Exception e) {
System.out.println("Read File error:" + e.toString());
return null;
}
}
/**
* 保存文件
*/
public static boolean saveFile(String str, String filepath) {
if (filepath == null || filepath.trim().length() == 0) return false;
String strTmp = str;
if (strTmp == null) strTmp = "";
try {
File file = new File(filepath);
RandomAccessFile rdfile = new RandomAccessFile(file, "rw");
rdfile.write(strTmp.getBytes());
rdfile.close();
} catch (Exception e) {
System.out.println("save File error:" + e.toString());
return false;
}
return true;
}
/**
* 保存文件
*/
public static boolean saveFile(byte[] str, String filepath) {
if (filepath == null || filepath.trim().length() == 0) return false;
byte[] strTmp = str;
if (strTmp == null) strTmp = new byte[0];
try {
File file = new File(filepath);
RandomAccessFile rdfile = new RandomAccessFile(file, "rw");
rdfile.write(strTmp);
rdfile.close();
} catch (Exception e) {
System.out.println("save File error:" + e.toString());
return false;
}
return true;
}
/**
* 复制文件
*/
public static boolean copyFile(String spath, String tpath) {
if (spath == null || spath.trim().length() == 0 || tpath == null || tpath.trim().length() == 0)
return false;
try {
File sfile = new File(spath);
String filename = sfile.getName();
File tfile = new File(tpath, filename);
RandomAccessFile rdtfile = new RandomAccessFile(tfile, "rw");
RandomAccessFile rdsfile = new RandomAccessFile(sfile, "r");
byte[] contents = new byte[(int) rdsfile.length()];
rdsfile.read(contents);
rdsfile.close();
rdtfile.write(contents);
rdtfile.close();
return true;
} catch (Exception e) {
System.out.println("Copy file error:" + e.toString());
return false;
}
}
/**
* 移动文件
*/
public static boolean moveFile(String spath, String tpath) {
if (spath == null || spath.trim().length() == 0 || tpath == null || tpath.trim().length() == 0)
return false;
if (copyFile(spath, tpath))
return delete(spath);
else
return false;
}
/**
* 向文件写入文本
*/
public static boolean writeLineFile(RandomAccessFile file, String lineTxt, boolean isRW) {
boolean result = true;
try {
String str = lineTxt + "\r\n";
byte[] strTxt = str.getBytes();
if (isRW) {
file.write(strTxt);
}
} catch (Exception e) {
}
return result;
}
/**
* 读取文件内容
*/
public static ArrayList readTxtFile(String fileName) throws IOException, Exception {
ArrayList txtList = new ArrayList();
boolean result = true;
//检查文件是否存在
result = FileManage.checkFileExist(fileName);
//打开文件
if (result) {
RandomAccessFile file = new RandomAccessFile(fileName, "rw");
try {
String inLine = file.readLine();
while (inLine != null) {
if (!inLine.equals(""))
txtList.add(inLine);
inLine = file.readLine();
}
} catch (Exception e) {
}
finally {
file.close();
}
}
return txtList;
}
/**
* 向文件写入文本
*/
public static boolean writeTxtFile(String fileName, ArrayList lineTxt) throws IOException, Exception {
boolean result = true;
//检查文件是否存在
result = FileManage.checkFileExist(fileName);
//存在,先删除文件
result = (result) ? FileManage.delete(fileName) : true;
//打开文件
if (result) {
RandomAccessFile file = new RandomAccessFile(fileName, "rw");
try {
for (int i = 0; i < lineTxt.size(); i++) {
//写一行文件内容
result = result && FileManage.writeLineFile(file, lineTxt.get(i).toString(), true);
}
file.close();
} catch (Exception e) {
} finally {
file.close();
}
}
return result;
}
}共0条评论