手机
当前位置:查字典教程网 >编程开发 >Java >使用jdk7的nio2操作文件拷贝和剪切示例
使用jdk7的nio2操作文件拷贝和剪切示例
摘要:复制代码代码如下:packagecom.xyq.io.simply.core;importjava.io.File;importjava.i...

复制代码 代码如下:

package com.xyq.io.simply.core;

import java.io.File;

import java.io.IOException;

import java.nio.file.FileVisitResult;

import java.nio.file.FileVisitor;

import java.nio.file.Files;

import java.nio.file.Path;

import java.nio.file.Paths;

import java.nio.file.StandardCopyOption;

import java.nio.file.attribute.BasicFileAttributes;

import java.util.ArrayList;

import java.util.List;

import com.xyq.io.enums.FileTypeMode;

import com.xyq.io.enums.OptionFile_TYPE;

import com.xyq.io.inf.NewNIoInf;

import com.xyq.io.util.FindFileUtil;

import com.xyq.io.util.MD5Util;

public class NewNIO implements NewNIoInf {

/***

* 拷贝或者移动文件

*/

@Override

public boolean copeOrMoveFile(String src, String tar, OptionFile_TYPE type) {

return realCopeOrMoveFile(Paths.get(src), tar, type);

}

private boolean realCopeOrMoveFile(Path srcPath, String tar,

OptionFile_TYPE type) {

Path tarPath = null;

boolean copeSuccess = true;

// 必须原始文件存在

if (srcPath.toFile().exists()) {

/***

* 如果原始路径是带斜杠的那么就认为这是一个文件夹

*/

if (isDir(tar))

tarPath = Paths.get(tar + File.separator

+ srcPath.toFile().getName());

else

tarPath = Paths.get(tar);

/***

* 然后进行N次(可以作为参数)拷贝操作(出错重连),是否覆盖拷贝,拷贝属性,拷贝操作不能使用回滚选项

*/

for (int i = 0; i < 3; i++) {

/***

* 如果是目标文件已经存在

*/

if (tarPath.toFile().exists()) {

/***

* 如果验证两个文件夹是相同的,拷贝选项下就不用拷贝,移动选项下就是删除原始文件

*/

// 拷贝

if (OptionFile_TYPE.COPE.equals(type)) {

if (equalsFile(srcPath.toFile(), tarPath.toFile()))

return true;

else

copeSuccess = copeFile(srcPath, tarPath, true);

}

/***

* 移动操作,这里得非常小心,正常情况下,如果两个文件是一样的话,

* 那么直接删除原始文件就可以了。但是,如果两个文件的一样,并且地址也

* 是一样的话,那么就不能删除原始的了,因为就是同一个文件,不能删除的。

*/

else if (OptionFile_TYPE.MOVE.equals(type)) {

if (equalsFile(srcPath.toFile(), tarPath.toFile())) {

if (!srcPath.toFile().getAbsoluteFile()

.equals(tarPath.toFile().getAbsoluteFile()))

try {

Files.delete(srcPath);

/***

* 之所以要手动指向true,是因为可能存在前面删除失败的情况

*/

if (!copeSuccess)

copeSuccess = true;

} catch (IOException e) {

copeSuccess = false;

}

// 前面因为有异常的可能就不直接return,这里就可以了

else

return true;

} else

copeSuccess = moveFile(srcPath, tarPath);

}

}

/***

* 当目标文件不存在的时候,先判断父类文件夹是可创 建(父类文件夹存在或者可以创建),可创建时就创建

*/

else {

File par = tarPath.getParent().toFile();

/***

* 如果父类文件夹不存在并且无法创建,那么就不用拷贝了

*/

if (!par.exists() && !par.mkdirs())

copeSuccess = false;

else if (OptionFile_TYPE.COPE.equals(type))

copeSuccess = copeFile(srcPath, tarPath, false);

else if (OptionFile_TYPE.MOVE.equals(type))

copeSuccess = moveFile(srcPath, tarPath);

}

// 如果操作成功,跳出循环

if (copeSuccess)

break;

}

} else

copeSuccess = false;

return copeSuccess;

}

/****

* 拷贝文件

*/

private boolean copeFile(Path srcPath, Path tarPath, boolean isExist) {

if (isExist)

try {

Files.copy(srcPath, tarPath,

StandardCopyOption.REPLACE_EXISTING,

StandardCopyOption.COPY_ATTRIBUTES);

} catch (IOException e) {

return false;

}

else

try {

Files.copy(srcPath, tarPath, StandardCopyOption.COPY_ATTRIBUTES);

} catch (IOException e) {

return false;

}

return true;

}

/***

* 移动文件,不能使用属性选项

*

* @param srcPath

* @param tarPath

* @return

*/

private boolean moveFile(Path srcPath, Path tarPath) {

try {

Files.move(srcPath, tarPath, StandardCopyOption.ATOMIC_MOVE);

} catch (IOException e) {

return false;

}

return true;

}

/***

* 判断path路径是否是一个文件夹

*

* @param path

* @return

*/

private boolean isDir(String path) {

char lastC = path.charAt(path.length() - 1);

if (lastC == '' || lastC == '/')

return true;

return false;

}

/***

* 这是来验证两个文件是否相同,只是简单验证,可以强制必须使用md5进行验证

*/

public boolean equalsFile(File src, File tar) {

// 如果两个文件的长度不一样,那么肯定两个文件是不一样的

if (src.length() != tar.length())

return false;

if (!src.getName().equals(tar.getName())

|| src.lastModified() != tar.lastModified())

return MD5Util.EncoderFileByMd5(src).equals(

MD5Util.EncoderFileByMd5(tar));

return true;

}

/***

* 拷贝或者移动文件夹

*/

@Override

public void copeOrMoveDirectory(String src, final String tar, int tierSize,

final OptionFile_TYPE type) {

if (!new File(src).exists())

throw new RuntimeException("找不到原始文件夹" + src);

final int rootPos = getRootPosition(new File(src), tierSize);

if (rootPos != -1) {

try {

Files.walkFileTree(Paths.get(src), new FileVisitor<Path>() {

String tarDirString = null;

/***

* 到达文件夹前,先把目标路径写好

*

* @param dir

* @param attrs

* @return

* @throws IOException

*/

@Override

public FileVisitResult preVisitDirectory(Path dir,

BasicFileAttributes attrs) throws IOException {

tarDirString = dir.toFile().getAbsolutePath();

tarDirString = tar + tarDirString.substring(rootPos)

+ File.separator;

return FileVisitResult.CONTINUE;

}

/***

* 到达文件之后,进行拷贝或者移动操作

*

* @param file

* @param attrs

* @return

* @throws IOException

*/

@Override

public FileVisitResult visitFile(Path file,

BasicFileAttributes attrs) throws IOException {

File f = file.toFile();

if (f.exists() && f.canRead() && !f.isHidden())

realCopeOrMoveFile(file, tarDirString, type);

return FileVisitResult.CONTINUE;

}

@Override

public FileVisitResult visitFileFailed(Path file,

IOException exc) throws IOException {

return FileVisitResult.CONTINUE;

}

/***

* 到达文件夹后

*

* @param dir

* @param exc

* @return

* @throws IOException

*/

@Override

public FileVisitResult postVisitDirectory(Path dir,

IOException exc) throws IOException {

return FileVisitResult.CONTINUE;

}

});

} catch (Exception e) {

e.printStackTrace();

}

// 如果是剪切操作,并且剪切成功,那么就要删除所有文件夹

if (OptionFile_TYPE.MOVE.equals(type) && isBlockDir(src))

delDir(src);

} else

throw new RuntimeException("指定父类文件夹层次错误~~~");

}

/***

* 根据指定层次获取指定盘符的位置

*/

private int getRootPosition(File file, int tier) {

if (file != null) {

String path = file.getAbsolutePath();

int cc = 0;

for (int i = path.length() - 1; i >= 0; i--) {

if (path.charAt(i) == '') {

cc++;

if (cc == tier + 1) {

cc = i;

return cc;

}

}

}

}

return -1;

}

/***

* 查看该文件夹下是否还有文件

*

* @param dirPath

* @return

*/

private boolean isBlockDir(String dirPath) {

File dir = new File(dirPath);

File[] dirList = dir.listFiles();

if (dirList == null || dirList.length == 0)

return true;

else {

// 寻找文件

for (File f : dirList)

if (!f.isDirectory())

return false;

}

return true;

}

/***

* 删除空文件夹

*

* @param dirPath

*/

private void delDir(String dirPath) {

File dir = new File(dirPath);

File[] dirList = dir.listFiles();

if (dirList == null || dirList.length == 0)

dir.delete();

else {

// 删除所有文件

for (File f : dirList)

if (f.isDirectory())

delDir(f.getAbsolutePath());

else

f.delete();

// 删除完当前文件夹下所有文件后删除文件夹

dirList = dir.listFiles();

if (dirList.length == 0)

dir.delete();

}

}

/***

* 根据文件类型查找相关的文件

*/

@Override

public List<String> findFilesByType(String dir, String[] keys,

boolean isMatchCase) throws IOException {

List<String> list = new ArrayList<String>();

Files.walkFileTree(Paths.get(dir), new FindFileUtil(keys, isMatchCase,

list, FileTypeMode.TYPES));

return list;

}

/***

* 根据文件名称查找相关的文件

*/

@Override

public List<String> findFilesByName(String dir, String[] keys,

boolean isMatchCase) throws IOException {

List<String> list = new ArrayList<String>();

Files.walkFileTree(Paths.get(dir), new FindFileUtil(keys, isMatchCase,

list, FileTypeMode.NAMES));

return list;

}

public static void main(String[] args) throws IOException {

NewNIoInf inf = new NewNIO();

inf.copeOrMoveFile("e:/cc/dd/11.txt", "e:/XX/xxx/zzz/",

OptionFile_TYPE.COPE);

inf.copeOrMoveDirectory("e:BBCCDD", "e:",1, OptionFile_TYPE.MOVE);

System.out.println(inf.findFilesByName("D:workspace", new String[] { "txt" },

false).size());

}

}

---------------------------

package com.xyq.io.enums;

/***

* 文件类型

* @author xyq

*

*/

public enum FileTypeMode {

TYPES,NAMES

}

---------------------------------

package com.xyq.io.enums;

/***

* 操作文件的类型

* @author xyq

*

*/

public enum OptionFile_TYPE {

COPE,MOVE;

}

---------------------

package com.xyq.io.inf;

import java.io.IOException;

import java.util.List;

import com.xyq.io.enums.OptionFile_TYPE;

public interface NewNIoInf {

/***

* 拷贝或者移动文件

* @param src

* @param tar

* @return

*/

public boolean copeOrMoveFile(String src,String tar,OptionFile_TYPE type);

/***

* 拷贝或者移动文件夹

* @param src

* @param tar

* @param tierSize 层次,拷贝完成后的路径0只是当前文件夹,+1就是加一级父类文件夹(但不拷贝父类内容)

* @param type

*/

public void copeOrMoveDirectory(String src,String tar,int tierSize,OptionFile_TYPE type);

/**

* 根据文件类型查找相关文件集合,多种类型时用逗号隔开

*

* @param dir

* 目录

* @param keys

* 文件类型

* @param isMatchCase

* 是否区分大小写

* @return

* @throws IOException

*/

List<String> findFilesByType(String dir, String[] keys, boolean isMatchCase)

throws IOException;

/**

* 根据文件名称查找相关文件集合,多种民称时用逗号隔开

*

* @param dir

* 目录

* @param keys

* 文件名称

* @param isMatchCase

* 是否区分大小写

* @return

* @throws IOException

*/

List<String> findFilesByName(String dir, String[] keys, boolean isMatchCase)

throws IOException;

}

--------------------

package com.xyq.io.util;

import java.io.File;

import java.io.IOException;

import java.nio.file.FileVisitResult;

import java.nio.file.Path;

import java.nio.file.SimpleFileVisitor;

import java.nio.file.attribute.BasicFileAttributes;

import java.util.List;

import com.xyq.io.enums.FileTypeMode;

public class FindFileUtil extends SimpleFileVisitor<Path> {

/***

* 关键词列表,是否转换大小写,返回结果集

*/

private String[] keyArray = null;

private boolean isMatchCase;

private List<String> resultList;

private FileTypeMode mode;

public FindFileUtil(String[] keyArray, boolean isMatchCase,

List<String> resultList, FileTypeMode mode) {

this.keyArray = keyArray;

this.isMatchCase = isMatchCase;

this.resultList = resultList;

this.mode = mode;

}

@SuppressWarnings("unused")

private FindFileUtil() {

}

@Override

public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)

throws IOException {

File f = file.toFile();

if (f.exists() && f.canRead() && !f.isHidden())

if (this.keyArray != null) {

for (String key : this.keyArray) {

if (!this.isMatchCase)

key = key.toLowerCase();

if (matchFile(file, this.mode, key, isMatchCase))

resultList.add(file.toString());

}

}

return FileVisitResult.CONTINUE;

}

/***

* 根据大小写和类型或名称进行文件匹配

*

* @param file

* @param mode

* @param key

* @param isMatchCase

* @return

*/

private boolean matchFile(Path file, FileTypeMode mode, String key,

boolean isMatchCase) {

File f = file.toFile();

if (f.exists() && f.canRead() && !f.isHidden()

&& !"System Volume Information".equals(f.getName())) {

String fileName = null;

if (FileTypeMode.TYPES.equals(mode)) {

fileName = file.toString();

return isMatchCase ? fileName.endsWith(key) : fileName

.toLowerCase().endsWith(key);

} else if (FileTypeMode.NAMES.equals(mode)) {

fileName = file.toFile().getName();

return isMatchCase ? (fileName.indexOf(key) == -1 ? false

: true)

: (fileName.toLowerCase().indexOf(key) == -1 ? false

: true);

}

}

return false;

}

@Override

public FileVisitResult visitFileFailed(Path file, IOException exc)

throws IOException {

//如果错误信息中包含X:System Volume Information,这是表示系统的隐藏盘,是不能读的

System.out.println(exc.getMessage());

return FileVisitResult.CONTINUE;

}

}

--------------------------

package com.xyq.io.util;

import java.io.Closeable;

public class CloseIoUtil {

/***

* 关闭IO流

*

* @param cls

*/

public static void closeAll(Closeable... cls) {

if (cls != null) {

for (Closeable cl : cls) {

try {

if (cl != null)

cl.close();

} catch (Exception e) {

} finally {

cl = null;

}

}

}

}

}

-----------------------------

package com.xyq.io.util;

import java.io.BufferedInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

import sun.misc.BASE64Encoder;

public class MD5Util {

/***

* 加密字符串

*

* @param str

* @return

* @throws NoSuchAlgorithmException

* @throws UnsupportedEncodingException

*/

public static String EncoderStringByMd5(String str)

throws NoSuchAlgorithmException, UnsupportedEncodingException {

// 确定计算方法

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

BASE64Encoder base64en = new BASE64Encoder();

// 加密后的字符串

String newstr = base64en.encode(md5.digest(str.getBytes("utf-8")));

return newstr;

}

/***

* 加密文件

*

* @param file

* @return

* @throws NoSuchAlgorithmException

* @throws IOException

*/

public static String EncoderFileByMd5(File file) {

String newstr = null;

FileInputStream fis = null;

BufferedInputStream bis = null;

try {

// 确定计算方法

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

BASE64Encoder base64en = new BASE64Encoder();

byte[] buffer = new byte[1024];

fis = new FileInputStream(file);

bis = new BufferedInputStream(fis);

int length = -1;

while ((length = bis.read(buffer)) != -1)

md5.update(buffer, 0, length);

// 加密后的字符串

newstr = base64en.encode(md5.digest());

} catch (Exception e) {

e.printStackTrace();

} finally {

CloseIoUtil.closeAll(bis, fis);

}

return newstr;

}

public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException {

System.out.println(EncoderStringByMd5("23"));

}

}

【使用jdk7的nio2操作文件拷贝和剪切示例】相关文章:

Java生成PDF文件的实例代码

java使用dom4j解析xml配置文件实现抽象工厂反射示例

使用java实现http多线程断点下载文件(二)

Java中如何调用cmd压缩文件

基于JavaCore文件的深入分析

java使用dom4j操作xml示例代码

使用JAVA判断凸多边形的示例代码

java nio基础使用示例

java调用百度定位api服务获取地理位置示例

使用Log4j为项目配置日志输出应用详解以及示例演示的实现分析

精品推荐
分类导航