目录
前言
日常生活中,经常会遇到需要批量操作系统文件的情况,通常,只能手动重复操作,这样既费时有费力。本案例要求编写一个文件管理器,实现文件的批量操作。
一、具体实现的功能
1、指定关键字搜索文件——用户输入检索的目录和关键字,系统在用户的指定目录下检索出文件名中包含关键字的文件,并将绝对路径展示出来。
2、指定后缀名搜索文件——用户输入检索的目录和后缀名,系统在指定的目录下检索出指定的后缀名文件,并将其绝对路径展现出来。
3、删除文件——输入需要删除的文件目录,程序执行后会将目录以及目录下的内容全部删除。
4、复制文件。
二、
全部代码
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;
public class 批量操作文件管理器 {
//
static Scanner s =new Scanner(System.in);
static String filePath="";//定义静态变量filePath文件路径
static String message="";//检索文件的信息用message存储
static File f=null;
static int command;
public static void main(String[] args) {
//指令
System.out.println("--1:指定文件关键字搜索--2:后缀名查询--3:删除文件--4:复制文件--5:退出");
while(true) {
System.out.println("请输入指令数字:");
command=s.nextInt();
//try语句捕捉错误
try{
switch(command) {
case 1:
getmessage();
keySearch(f,message);break;
case 2:
getmessage();
endSearch(f,message);break;
case 3:
System.out.println("请输入查询文件的根目录:");
filePath = s.next();
deleteFile(new File(filePath));break;
case 4://复制文件
CopyFile();
System.out.println("复制完成");break;
case 5:
s.close();
System.out.println("程序结束");System.exit(0);
}
}catch(InputMismatchException e) {
e.getStackTrace();
}catch(IllegalArgumentException e) {
e.getStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//指定关键字搜索
private static void keySearch(File f,String message) throws InputMismatchException{
File[] dir= f.listFiles();
for(int i =0; i<dir.length;i++)
{
if(dir[i].isDirectory()) {
keySearch(dir[i],message);//是文件夹的话,就利用迭代在此寻找
}else {
if(dir[i].isFile()) {//如果是文件,判断文件名字是否和关键字相同
if(dir[i].getName().contains(message)) {//contains方法
System.out.println(dir[i].getAbsolutePath());
}
}else {
System.out.println("未能找到文件");
}
}
}
}
//后缀名搜索
public static void endSearch(File f,String s) {
File[] dir= f.listFiles();
for(int i =0; i<dir.length;i++)
{
if(dir[i].isDirectory()) {
endSearch(dir[i],message);
}else {
if(dir[i].isFile()) {
if(dir[i].getPath().toLowerCase().endsWith(message)) {
System.out.println(dir[i].getAbsolutePath());
}
}else {
System.out.println("未能找到文件");
}
}
}
}
//删除文件
public static void deleteFile(File f) {
File[] dir=f.listFiles();
for(int i =0;i<dir.length;i++) {
if(dir[i].isDirectory()) {
deleteFile(dir[i]);
dir[i].delete();
}else {
if(dir[i].isFile()) {
dir[i].delete();
}
}
}
}
//为了减少代码重复性,创建了个获取信息的函数getmessage
public static void getmessage() {
filePath="";
message="";//初始化
System.out.println("请输入查询文件的根目录:");
filePath = s.next();
if(command==1){
System.out.println("请输入查询文件的关键字:");
}
if(command==2){
System.out.println("请输入查询文件的后缀名:");
}
message = s.next();
f = new File(filePath);
}
//复制文件,利用字符流复制文件
public static void CopyFile()throws FileNotFoundException,IOException{
BufferedReader fr1=null;
BufferedWriter fw1=null;
System.out.println("请输入要复制的源文件:");
String filePath01=s.next();
System.out.println("请输入要复制到的目标文件路径:");
String filePath02=s.next();
fr1=new BufferedReader(new FileReader(filePath01));
fw1=new BufferedWriter(new FileWriter(filePath02));
String str=null;
while((str=fr1.readLine())!=null){
fw1.write(str);
fw1.newLine();
}
if(fr1!=null){
fr1.close();
}
if(fw1!=null){
fw1.close();
}
}
}
总结
此案列利用了java类库中File类的一些基本操作。新人发文,不好的地方还请谅解。