Java文件操作工具类

来源:互联网 发布:淘宝综合排名搜不到 编辑:程序博客网 时间:2024/05/22 01:50

近来,闲来无事写了一些常用的工具类,不是很丰富,只是封装了一些 简单的方法。该工具类包括文件的创建,文件的删除,文件的复制,文件的读写,文件的压缩、解压等简单操作。这次没有把压缩、解压的方法加进来,下次再对文件的压缩、解压进行学习。第一次写,写的不好,请多多指教。

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.Stack;


/**
 * 文件操作工具类
 * @author wzy
 *
 */
public class FileOperate {
//新建文件/文件夹
public static void createNew(String path,boolean isFolder) throws IOException{
File file = new File(path);
if(!file.getParentFile().exists()){
if(isFolder){
file.mkdirs();
}else{
file.getParentFile().mkdirs();
file.createNewFile();
}
}else{
if(isFolder){
file.mkdir();
}else{
file.createNewFile();
}
}
}
//新建文件
public static void createFile(String path) throws IOException{
File file = new File(path);
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
file.createNewFile();
}
//新建文件夹
public static void createFolder(String path) throws IOException{
File file = new File(path);
file.mkdirs();
}
//删除文件夹及文件夹下所有内容
public static void deleteFile(String path){
File file = new File(path);
if(!file.exists())return;
if(file.isFile()){
file.delete();
return;
}
Stack<File> folders = new Stack<File>();//栈记录文件夹
int n = 0;
folders.push(file);
n++;
while(n>0){
File[] listfiles = folders.get(folders.size()-n).listFiles();
n--;
for(File f : listfiles){
if(f.isDirectory()){
folders.push(f); //进栈
n++;
}else if(f.isFile()){
f.delete(); //删除文件
}
}
}
//System.out.println("size:"+folders.size());
while(!folders.empty()){
folders.pop().delete(); //删除文件夹
}

}
//复制文件
public static void copyFile(String srcFilePath,String desFolder) throws Exception{
if(desFolder.endsWith("\\")){
desFolder = desFolder.substring(0, desFolder.lastIndexOf("\\"));
}
File srcfile = new File(srcFilePath);
if(!srcfile.exists())throw new RuntimeException("复制失败,源路径非法");
File dec = new File(desFolder);
if(dec.isFile())throw new RuntimeException("复制失败,目标路径非法");
if(!dec.exists())dec.mkdirs();
if(srcfile.isFile()){
InputStream in = new FileInputStream(srcfile);
writeFileStream(desFolder+"\\"+srcfile.getName(), in);
in.close();
}else if(srcfile.isDirectory()){
StringBuffer spath = new StringBuffer();
spath.append(desFolder);
Stack<HashMap<String,List<File>>> folders = new Stack<HashMap<String,List<File>>>();
HashMap<String,List<File>> rootFolder = new HashMap<String,List<File>>();
List<File> lfile = new ArrayList<File>();
lfile.add(srcfile);
rootFolder.put("root",lfile);
folders.push(rootFolder);
String parent = desFolder+"\\"+srcfile.getName();
while(!folders.isEmpty()){
HashMap<String,List<File>> hmfolders = folders.pop();
Set<String> key = hmfolders.keySet();
String parentfolder = key.iterator().next();
System.out.println(parentfolder);
List<File> lfodler = hmfolders.get(parentfolder);
for(int i=0;i < lfodler.size();i++){
File[] files = lfodler.get(i).listFiles();
if(!parentfolder.equals("root"))
parent = parentfolder +"\\"+ lfodler.get(i).getName();
List<File> listfodler = new ArrayList<File>();
for(File f : files){
if(f.isFile()){
InputStream in = new FileInputStream(f);
writeFileStream(parent+"\\"+f.getName(),in);
}else if(f.isDirectory()){
createFolder(parent+"\\"+f.getName());
listfodler.add(f);
}
}
HashMap<String,List<File>> hm = new HashMap<String,List<File>>();
hm.put(parent,listfodler);
folders.push(hm);
}
}
}
}

//读取文件返回byte[]
public static byte[] readFile(String path) throws Exception{
File file = new File(path);
if(!file.isFile()||!file.exists()){
throw new RuntimeException("路径参数非法或文件不存在!");
}
InputStream in = new FileInputStream(new File(path));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while((len = in.read(buffer))!=-1){
bos.write(buffer, 0, len);
}
in.close();
bos.close();
return bos.toByteArray();
}

//写文件字符流
public static void writeFiler(String path,String str,boolean isAppand) throws FileNotFoundException{
File file = new File(path);
if(!file.isFile()){
throw new RuntimeException("路径参数非法");
}
PrintWriter pw = new PrintWriter(new BufferedOutputStream(new FileOutputStream(file, isAppand)));
pw.write(str);
pw.flush();
pw.close();
}
//写文件字节流
public static void writeFileStream(String path,InputStream in) throws Exception{
File file = new File(path);
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
file.createNewFile();
int len = -1;
byte[] buffer = new byte[1024];
FileOutputStream fos = new FileOutputStream(file); 
while((len=in.read(buffer))!=-1){
fos.write(buffer,0,len);
}
fos.flush();
in.close();
fos.close();
}
}
 


0 0
原创粉丝点击