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();
}
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();
}
相关文章推荐
- java常用类——java web接受文件表单时常用文件解析方法
- javaweb中静态文件的常用处理方法汇总
- 操作文件处理常用方法(Java实现)
- JAVA处理日期时间常用方法
- JAVA处理日期时间常用方法
- java 追加内容到文件末尾的几种常用方法
- 关于Eclipse无法重新编译java文件的处理方法之一
- Java通过BufferedWriter追加内容到文件末尾的几种常用方法
- Java常用文件目录处理代码集
- java 追加内容到文件末尾的几种常用方法
- Android 丢失R.java文件处理方法
- java.io.File操作文件的一些常用方法
- JAVA处理日期时间常用方法
- Java中的字符处理常用方法
- java中properties文件的常用读取方法
- 关于Eclipse无法重新编译java文件的处理方法之一
- Java处理文件的方法(FileInputStream、FileReader、File*)
- JAVA处理日期时间常用方法: java.util.Calendar
- java中常用的处理时间方法
- JAVA处理日期时间常用方法