java io封装_java_io_操作封装

package com.wiker;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.BufferedReader;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileFilter;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStream;

import java.io.PrintWriter;

import java.math.BigInteger;

import java.nio.ByteBuffer;

import java.nio.MappedByteBuffer;

import java.nio.channels.FileChannel;

import java.security.MessageDigest;

import java.text.DecimalFormat;

import java.util.Arrays;

import java.util.HashSet;

import java.util.List;

import java.util.Set;

import java.util.zip.ZipEntry;

import java.util.zip.ZipInputStream;

import java.util.zip.ZipOutputStream;/**

* @author Wiker Yong Email:wikeryong@gmail.com

* @date 2013-11-8 下午6:21:45

* @version 1.0-SNAPSHOT*/@SuppressWarnings("resource")public classFileUtils {/**

* 获取文件MD5值

*

* @param file

* @return*/

public staticString getMd5ByFile(File file) {

String value= null;

FileInputStreamin = null;try{in = newFileInputStream(file);

MappedByteBuffer byteBuffer= in.getChannel().map(FileChannel.MapMode.READ_ONLY, 0,

file.length());

MessageDigest md5= MessageDigest.getInstance("MD5");

md5.update(byteBuffer);

BigInteger bi= new BigInteger(1, md5.digest());

value= bi.toString(16);

}catch(Exception e) {

e.printStackTrace();

}finally{if (null != in) {try{in.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}returnvalue;

}/**

* 获取文件大小

*

* @param file

* @return*/

public static longgetFileLength(File file)

throws IOException {

FileInputStream fis= null;

fis= newFileInputStream(file);returnfis.available();

}/**

* 读取文件到二进制

*

* @author WikerYong Email:yw_312@foxmail.com

* @version 2012-3-23 上午11:47:06

* @param file

* @return

* @throws IOException*/

public static byte[] getBytesFromFile(File file)

throws IOException {

InputStreamis = newFileInputStream(file);long length =file.length();if (length >Integer.MAX_VALUE) {//File is too large

}byte[] bytes = new byte[(int) length];//Read in the bytes

int offset = 0;int numRead = 0;while (offset = 0) {

offset+=numRead;

}//Ensure all the bytes have been read in

if (offset

}is.close();returnbytes;

}/**

* 获取标准文件大小,如30KB,15.5MB

*

* @param file

* @return

* @throws IOException*/

public staticString getFileSize(File file)

throws IOException {long size =getFileLength(file);

DecimalFormat df= new DecimalFormat("###.##");floatf;if (size < 1024 * 1024) {

f= (float) ((float) size / (float) 1024);return (df.format(new Float(f).doubleValue()) + "KB");

}else{

f= (float) ((float) size / (float) (1024 * 1024));return (df.format(new Float(f).doubleValue()) + "MB");

}

}/**

* 复制文件

*

* @param f1

* 源文件

* @param f2

* 目标文件

* @throws Exception*/

public static voidcopyFile(File f1, File f2)

throws Exception {int length = 2097152;

FileInputStreamin = newFileInputStream(f1);

FileOutputStreamout = newFileOutputStream(f2);

FileChannel inC= in.getChannel();

FileChannel outC= out.getChannel();

ByteBuffer b= null;while (true) {if (inC.position() ==inC.size()) {

inC.close();

outC.close();

}if ((inC.size() - inC.position())

length= (int) (inC.size() -inC.position());

}elselength= 2097152;

b=ByteBuffer.allocateDirect(length);

inC.read(b);

b.flip();

outC.write(b);

outC.force(false);

}

}/**

* 检查文件是否存在

*

* @param fileName

* @return

* @throws IOException*/

public staticboolean existFile(String fileName)

throws IOException {

File file= newFile(fileName);if (!file.exists()) {throw new IOException("文件未找到:" +fileName);

}returnfile.exists();

}/**

* 删除文件

*

* @param fileName*/

public static voiddeleteFile(String fileName)

throws IOException {

File file= newFile(fileName);if (!file.exists()) {throw new IOException("文件未找到:" +fileName);

}

file.delete();

}/**

* 读取文件到字符串

*

* @param fileName

* @return

* @throws IOException*/

public staticString readFile(String fileName)

throws IOException {

File file= newFile(fileName);if (!file.exists()) {throw new IOException("文件未找到:" +fileName);

}

BufferedReaderin = new BufferedReader(newFileReader(file));

StringBuffer sb= newStringBuffer();

String str= "";while ((str = in.readLine()) != null) {

sb.append(str);

}in.close();returnsb.toString();

}/**

* 获取目录所有所有文件和文件夹

*

* @param fileName

* @return

* @throws IOException*/

public static ListlistFiles(String fileName)

throws IOException {

File file= newFile(fileName);if (!file.exists()) {throw new IOException("文件未找到:" +fileName);

}returnArrays.asList(file.listFiles());

}/**

* 创建目录

*

* @param dir*/

public static voidmkdir(String dir) {

String dirTemp=dir;

File dirPath= newFile(dirTemp);if (!dirPath.exists()) {

dirPath.mkdir();

}

}/**

* 新建文件

*

* @param fileName

* String 包含路径的文件名 如:E:\phsftp\src\123.txt

* @param content

* String 文件内容*/

public static voidcreateNewFile(String fileName, String content)

throws IOException {

String fileNameTemp=fileName;

File filePath= newFile(fileNameTemp);if (!filePath.exists()) {

filePath.createNewFile();

}

FileWriter fw= newFileWriter(filePath);

PrintWriter pw= newPrintWriter(fw);

String strContent=content;

pw.println(strContent);

pw.flush();

pw.close();

fw.close();

}/**

* 删除文件夹

*

* @param folderPath

* 文件夹路径*/

public static voiddelFolder(String folderPath) {//删除文件夹里面所有内容

delAllFile(folderPath);

String filePath=folderPath;

java.io.File myFilePath= newjava.io.File(filePath);//删除空文件夹

myFilePath.delete();

}/**

* 删除文件夹里面的所有文件

*

* @param path

* 文件夹路径*/

public static voiddelAllFile(String path) {

File file= newFile(path);if (!file.exists()) {return;

}if (!file.isDirectory()) {return;

}

String[] childFiles=file.list();

File temp= null;for (int i = 0; i < childFiles.length; i++) {//File.separator与系统有关的默认名称分隔符//在UNIX系统上,此字段的值为'/';在Microsoft Windows系统上,它为 '\'。

if(path.endsWith(File.separator)) {

temp= new File(path +childFiles[i]);

}else{

temp= new File(path + File.separator +childFiles[i]);

}if(temp.isFile()) {

temp.delete();

}if(temp.isDirectory()) {

delAllFile(path+ File.separatorChar + childFiles[i]);//先删除文件夹里面的文件

delFolder(path + File.separatorChar + childFiles[i]);//再删除空文件夹

}

}

}/**

* 复制单个文件,传统方式

*

* @param srcFile

* 包含路径的源文件 如:E:/phsftp/src/abc.txt

* @param dirDest

* 目标文件目录;若文件目录不存在则自动创建 如:E:/phsftp/dest

* @throws IOException*/

public static voidcopyFile(String srcFile, String dirDest)

throws IOException {

FileInputStreamin = newFileInputStream(srcFile);

mkdir(dirDest);

FileOutputStreamout = new FileOutputStream(dirDest + "/" + newFile(srcFile).getName());intlen;byte buffer[] = new byte[1024];while ((len = in.read(buffer)) != -1) {out.write(buffer, 0, len);

}out.flush();out.close();in.close();

}/**

* 复制文件夹

*

* @param oldPath

* String 源文件夹路径 如:E:/phsftp/src

* @param newPath

* String 目标文件夹路径 如:E:/phsftp/dest

* @return boolean*/

public static voidcopyFolder(String oldPath, String newPath)

throws IOException {//如果文件夹不存在 则新建文件夹

mkdir(newPath);

File file= newFile(oldPath);

String[] files=file.list();

File temp= null;for (int i = 0; i < files.length; i++) {if(oldPath.endsWith(File.separator)) {

temp= new File(oldPath +files[i]);

}else{

temp= new File(oldPath + File.separator +files[i]);

}if(temp.isFile()) {

FileInputStream input= newFileInputStream(temp);

FileOutputStream output= new FileOutputStream(newPath + "/"

+(temp.getName()).toString());byte[] buffer = new byte[1024 * 2];intlen;while ((len = input.read(buffer)) != -1) {

output.write(buffer,0, len);

}

output.flush();

output.close();

input.close();

}if (temp.isDirectory()) {//如果是子文件夹

copyFolder(oldPath + "/" + files[i], newPath + "/" +files[i]);

}

}

}/**

* 移动文件到指定目录

*

* @param oldPath

* 包含路径的文件名 如:E:/phsftp/src/ljq.txt

* @param newPath

* 目标文件目录 如:E:/phsftp/dest*/

public static voidmoveFile(String oldPath, String newPath)

throws IOException {

copyFile(oldPath, newPath);

deleteFile(oldPath);

}/**

* 移动文件到指定目录,不会删除文件夹

*

* @param oldPath

* 源文件目录 如:E:/phsftp/src

* @param newPath

* 目标文件目录 如:E:/phsftp/dest*/

public static voidmoveFiles(String oldPath, String newPath)

throws IOException {

copyFolder(oldPath, newPath);

delAllFile(oldPath);

}/**

* 移动文件到指定目录,会删除文件夹

*

* @param oldPath

* 源文件目录 如:E:/phsftp/src

* @param newPath

* 目标文件目录 如:E:/phsftp/dest*/

public static voidmoveFolder(String oldPath, String newPath)

throws IOException {

copyFolder(oldPath, newPath);

delFolder(oldPath);

}/**

* 解压zip文件

* 说明:本程序通过ZipOutputStream和ZipInputStream实现了zip压缩和解压功能.

* 问题:由于java.util.zip包并不支持汉字,当zip文件中有名字为中文的文件时,

* 就会出现异常:"Exception in thread "main " java.lang.IllegalArgumentException

* at java.util.zip.ZipInputStream.getUTF8String(ZipInputStream.java:285)

* @param srcDir

* 解压前存放的目录

* @param destDir

* 解压后存放的目录

* @throws Exception*/

public static voidunZip(String srcDir, String destDir)

throws IOException {int leng = 0;byte[] b = new byte[1024 * 2];/** 获取zip格式的文件 **/File[] zipFiles= new ExtensionFileFilter("zip").getFiles(srcDir);if (zipFiles != null && !"".equals(zipFiles)) {for (int i = 0; i < zipFiles.length; i++) {

File file=zipFiles[i];/** 解压的输入流 **/ZipInputStream zis= new ZipInputStream(newFileInputStream(file));

ZipEntry entry= null;while ((entry = zis.getNextEntry()) != null) {

File destFile= null;if(destDir.endsWith(File.separator)) {

destFile= new File(destDir +entry.getName());

}else{

destFile= new File(destDir + File.separator +entry.getName());

}/** 把解压包中的文件拷贝到目标目录 **/FileOutputStream fos= newFileOutputStream(destFile);while ((leng = zis.read(b)) != -1) {

fos.write(b,0, leng);

}

fos.close();

}

zis.close();

}

}

}/**

* 压缩文件

* 说明:本程序通过ZipOutputStream和ZipInputStream实现了zip压缩和解压功能.

* 问题:由于java.util.zip包并不支持汉字,当zip文件中有名字为中文的文件时,

* 就会出现异常:"Exception in thread "main " java.lang.IllegalArgumentException

* at java.util.zip.ZipInputStream.getUTF8String(ZipInputStream.java:285)

* @param srcDir

* 压缩前存放的目录

* @param destDir

* 压缩后存放的目录

* @throws Exception*/

public static voidzip(String srcDir, String destDir)

throws IOException {

String tempFileName= null;byte[] buf = new byte[1024 * 2];intlen;//获取要压缩的文件

File[] files = newFile(srcDir).listFiles();if (files != null) {for(File file : files) {if(file.isFile()) {

FileInputStream fis= newFileInputStream(file);

BufferedInputStream bis= newBufferedInputStream(fis);if(destDir.endsWith(File.separator)) {

tempFileName= destDir + file.getName() + ".zip";

}else{

tempFileName= destDir + File.separator + file.getName() + ".zip";

}

FileOutputStream fos= newFileOutputStream(tempFileName);

BufferedOutputStream bos= newBufferedOutputStream(fos);

ZipOutputStream zos= new ZipOutputStream(bos);//压缩包

ZipEntry ze= new ZipEntry(file.getName());//压缩包文件名

zos.putNextEntry(ze);//写入新的ZIP文件条目并将流定位到条目数据的开始处

while ((len = bis.read(buf)) != -1) {

zos.write(buf,0, len);

zos.flush();

}

bis.close();

zos.close();

}

}

}

}/**

* 读取数据

*

* @param inSream

* @param charsetName

* @return

* @throws Exception*/

public staticString readData(InputStream inSream, String charsetName)

throws IOException {

ByteArrayOutputStream outStream= newByteArrayOutputStream();byte[] buffer = new byte[1024];int len = -1;while ((len = inSream.read(buffer)) != -1) {

outStream.write(buffer,0, len);

}byte[] data =outStream.toByteArray();

outStream.close();

inSream.close();return newString(data, charsetName);

}public staticboolean rename(String srcPath,String destPath){//被移动的文件夹

File file = newFile(srcPath);//目标文件夹

File dir = newFile(destPath);//将文件移动到另一个文件目录下

boolean success =file.renameTo(dir);returnsuccess;

}/**

* 一行一行读取文件,适合字符读取,若读取中文字符时会出现乱码

*

* @param path

* @return

* @throws Exception*/

public static SetreadFileLine(String path)

throws IOException {

Set datas = new HashSet();

FileReader fr= newFileReader(path);

BufferedReader br= newBufferedReader(fr);

String line= null;while ((line = br.readLine()) != null) {

datas.add(line);

}

br.close();

fr.close();returndatas;

}public staticString getExtensionExclude(String filepath){int idx = filepath.lastIndexOf(".");if (idx == -1) {return "";

}else if (idx == filepath.length() - 1) {return "";

}else{return filepath.substring(0,idx);

}

}public static voidmain(String[] args) {try{

unZip("c:/test", "c:/test");

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}

}classExtensionFileFilter

implements FileFilter {privateString extension;publicExtensionFileFilter(String extension) {this.extension =extension;

}publicFile[] getFiles(String srcDir) throws IOException {return(File[]) FileUtils.listFiles(srcDir).toArray();

}publicboolean accept(File file) {if(file.isDirectory()) {return false;

}

String name=file.getName();//find the last

int idx = name.lastIndexOf(".");if (idx == -1) {return false;

}else if (idx == name.length() - 1) {return false;

}else{return this.extension.equals(name.substring(idx + 1));

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值