重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章将为大家详细讲解有关java中常用的文件操作方法有哪些,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
发展壮大离不开广大客户长期以来的信赖与支持,我们将始终秉承“诚信为本、服务至上”的服务理念,坚持“二合一”的优良服务模式,真诚服务每家企业,认真做好每个细节,不断完善自我,成就企业,实现共赢。行业涉及成都PE包装袋等,在网站建设公司、营销型网站、WAP手机网站、VI设计、软件开发等项目上具有丰富的设计经验。
常用的文件的操作方法
public class FileUtility {
/**
* 创建目录
*
* @param fileDir
* 文件所在目录
*
* @throws java.lang.Exception
*/
public static void createDir(String filePath){
File fileDir = new File(filePath);
try {
if (!fileDir.exists()) {
fileDir.mkdir();
}
} catch (Exception ex) {
}
}
/**
* @roseuid 46345A88024A
*/
public FileUtility() {
}
/**
* 如果文件存在,则删除后并重新创建;如果不存在,则创建。
*
* @param filePath -
* 文件路径
* @param fileName -
* 文件名称
* @param sb -
* 输出的内容
* @roseuid 4612F3C103B9
*/
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) {
}
}
/**
* 删除文件
*
* @param filePath -
* 文件路径
* @param fileName -
* 文件名称
* @roseuid 4612F3C103B9
*/
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) {
}
}
/**
* 创建新的文件
* @param filePath
* @param fileName
*/
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) {
}
}
/**
* 将内容添加到文档中去
* @param filePath
* @param fileName
* @param sb
*/
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) {
}
}
/**
* 将指定目录下的文件进行压缩
*
* @param zipList -
* 文件名称,该list中的文件名压缩到压缩包中
* @param filePath -
* 文件路径
* @param zipName -
* 压缩后文件的名称
* @roseuid 4612F4AE03B1
*/
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;
}
}
/**
* 删除临时文件
*
* @param delList -
* 要删除的文件的名称的列表
* @param filePath -
* 删除文件的路径
* @roseuid 4612F5380341
*/
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) {
}
}
/**
* 从zip文件中获取指定名称的文件
*
* @param filePath -
* zip所在文件目录
* @param zipFileName -
* zip文件名称
* @param fileName -
* 要读出的文件名称
* @return BufferedReader
* @roseuid 4612F788032E
*/
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;
}
/**
* 保存导出文件
*
* @param saveDir
* String
* @param fileName
* String
* @param doc
* Document
*/
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) {
}
}
/**
* 检查路径是否合法,并抛出异常
* @param path
* @return
*/
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中常用的文件操作方法有哪些”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。