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

java文件相关操作,比较。

2013-05-13 15:11 309 查看
package com.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;

public class FileTest {

public FileTest() {

}

protected static MessageDigest messagedigest = null;
static {
try {
messagedigest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {

}
}

public static void main(String[] args) throws Exception {
// newFile();
// readFile();
//		long c1 = System.currentTimeMillis();
//		compareFile();
//		long c2 = System.currentTimeMillis();
//		System.out.println(c2 - c1 + "daddd");
long a = System.currentTimeMillis();
System.out.println(getHash("E://bsoamyeclipse2.rar", "md5"));
long b = System.currentTimeMillis();
System.out.println(b - a);
System.out.println(getJarHash());
long c = System.currentTimeMillis();
System.out.println(c - b);
System.out.println(getHash1("E://bsoamyeclipse2.rar", "md5"));
long d = System.currentTimeMillis();
System.out.println(d-c);
}

/**
* 创建文件
*/
public static void newFile() {
File myFile = new File("1.txt");
if (!myFile.exists()) {
myFile.mkdirs();
}
System.out.println(myFile.getName());// 取得文件名称的方法
System.out.println(myFile.getPath());// 取得文件路径的方法
System.out.println(myFile.isAbsolute());// 判断文件是否完整
System.out.println(myFile.getParent());// 取得文件的根目录
System.out.println(myFile.exists());// 判断文件是否存在
System.out.println(myFile.isDirectory());// 判断是否是目录
System.out.println(myFile.isFile());// 判断是否是文件
System.out.println(myFile.isHidden());// 判断是否是隐藏文件
System.out.println(myFile.canRead());// 判断是否可读
System.out.println(myFile.canWrite());// 判断是否可写
}

/**
* 获取文件最后修改时间,大小,效率控制在1毫秒之内。。
*/
public static void compareFile() {
String path1 = "E://Test1//Test1//test1.txt";
String path2 = "E://Test2//Test1//test1.txt";
String path3 = "E://bsoamyeclipse.rar";
File file1 = new File(path1);
File file2 = new File(path2);
File file3 = new File(path3);
if (file1.lastModified() == file2.lastModified()) {
System.out.println("11");
}
long l1 = file1.length();
long l2 = file2.length();
long l3 = file3.length();
System.out.println(l1);
System.out.println(l2);
System.out.println(l3);
}

/**
* 迭代文件
*/
public static void readFile() {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String path = "E://Test1";
File myFile = new File(path);
// for (String s : myFile.list()) {// 读取某个目录下所有文件
// System.out.println(s);
// }
for (int i = 0; i < scanFile(myFile).size(); i++) {
File file = (File) scanFile(myFile).get(i);
System.out.println(file.getName());
}
// System.out.println(sf.format(new Date(myFile.lastModified())));//
// 最后一次编辑时间
}

/**
* 遍历文件夹属性
*
* @param root
* @return
*/
public static List<File> scanFile(File root) {
List<File> fileInfo = new ArrayList<File>();
File[] files = root.listFiles();
for (File file : files) {// 逐个遍历文件
if (file.isDirectory()) { // 如果是文件夹,则进一步遍历,递归调用
List<File> ff = scanFile(file);
fileInfo.addAll(ff);
} else {
fileInfo.add(file);
}
}
return fileInfo;
}

/**
* java自带的MD5加密算法,io,效率为300M的文件,耗时17562,坑爹啊。。
*
* @param fileName
* @param hashType
* @return
* @throws Exception
*/
public static String getHash(String fileName, String hashType)
throws Exception {
InputStream fis = new FileInputStream(fileName);
byte[] buffer = new byte[8 * 1024];
MessageDigest md5 = MessageDigest.getInstance(hashType);
int numRead = 0;
while ((numRead = fis.read(buffer)) > 0) {
md5.update(buffer, 0, numRead);
}
fis.close();
return toHexString(md5.digest());
}

/**
* nio.
* @param fileName
* @param hashType
* @return
*/
public static String getHash1(String fileName, String hashType){
FileInputStream fis = null;
String s = null;
try {
fis = new FileInputStream(fileName);
FileChannel fChannel = fis.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(8 * 1024);
MessageDigest md5 = MessageDigest.getInstance(hashType);
for (int count = fChannel.read(buffer); count != -1; count = fChannel
.read(buffer)) {
buffer.flip();
md5.update(buffer);
if (!buffer.hasRemaining()) {
buffer.clear();
}
}
s = toHexString(md5.digest());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if( fis!=null )
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return s;
}

public static String toHexString(byte[] b) {
char[] hexChar = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f' };
StringBuilder sb = new StringBuilder(b.length * 2);
for (int i = 0; i < b.length; i++) {
sb.append(hexChar[(b[i] & 0xf0) >>> 4]);
sb.append(hexChar[b[i] & 0x0f]);
}
return sb.toString();
}

/**
* commons-codec-1.4.jar带的MD5加密方法,效率为300M的文件,耗时1654
*
* @return
* @throws FileNotFoundException
* @throws IOException
*/
public static String getJarHash() throws FileNotFoundException, IOException {
return DigestUtils.md5Hex(new FileInputStream(new File(
"E://bsoamyeclipse2.rar")));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Java MD5