package cn.javabs.iocase;
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author Mryang
*
*/
public class DocumentManager {
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in);
System.out.println("---1:指定关键字检索文件 2:指定后缀名检索文件 3:赋值文件/目录 4:退出----");
while (true) {
System.out.println("请输入命令:");
int command = scan.nextInt();
switch (command) {
case 1:
searchByKeyWorld();
break;
case 2:
searchBySuffix();
break;
case 3:
copyDirectory();
case 4:
exit();
break;
default:
System.out.println("您输入的指令错误!");
break;
}
}
}
/**
* 指定后缀名检索文件
*/
private static void searchBySuffix() {
Scanner scan = new Scanner(System.in);
System.out.print("请输入您要检索的目录的位置");
String path = scan.next();
File file = new File(path);
if (!file.exists() || !file.isDirectory()) {
System.out.println(path + "(不是有效目录)");
return;
}
System.out.println("您输入搜索后缀:");
String suffix = scan.next();
String[] suffixArray = suffix.split(",");
ArrayList<String> list = FileUtils.listFiles(file, suffixArray);
for (Object obj : list) {
System.out.println(obj);
}
}
/**
* 退出
*/
private static void exit() {
System.out.println("您已退出系统,谢谢使用");
System.exit(0);
}
/**
* 复制文件/目录
*
* @throws Exception
*/
private static void copyDirectory() throws Exception {
Scanner scan = new Scanner(System.in);
System.out.print("请输入源目录");
String srcDirectory = scan.next();
File srcFile = new File(srcDirectory);
if (!srcFile.exists() || !srcFile.isDirectory()) {
System.out.println("无效目录!");
return;
}
System.out.print("请输入目标位置:");
String destDirectory = scan.next();
File destFile = new File(destDirectory);
if (!destFile.exists() || !destFile.isDirectory()) {
System.out.println("无效位置!");
return;
}
FileUtils.copySrcPathToDestPath(srcFile, destFile);
}
/**
* 指定关键字检索文件
*/
private static void searchByKeyWorld() {
Scanner scan = new Scanner(System.in);
System.out.print("请输入您要检索的目录的位置");
String path = scan.next();
File file = new File(path);
if (!file.exists() || !file.isDirectory()) {
System.out.println(path + "(不是有效目录)");
return;
}
System.out.println("您输入搜索关键字:");
String key = scan.next();
ArrayList<String> list = FileUtils.listFiles(file, key);
for (Object obj : list) {
System.out.println(obj);
}
}
}
package cn.javabs.iocase;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.util.ArrayList;
public class FileUtils {
public static ArrayList<String> listFiles(File file,
final String[] suffixArray) {
FilenameFilter filter = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
File currFile = new File(dir, name);
if (currFile.isFile()) {
for (String suffix : suffixArray) {
if (name.endsWith("." + suffix)) {
return true;
}
}
}
return false;
}
};
ArrayList<String> arraylist = fileDir(file, filter);
return arraylist;
}
/**
* 复制文件/目录
*
* @param srcFile
* @param desFile
* @throws Exception
*/
public static void copySrcPathToDestPath(File srcFile, File destFile)
throws Exception {
File[] files = srcFile.listFiles();
for (int i = 0; i < files.length; i++) {
File copiedFile = new File(destFile, files[i].getName());
if (files[i].isDirectory()) {
if (!copiedFile.mkdirs()) {
System.out.println("无法创建 + copiedFile");
return;
}
copySrcPathToDestPath(files[i], copiedFile);
} else {
FileInputStream input = new FileInputStream(files[i]);
FileOutputStream output = new FileOutputStream(copiedFile);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = input.read(buffer)) != -1) {
output.write(buffer, 0, len);
}
input.close();
output.close();
}
}
}
/**
* 指定关键字检索文件
*
* @param file
* @param key
* @return
*/
public static ArrayList<String> listFiles(File file, final String key) {
FilenameFilter filter = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
File currFile = new File(dir, name);
if (currFile.isFile() && name.contains(key)) {
return true;
}
return false;
}
};
// 递归当时获取规定的路径
ArrayList<String> arraylist = fileDir(file, filter);
return arraylist;
}
/**
*
* @param dir
* @param filter
* @return
*/
private static ArrayList<String> fileDir(File dir, FilenameFilter filter) {
ArrayList<String> arraylist = new ArrayList<String>();
File[] lists = dir.listFiles(filter);
for (File list : lists) {
arraylist.add(list.getAbsolutePath());
}
File[] files = dir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
ArrayList<String> every = fileDir(file, filter);
arraylist.addAll(every);
}
}
return arraylist;
}
}
作者: 杨校
出处: https://www.cnblogs.com/xiaoxiao5016/
分享是快乐的,也见证了个人成长历程,文章大多都是工作经验总结以及平时学习积累,基于自身认知不足之处在所难免,也请大家指正,共同进步。
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 如有问题, 可邮件(397583050@qq.com)咨询。
杨老师课堂_Java核心技术下之控制台模拟文件管理器案例
标签:cas 列表 模拟 展现 man 邮件 contains frame rri
原文地址:https://www.cnblogs.com/xiaoxiao5016/p/9052764.html