`

JAVA文件/目录操作类

阅读更多

一个JAVA对于文件/目录操作类,支持WINDOWS/LINUX/UNIX平台,其中ZIP与UNZIP对中文文件/目录名支持的不好,如果发现错误请联系我jenshy@126.com,转载请添加我的BLOG链接 http://blog.csdn.net/jenshy

/*
* @author JenshyHong
* @version 1.0
*/
import java.io.File;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class FileOperate
{
public static boolean renameFile(String strSrcPath,String strDstPath)
{
String srcPath,dstPath;
srcPath=strSrcPath.trim();
dstPath=strDstPath.trim();
if(srcPath.charAt(srcPath.length()-1)==File.separatorChar)
srcPath=srcPath.substring(0,srcPath.length()-1);
if(dstPath.charAt(dstPath.length()-1)==File.separatorChar)
dstPath=dstPath.substring(0,dstPath.length()-1);

if( srcPath.equals("") || dstPath.equals("") || srcPath.equals(dstPath) )
{
System.out.println("Path is error(FileOperate.renameFile())");
return false;
}
File file1=new File(srcPath);
if( !file1.exists() )
{
System.out.println("Source Path is error(FileOperate.renameFile())");
return false;
}

if(file1.isDirectory()) //dir
{
System.out.println("Source Path isn't file(FileOperate.renameFile())");
return false;
}
else //file
{
File file2 = new File(dstPath);
if (file2.exists())
{
if (file2.isDirectory())
{
System.out.println(
"Destination Path is error(FileOperate.copyFile())");
return false;
}
else
{
file2.delete();
}
}
else
{
File file3 = file2.getParentFile();
if ( (file3 != null) && !file3.exists())
{
file3.mkdirs();
}
}
return file1.renameTo(file2);
}

}//function renameFile end

public static void copyFile(String strSrcPath,String strDstPath)
{
String srcPath,dstPath;
srcPath=strSrcPath.trim();
dstPath=strDstPath.trim();
if(srcPath.charAt(srcPath.length()-1)==File.separatorChar)
srcPath=srcPath.substring(0,srcPath.length()-1);
if(dstPath.charAt(dstPath.length()-1)==File.separatorChar)
dstPath=dstPath.substring(0,dstPath.length()-1);

if( srcPath.equals("") || dstPath.equals("") || srcPath.equals(dstPath) )
{
System.out.println("Path is error(FileOperate.copyFile())");
return;
}
File file1=new File(srcPath);
if( !file1.exists() || file1.isDirectory())
{
System.out.println("Source Path is error(FileOperate.copyFile())");
return;
}
File file2=new File(dstPath);
File file3=file2.getParentFile();
if((file3!=null)&&!file3.exists())
{

file3.mkdirs();
}
try
{
BufferedInputStream in=new BufferedInputStream(new FileInputStream(srcPath),8192);
BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(dstPath),8192);
byte[] btData=new byte[1024];
int size;
while((size=in.read(btData))!=-1)
{
out.write(btData,0,size);
}
out.flush();
in.close();
out.close();

}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}

}//copyFile end

public static void copyFiles(String strSrcDir,String strDstDir)
{
String srcDir, dstDir;
srcDir = strSrcDir.trim();
dstDir = strDstDir.trim();
if (!srcDir.equals(File.separator))
if (srcDir.charAt(srcDir.length() - 1) == File.separatorChar)
srcDir = srcDir.substring(0, srcDir.length() - 1);
if (!dstDir.equals(File.separator))
if (dstDir.charAt(dstDir.length() - 1) == File.separatorChar)
dstDir = dstDir.substring(0, dstDir.length() - 1);

if (srcDir.equals("") || dstDir.equals("") || srcDir.equals(dstDir))
{
System.out.println("Path is error(FileOperate.copyFiles())");
return;
}
File file1 = new File(srcDir);
if (!file1.isDirectory())
{
System.out.println("Source Path is error(FileOperate.copyFiles())");
return;
}
File file2 = new File(dstDir);
if (file2.exists())
{
if (!file2.isDirectory())
{
System.out.println("Destination Path is error(FileOperate.copyFiles())");
return;
}
}
else
{
file2.mkdirs();
}

String[] fileNames = file1.list();

for (int i = 0; i < fileNames.length; i++)
{
File file = new File(file1, fileNames[i]);
if (file.isDirectory())
{
copyFiles( srcDir + File.separator + fileNames[i], dstDir + File.separator + fileNames[i] );
}
else
{
copyFile( srcDir + File.separator + fileNames[i], dstDir + File.separator + fileNames[i] );
}
}

}//copyFiles end

public static void moveFile(String strSrcPath,String strDstPath)
{
copyFile(strSrcPath,strDstPath);
deleteFile(strSrcPath);
}//moveFile end

public static void moveFiles(String strSrcDir,String strDstDir)
{
copyFiles(strSrcDir,strDstDir);
deleteFiles(strSrcDir,true);
}//moveFiles end


public static boolean deleteFile(String strPath)
{
File file=new File(strPath.trim());
if(file.exists())
{
System.out.println("delete file is unsuccessful(FileOperate.deleteFile())");
return file.delete();
}
else
{
System.out.println("File or Dir isn't existent(FileOperate.deleteFile())");
return false;
}
}//deleteFile end

public static void deleteFiles(String strDir,boolean deleteDir)
{
String dir=strDir.trim();
if(dir.charAt(dir.length()-1)==File.separatorChar)
dir=dir.substring(0,dir.length()-1);
if(dir.equals(""))
{
System.out.println("Path is error(FileOperate.deleteFiles())");
return;
}
File file=new File(dir);
if(!file.exists())
{
System.out.println("file or dir isn't existent(FileOperate.deleteFiles())");
return;
}
if (file.isDirectory())
{
String[] fileNames=file.list();
for (int i = 0; i < fileNames.length; i++)
{
File subFile=new File(file,fileNames[i]);
if (subFile.isDirectory())
{
deleteFiles(dir+File.separator+fileNames[i],deleteDir);
if (deleteDir)
subFile.delete();
}
else
{
subFile.delete();
}
}
if(deleteDir)
file.delete();
}
else
{
file.delete();
}
}//deleteFiles end

public static void zip(String strSrcPath, String strDstPath)
{
String srcPath, dstPath;
srcPath = strSrcPath.trim();
dstPath = strDstPath.trim();
if (srcPath.charAt(srcPath.length() - 1) == File.separatorChar)
srcPath = srcPath.substring(0, srcPath.length() - 1);
if (dstPath.charAt(dstPath.length() - 1) == File.separatorChar)
dstPath = dstPath.substring(0, dstPath.length() - 1);
if (srcPath.equals("") || dstPath.equals("") || srcPath.equals(dstPath))
{
System.out.println("Path is error(FileOperate.zip())");
return;
}
File srcFile = new File(srcPath);
try
{
if (srcFile.exists())
{
ZipOutputStream zipOut;
if (dstPath.endsWith(".zip") || dstPath.endsWith(".ZIP"))
{
zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(dstPath)));
//zipOut = new ZipOutputStream(new FileOutputStream(dstPath));
}
else
{
File file = new File(dstPath);
if (!file.exists())
file.mkdirs();
zipOut = new ZipOutputStream(new FileOutputStream(new File(
file, srcFile.getName() + ".zip")));
}
zip(srcFile, zipOut, "");
zipOut.close();
}
else
{
System.out.println("file or dir isn't existent(FileOperate.zip())");
}
}

catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}

}

private static void zip(File file, ZipOutputStream zip, String entryName)
{
try
{
if (file.isDirectory())
{
ZipEntry ze = new ZipEntry(entryName + file.getName() + "/");
ze.setTime(file.lastModified());
zip.putNextEntry(ze);
zip.closeEntry();
String[] names = file.list();
for (int i = 0; i < names.length; i++)
{
zip(new File(file, names[i]), zip, entryName + file.getName() + "/");
}
}
else
{
int len;
byte[] buf = new byte[1024];

BufferedInputStream in = new BufferedInputStream(new
FileInputStream(file), 1024);
ZipEntry ze = new ZipEntry(entryName + file.getName());
ze.setTime(file.lastModified());
zip.putNextEntry(ze);
while ( (len = in.read(buf)) != -1)
zip.write(buf, 0, len);
zip.flush();
in.close();
zip.closeEntry();
}

}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}

}
public static void unzip(String strSrcPath, String strDstPath)
{
String srcPath, dstPath;
srcPath = strSrcPath.trim();
dstPath = strDstPath.trim();
if (srcPath.charAt(srcPath.length() - 1) == File.separatorChar)
srcPath = srcPath.substring(0, srcPath.length() - 1);
if (dstPath.charAt(dstPath.length() - 1) == File.separatorChar)
dstPath = dstPath.substring(0, dstPath.length() - 1);
if (srcPath.equals("") || dstPath.equals("") || srcPath.equals(dstPath))
{
System.out.println("Path is error(FileOperate.unzip())");
return;
}
if(!new File(srcPath).exists())
{
System.out.println("Source File isn't existent(FileOperate.unzip())");
return;
}
if (srcPath.endsWith(".zip") || srcPath.endsWith(".ZIP"))
{
try
{
File myDir = new File(dstPath);
//目标目录,解压缩到此目录下
if (myDir.exists())
{
if(!myDir.isDirectory())
{
myDir.delete();
myDir.mkdirs();
}
}
else
{
myDir.mkdirs(); //若此目录不存在,就建立它
}
ZipInputStream myZipInput = new ZipInputStream(
new BufferedInputStream( new FileInputStream(srcPath)));
//套入 ZipInputStream
ZipEntry myZipEntry = null;
while ( (myZipEntry = myZipInput.getNextEntry()) != null)
{
/* 读取下一个 ZipEntry,并将串流内的位置
移至该 entry 所指之资料的开头。 */
//========= 以下是解压缩 =============
if (myZipEntry.isDirectory())
{ //若这个 entry 指到的是目录
File theDir = new File(myDir,myZipEntry.getName());
//前面冠上解压缩的目标目录
if (!theDir.exists())
theDir.mkdirs(); //若此目录不存在,就建立它
theDir.setLastModified(myZipEntry.getTime());
myZipInput.closeEntry();
}
else
{ //若这个 entry 指到是文件
File file=new File(myDir,myZipEntry.getName());
File myPath = file.getParentFile();
//此文件的 Path,即它被压缩时的上层目录
if (myPath != null)
{
//若此目录不存在,就建立它
if(!myPath.exists())
myPath.mkdirs();
}
BufferedOutputStream myOut =new BufferedOutputStream( new FileOutputStream(file));

byte[] btBuf=new byte[1024];
int len;
while ((len=myZipInput.read(btBuf)) != -1)
myOut.write(btBuf,0,len); //解压缩读入后,再写出到一般文件
myOut.close();
file.setLastModified(myZipEntry.getTime());
myZipInput.closeEntry();
//关闭此 entry,并让串流内指到适当位置,以备下个 entry 的读取。
}
}
myZipInput.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
else
{
System.out.println("file is error(FileOperate.unzip())");
}
}//Function unzip end


}//class FileOperate end

分享到:
评论

相关推荐

    java文件操作类

    java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java...

    java实现对文件的各种操作的工具类.md

    # java实现对文件的各种操作的工具类 ## 可以实现的操作有: 1. 删除单个文件 2. 删除文件夹及文件夹下的文件 3. 使用文件流对单个文件进行复制 4. 复制整个文件夹内容(包含子文件夹中的所有内容) 5. ...

    Java文件操作封装类

    Java文件操作封装类

    java 文件操作类

    java 文件操作类 20多个实用方法,操作文件的好帮手。 JAVA自定义Util大放送-集成j2ee精英团队十年编程之精华

    java文件操作工具类

    java文件操作工具类是java中针对文件操作的一个工具类,可以方便的读取,下载,上传文件等操作,希望可以帮到大家。

    Java文件操作类

    pan.razerpen.file中提供一个多对象单文件存储类FileMap和一个单对象单文件存储类FilePage。 提供方便快捷的基本类型和对象的文件存取方式。详细用法见sample.razerpen.file

    java properties文件操作工具类,可追加修改

    此工具类只用于Java后端在操作Properties文件的时候写的工具类,方便properties文件的存取操作

    java操作文件工具类

    文件工具类java操作文件工具类java操作文件工具类java操作文件工具类java操作文件工具类

    有关java文件操作的类和用法

    java文件操作相关的类和基本用法,都是一些基本的用法,但很重要

    Java 功能丰富的文件操作类.rar

    与大家分享一个功能丰富的Java文件操作类,类中封装了一些常用的文件操作,大部分涉及文件的修改、内容替换等。类中的方法都是静态方法,不需要生成此类的实例, 为避免生成此类的实例,构造方法被申明为private类型...

    Java文件处理工具类--FileUtil

    import java.io.*; /** * FileUtil. Simple file operation class. * * @author BeanSoft * */ public class FileUtil { /** * The buffer. */ protected static byte buf[] = new byte[1024]; /**...

    自己写的java中文件的操作工具类,包括对目录的管理

    自己写的java中文件的操作工具类,包括对目录的管理

    java文件操作的工具类

    此文档为java中操作File对象的一个通用工具类,包含了byte数组和File类的相互转换

    文件操作类(java)

    java实现对文件的基本操作,如创建,删除,读取,写入,解压缩,复制,移动等操作。{@link #createFile(String)} * * {@link #readFile(String, String)} * * {@link #moveFile(String, String)} * {@link #...

    java基于commons文件上传工具类

    本人一个项目中写的java基于commons文件上传工具类

    java视频教程—Java文件操作

    java视频教程 Java文件操作 JavaFile

    Java_IO及文件管理.ppt

    Java_IO种各接口描述,File类,文件和目录操作

    hadoop Java接口 文件操作类

    基于hadoop Java接口 文件操作类,对hadoop服务进行增删改查等系列操作,非常实用

Global site tag (gtag.js) - Google Analytics