Java基础之访问文件与目录——移动或复制文件和目录(MoveAndCopyFiles)
控制台程序,创建和删除目录以及复制和移动文件。
import java.nio.file.*;
import java.nio.file.attribute.*;
import java.io.IOException; public class MoveAndCopyFiles {
static void createSingleDirectory(Path path){
try{
Files.createDirectories(path);
System.out.println("\n" + path + " directory created.");
} catch (IOException e) {
System.out.println("\nDirectory creation failed:\n" + e);
}
} static boolean isDirectory(Path path) {
try{
BasicFileAttributes attr =Files.readAttributes(path,BasicFileAttributes.class);
return attr.isDirectory();
} catch (IOException e) {
System.err.println("I/O error in isDirectory method. " + e);
}
return false;
} static boolean copyFiles(Path from, Path to) {
if(!isDirectory(from)) {
System.out.println("Cannot copy files. " + from + " is not a directory.");
return false;
}
if(!isDirectory(to)) {
System.out.println("Cannot copy files. " + to + " is not a directory.");
return false;
} try (DirectoryStream<Path> files =Files.newDirectoryStream(from, "*.*")) {
System.out.println("Starting copy...");
for(Path file : files) {
Files.copy(file, to.resolve(file.getFileName()));
System.out.println(" " + file.getFileName() + " copied.");
}
} catch (IOException e) {
System.err.println("I/O error in copyFiles. " + e);
return false;
}
return true;
} static boolean moveFiles(Path from, Path to) {
if(!isDirectory(from)) {
System.out.println("Cannot move files. " + from + " is not a directory.");
return false;
}
if(!isDirectory(to)) {
System.out.println("Cannot move files. " + to + " is not a directory.");
return false;
} try (DirectoryStream<Path> files =Files.newDirectoryStream(from, "*.*")) {
System.out.println("Starting move...");
for(Path file : files) {
Files.move(file, to.resolve(file.getFileName()));
System.out.println(" " + file.getFileName() + " moved.");
}
} catch (IOException e) {
System.err.println("I/O error in copyFiles. " + e);
return false;
}
return true;
} static void waitForEnter() {
try {
System.out.println("waiting...");
System.in.read();
}catch(IOException e) {
System.err.println(e);
}
} public static void main(String[] args) {
Path current =Paths.get("E:/JavaProject/BeginningJava/Ch9_Directories/MoveAndCopyFiles");
Path newDir =Paths.get("junkDir");
newDir=newDir.toAbsolutePath();
createSingleDirectory(newDir); System.out.println("Copying files from " + current + " to " + newDir);
if(!copyFiles(current,newDir)) {
System.out.println("Copying files failed.");
return;
}
System.out.println("You can look at the directory to verify that the copy has worked.");
System.out.println("Press Enter to continue.");
waitForEnter(); Path newDir2 =Paths.get("junkDirBackup");
newDir2=newDir2.toAbsolutePath();
createSingleDirectory(newDir2); System.out.println("Moving files from " + newDir + " to " + newDir2);
if(!moveFiles(newDir,newDir2)) {
System.out.println("Moving files failed.");
return;
} System.out.println("You can look at the directory to verify that the move has worked.");
System.out.println("Press Enter to continue.");
waitForEnter(); try {
System.out.println("Deleting " + newDir + "...");
Files.delete(newDir);
}catch(IOException e) {
System.err.println("Deleting " + newDir + " failed:\n" + e);
} try (DirectoryStream<Path> files =Files.newDirectoryStream(newDir2, "*.*")) {
System.out.println("Deleting files from " + newDir2 + "...");
for(Path file:files) {
Files.delete(file);
System.out.println(" " + file.getFileName() + " deleted.");
}
System.out.println("Deleting " + newDir2 + "...");
Files.delete(newDir2);
} catch (IOException e) {
System.err.println("I/O error deleting files. " + e);
}
}
}
Java基础之访问文件与目录——移动或复制文件和目录(MoveAndCopyFiles)的更多相关文章
- Java基础——protected访问修饰符探讨
Java基础——protected访问修饰符探讨 根据官方说法:(如图) protected修饰符是可以修饰其他包中的子孙类的,但是我做了个实验,结果发现了一个有趣的现象! 具体请往下看: packa ...
- Java基础之访问权限控制
Java基础之访问权限控制 四种访问权限 Java中类与成员的访问权限共有四种,其中三种有访问权限修饰词:public,protected,private. Public:权限最大,允许所有类访问,但 ...
- Web 在线文件管理器学习笔记与总结(13)重命名文件夹(14)复制文件夹
(13)重命名文件夹 ① 重命名文件夹通过 rename($oldname,$newname) 实现 ② 检测文件夹名是否符合规范 ③ 检测当前目录中是否存在同名文件夹名称,如果不存在则重命名成功 i ...
- c# 封装的文件夹操作类之复制文件夹
c# 封装的文件夹操作类之复制文件夹 一.复制文件夹原理: 1.递归遍历文件夹 2.复制文件 二.FolderHelper.cs /// <summary> /// 文件夹操作类 /// ...
- 用winscp从本地上传文件到服务器上出现复制文件到远端时错误。
用winscp从本地上传文件到服务器上出现复制文件到远端时错误. 错误码:4 服务器返回的错误消息:write failed 报错如下图所示: 分析过程: 1.刚开始以为是权限不够,后面上网查了一下是 ...
- IO流,字节流复制文件,字符流+缓冲复制文件
JAVAIO如果按流向分:输入流和输出流两种 输入流的基类:InputStream Reader 输出流的基类:OutputStream Writer 如果按数据单元划分:字节流和字符流 字节 ...
- java基础知识回顾之javaIO类--File类应用:获取指定目录下面的指定扩展名的文件,将文件的绝对路径写入到目的文件当中
/** * File文件综合应用 * 需求:获取指定目录下面,指定扩展名的文件,将文件的绝对路径写到文本文件当中. * * 思路:1.需要深度遍历.--递归 * 2.遍历的过程中过滤指定扩展名的文件 ...
- RandomAccessFile(),读写文件数据的API,以及复制文件操作
package seday03;import java.io.File;import java.io.RandomAccessFile; import java.io.IOException; /** ...
- UNIX环境编程学习笔记(4)——文件I/O之dup复制文件描述符
lienhua342014-08-23 UNIX 提供了两个函数 dup 和 dup2 用于复制一个现存的文件描述符. #include <unistd.h> int dup(int fi ...
- Java基础之访问文件与目录——测试文件或目录的路径(TryPath)
控制台程序,测试文件或目录的路径. import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.FileSy ...
随机推荐
- pycharm使用笔记
Basic code completion (the name of any class, method or variable) control + 空格 # 代码补全,如果跟系统spotligh ...
- Windows下编译objective-C
Windows下编译objective-C 2011-08-31 14:32 630人阅读 评论(0) 收藏 举报 windowscocoa工具objective clibraryxcode 目录 ...
- Matplotlib for Python Developers
这个教程也很不错,http://reverland.org/python/2012/09/07/matplotlib-tutorial/ 也可以参考官网的Gallery,http://matplotl ...
- 用c++builder读取一个一行有多行变量的文件
文件内容如下: C DXDY.INP FILE, IN FREE FORMAT ACROSS COLUMNS for 83658 Active CellsC 2013-5-25 上午 10:43 ...
- 取url的键值对,location的search:从?开始的字符串
function urlArgs(){ var args=""; var query=location.search.substring(1);//去除问号 var pairs=q ...
- QProgressBar的使用例子
今天下午动手实践了一下QProgressBar,遇到的问题比较多,浪费了不少时间,但收获同样颇多... 程序界面如下: 1 // progressbar.h 2 3 #ifndef PROGR ...
- insert into hi_user_score set hello_id=74372073,a=10001 on duplicate key update hello_id=74372073, a=10001
insert into hi_user_score set hello_id=74372073,a=10001 on duplicate key update hello_id=74372073, a ...
- How to control printer orientation(Landscape / Portrait) for an AX report in X++
You should try this: 1. Set property Orientation on your report design to Auto 2. In your fetch meth ...
- Android调用WCF
http://www.cnblogs.com/davidgu/archive/2012/05/16/2504182.html
- Swift-04-Designated&&Convenience
class ClassA { let numA:Int init(num: Int){ numA = num } } class ClassB: ClassA { let numB:Int overr ...