您的位置:首页 > 编程语言 > Java开发

java常用类——java常用文件处理方法

2018-02-26 10:32 537 查看
开发过程中用到过的一些文件处理方法,整理了一下,方便以后查找参阅。
1.获取指定文件夹下面文件名称集合       /**
* 获取指定路径下所有文件名称集合
*/
public static List<String> getFileList(String strPath) {
List<String> filelist=new ArrayList<String>();
File dir = new File(strPath);
File[] files = dir.listFiles(); // 该文件目录下文件全部放入数组
if (files != null) {
for (int i = 0; i < files.length; i++) {
String fileName = files[i].getName();
if (files[i].isDirectory()) { // 判断是文件还是文件夹
getFileList(files[i].getAbsolutePath()); // 获取文件绝对路径
} else if (fileName.endsWith("png")||fileName.endsWith("jpg")||fileName.endsWith("JPG")) { // 判断文件名是否以.avi结尾
filelist.add(files[i].getName());
} else {
continue;
}
}

}
retu
4000
rn filelist;
}2.创建文件/**
* 创建文件
*/
public static File createFile(String allPath) throws IOException {

File file = new File(allPath);

if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
if (!file.exists()) {
file.createNewFile();
}
return file;
}3.复制文件/**
* 复制文件
* @param fromFile
* @param toFile
* 2016年12月19日 下午3:31:50
* @throws IOException
*/
public static void copyFile(File fromFile,File toFile){
FileInputStream ins=null;
FileOutputStream out=null;
try {
ins = new FileInputStream(fromFile);
out = new FileOutputStream(toFile);
byte[] b = new byte[1024];
int n=0;
while((n=ins.read(b))!=-1){
out.write(b, 0, n);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{

try {
if(ins!=null)ins.close();
if(ins!=null)out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}4.文件压缩/**
* 文件压缩
* @param srcFile 目录或者单个文件
* @param zipFile 压缩后的ZIP文件
*/
public static void doCompress(File srcFile, File zipFile) throws IOException {
ZipOutputStream out = null;
try {
out = new ZipOutputStream(new FileOutputStream(zipFile));
doCompress(srcFile, out);
} catch (Exception e) {
try {
throw e;
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} finally {
out.close();//记得关闭资源
}
}

public static void doCompress(String filelName, ZipOutputStream out) throws IOException{
doCompress(new File(filelName), out);
}

public static void doCompress(File file, ZipOutputStream out) throws IOException{
doCompress(file, out, "");
}

public static void doCompress(File inFile, ZipOutputStream out, String dir) throws IOException {
if ( inFile.isDirectory() ) {
File[] files = inFile.listFiles();
if (files!=null && files.length>0) {
for (File file : files) {
String name = inFile.getName();
if (!"".equals(dir)) {
name = dir + "/" + name;
}
ZipUtils.doCompress(file, out, name);
}
}
} else {
ZipUtils.doZip(inFile, out, dir);
}
}

public static void doZip(File inFile, ZipOutputStream out, String dir) throws IOException {
String entryName = null;
if (!"".equals(dir)) {
entryName = dir + "/" + inFile.getName();
} else {
entryName = inFile.getName();
}
ZipEntry entry = new ZipEntry(entryName);
out.putNextEntry(entry);

int len = 0 ;
byte[] buffer = new byte[1024];
FileInputStream fis = new FileInputStream(inFile);
while ((len = fis.read(buffer)) > 0) {
out.write(buffer, 0, len);
out.flush();
}
//out.closeEntry();
fis.close();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: