/**
* 以文件流的方式复制文件
* @param src 文件源目录
* @param dest 文件目的目录
* @throws IOException
*/
public static void copyFile(String src,String dest) throws IOException{
FileInputStream in=new FileInputStream(src);
File file=new File(dest);
if(!file.exists())
file.createNewFile();
FileOutputStream out=new FileOutputStream(file);
int c;
byte buffer[]=new byte[1024];
while((c=in.read(buffer))!=-1){
for(int i=0;i<c;i++)
out.write(buffer[i]);
}
in.close();
out.close();
}
/**
* 利用PrintStream写文件
* @param src 写入文件的路径
*/
public static void PrintStreamOut(String src,String contents){
try {
FileOutputStream out=new FileOutputStream(src);
PrintStream p=new PrintStream(out);
for(int i=0;i<contents.length();i++)
p.print(contents.charAt(i));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* 利用PrintStream写文件
* @param src 写入文件的路径
*/
public static void StringBufferOut(String src,String contents) throws IOException{
File file=new File(src);
if(!file.exists())
file.createNewFile();
FileOutputStream out=new FileOutputStream(file,true);
for(int i=0;i<contents.length();i++){
StringBuffer sb=new StringBuffer();
sb.append(contents.charAt(i));
out.write(sb.toString().getBytes("utf-8"));
}
out.close();
}
/**
* 文件重命名
* @param path 文件目录
* @param oldname 原来的文件名
* @param newname 新文件名
* @param num 已存在该文件名
*/
public static void renameFile(String path,String oldname,String newname,int num,String suffix){
if(!oldname.equals(newname)){//新的文件名和以前文件名不同时,才有必要进行重命名
File oldfile=new File(path+"/"+oldname);
File newfile = null;
String temp = newname;
if(num==0){
newname = newname+suffix;
newfile=new File(path+"/"+newname);
}else{
newname = newname+num+suffix;
newfile=new File(path+"/"+newname);
}
if(newfile.exists()){//若在该目录下已经有一个文件和新文件名相同,则命名为newname1.2.3...
num++;
renameFile(path,oldname,temp,num,suffix);
}else{
oldfile.renameTo(newfile);
}
}
}
/**
* 转移文件目录
* @param filename 文件名
* @param oldpath 旧目录
* @param newpath 新目录
* @param cover 若新目录下存在和转移文件具有相同文件名的文件时,是否覆盖新目录下文件,cover=true将会覆盖原文件,否则不操作
*/
public static void changeDirectory(String filename,String oldpath,String newpath,boolean cover){
if(!oldpath.equals(newpath)){
File oldfile=new File(oldpath+"/"+filename);
File newfile=new File(newpath+"/"+filename);
if(newfile.exists()){//若在待转移目录下,已经存在待转移文件
if(cover){//覆盖
newfile.delete();
oldfile.renameTo(newfile);
}else
System.out.println("在新目录下已经存在:"+filename);
}
else{
oldfile.renameTo(newfile);
}
}
}
/**
* @author LiZhen
* @param strFile
* @throws Exception
*/
public static void UnZip(String strFile) throws Exception {
// 输出文件夹
String baseDir = "d:\";
ZipFile zFile = new ZipFile(strFile);
System.out.println(zFile.getName());
Enumeration en = zFile.entries();
ZipEntry entry = null;
while (en.hasMoreElements()) {
// 得到其中一项ZipEntry
entry = (ZipEntry) en.nextElement();
// 如果是文件夹则不处理
if (entry.isDirectory()) {
System.out.println("Dir: " + entry.getName() + " skipped..");
} else {
// 如果是文件则写到输出目录
copyFile(zFile, baseDir, entry);
}
}
zFile.close();
}
private static void copyFile(ZipFile source, String baseDir, ZipEntry entry)
throws Exception {
// 以ZipEntry为参数得到一个InputStream,并写到OutputStream中
// 是否需要创建目录
mkdirs(baseDir, entry.getName());
// 建立输出流
OutputStream os = new BufferedOutputStream(new FileOutputStream(
new File(baseDir, entry.getName())));
// 取得对应ZipEntry的输入流
InputStream is = new BufferedInputStream(source.getInputStream(entry));
int readLen = 0;
byte[] buf = new byte[1024];
// 复制文件
System.out.println("Extracting: " + entry.getName() + "t"
+ entry.getSize() + "t" + entry.getCompressedSize());
while ((readLen = is.read(buf, 0, 1024)) != -1) {
os.write(buf, 0, readLen);
}
is.close();
os.close();
System.out.println("Extracted: " + entry.getName());
}
/**
* 给定根目录,返回一个相对路径所对应的实际文件名.
*
* @param baseDir
* 指定根目录
* @param absFileName
* 相对路径名,来自于ZipEntry中的name
* @return java.io.File 实际的文件
*/
private static void mkdirs(String baseDir, String relativeFileName) {
String[] dirs = relativeFileName.split("/");
File ret = new File(baseDir);
if (dirs.length > 1) {
for (int i = 0; i < dirs.length - 1; i++) {
ret = new File(ret, dirs[i]);
}
}
if (!ret.exists()) {
ret.mkdirs();
}
}
FileHelper
最新推荐文章于 2021-02-23 12:50:47 发布