这篇文章将为大家详细讲解有关java中常用的文件操作方法有哪些,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
常用的文件的操作方法
public class FileUtility {
public static void createDir(String filePath){
File fileDir = new File(filePath);
try {
if (!fileDir.exists()) {
fileDir.mkdir();
}
} catch (Exception ex) {
}
}
public FileUtility() {
}
public static void createFile(String filePath, String fileName,
StringBuffer sb) {
createDir(filePath);
java.io.File file = new java.io.File(filePath, fileName);
try {
if (file.exists() == true) {
file = new java.io.File(filePath, fileName);
file.delete();
}
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
Writer out = new OutputStreamWriter(fos, "GB2312");
out.write(sb.toString());
out.flush();
out.close();
fos.close();
out = null;
fos = null;
} catch (Exception ex) {
}
}
public static void deleteFile(String filePath, String fileName)
{
createDir(filePath);
java.io.File file = new java.io.File(filePath, fileName);
try {
if (file.exists()) {
file = new java.io.File(filePath, fileName);
file.delete();
}
file = null;
} catch (Exception ex) {
}
}
public static void createOrClearFile(String filePath, String fileName)
{
createDir(filePath);
File fileDS = new File(filePath, fileName);
try {
if (fileDS.exists()) {
fileDS = new File(filePath, fileName);
fileDS.delete();
}
fileDS.createNewFile();
} catch (Exception ex) {
}
}
public static void writeAppendFile(String filePath, String fileName,
StringBuffer sb) {
try {
FileWriter fileWriter = new FileWriter(filePath + File.separator
+ fileName, true);
PrintWriter out = new PrintWriter(fileWriter);
out.print(sb.toString());
out.flush();
fileWriter.flush();
out.close();
fileWriter.close();
out = null;
fileWriter = null;
} catch (Exception ex) {
}
}
public static void zip(List zipList, String filePath, String zipName)
{
BufferedInputStream origin = null;
ZipOutputStream out = null;
int BUFFER = 2048;
createDir(filePath);
try {
File zipFile = new File(filePath, zipName);
FileOutputStream dest = new FileOutputStream(zipFile);
byte data[] = new byte[BUFFER];
//针对压缩的文件打入标志位,当解压缩的时候判断是否被损坏,如果被损坏的话则不予解压缩
out = new ZipOutputStream(new BufferedOutputStream(dest));
// 设置文件为压缩方式
out.setMethod(ZipOutputStream.DEFLATED);
int num = zipList.size();
for (int i = 0; i < num; i++) {
FileInputStream fi = new FileInputStream(filePath
+ File.separator + (String) zipList.get(i));
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry((String) zipList.get(i));
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
origin.close();
}
out.close();
} catch (Exception e) {
} finally {
origin = null;
out = null;
}
}
public static void delete(List delList, String filePath)
{
try {
for (int i = 0; i < delList.size(); i++) {
String fileName = (String) delList.get(i);
File f = new File(filePath, fileName);
if (f.exists()) {
f.delete();
}
fileName = null;
f = null;
}
} catch (Exception e) {
}
}
public static BufferedReader getZipReader(String filePath,
String zipFileName, String fileName) {
BufferedReader br = null;
try {
ZipFile zf = new ZipFile(filePath + File.separator + zipFileName);
ZipEntry entry = zf.getEntry(fileName);
br = new BufferedReader(new InputStreamReader(zf
.getInputStream(entry)));
} catch (Exception e) {
}
return br;
}
public static Document readXmlFile(File xmlFile) {
Document doc = null;
try {
SAXBuilder saxBuilder = new org.jdom.input.SAXBuilder(false);
doc = saxBuilder.build(xmlFile);
} catch (Exception ex) {
}
return doc;
}
public static void saveXmlFile(String saveDir, String fileName, Document doc) {
createDir(saveDir);
try {
java.io.FileWriter fw = null;
java.io.BufferedWriter bw = null;
File file = new File(saveDir, fileName);
if (file.exists()) {
file.delete();
}
if (file.createNewFile()) {
XMLOutputter xmlOut = new XMLOutputter(" ", true);
xmlOut.setEncoding("GB2312");
fw = new java.io.FileWriter(file);
bw = new java.io.BufferedWriter(fw);
xmlOut.output(doc, bw);
bw.flush();
fw.flush();
bw.close();
fw.close();
bw = null;
fw = null;
}
} catch (Exception ex) {
}
}
public static void createEntryFile() {
try {
// 创建xml文件
Element rootElement = new Element("entry");
Document doc = new Document(rootElement);
FileUtility.saveXmlFile(FilePath, FileName, doc);
} catch (Exception e) {
}
}
public void zip(List tableList, String entryFileName,
String backupFilePath, String backupFileName){
// 1.获取所有的压缩文件名
List fileList = new ArrayList();
int num = tableList.size();
TableObject to = null;
for (int i = 0; i < num; i++) {
to = (TableObject) tableList.get(i);
//只有记录大于0的表才进行输出
if (to.getRecordnum() > 0) {
fileList.add(to.getBackupfile());
}
}
fileList.add(entryFileName);
// 2.将所有文件进行压缩并将临时文件删除
try {
FileUtility.zip(fileList, backupFilePath, backupFileName);
FileUtility.delete(fileList, backupFilePath);
} catch (Exception e) {
}
}
public static String checkPath(String path){
if (!path.matches("[^*?" < > | ]{1,}")) {
} else {
File dir = new File(path);
if (!dir.exists()) {
}
return dir.getAbsolutePath() + File.separator;
}
}
}
关于“java中常用的文件操作方法有哪些”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。