package src;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class task7_3 {
public static void main(String[] args) throws IOException {
System.out.println("--1.指令关键字检索文件2.指令后缀名检索文件3.复制文件/目录4.退步--");
Scanner in=new Scanner(System.in);
int n=0;
String forinput,aftinput;
while(true) {
System.out.print("请输入指令:");
try {
n=in.nextInt();
}catch(Exception e) {
System.out.println("输入格式有误");
continue;
}
switch (n){
case 1:
System.out.print("请输入要检索的目录位置:");
forinput=in.next();
System.out.print("请输入搜索关键字:");
aftinput=in.next();
Manage.findkeyword(forinput,aftinput);
break;
case 2:
System.out.print("请输入要检索的目录位置:");
forinput=in.next();
System.out.print("请输入搜索后缀:");
String tail=in.next();
String[] spilttail=tail.split(",");
Manage.findtail(forinput,spilttail);
break;
case 3:
System.out.print("请输入源目录:");
forinput=in.next();
System.out.print("请输入目标目录:");
aftinput=in.next();
Manage.filecopy(forinput,aftinput);
break;
case 4:
System.exit(0);
break;
default:
System.out.println("输入指令有误,重新输入");
break;
}
}
}
}
class Manage{
//找关键词
public static void findkeyword(String pathName,String forname) throws IOException {
File dirFile=new File(pathName);
if(!dirFile.exists()) {
System.out.println("文件不存在!");
return;
}
if(!dirFile.isDirectory()) {
if(dirFile.isFile()) {
String str=dirFile.getName();
if(str.indexOf(forname)!=-1);
System.out.println(dirFile.getCanonicalFile());
}
return;
}
if(dirFile.getName().indexOf(forname)!=-1)
System.out.println(dirFile.getCanonicalPath());
String[] filelist=dirFile.list();
for(int i=0;i<filelist.length;i++) {
String string=filelist[i];
File file=new File(dirFile.getPath(),string);
String name=file.getName();
if(file.isDirectory()) {
findkeyword(file.getAbsolutePath(),forname);
}
else {
if(name.indexOf(forname)!=-1)
System.out.println(file.getCanonicalFile());
}
}
}
//找后缀
public static void findtail(String pathName,String[] forname) throws IOException {
File dirFile=new File(pathName);
if(!dirFile.exists()) {
System.out.println("文件不存在!");
return;
}
if(!dirFile.isDirectory()) {
if(dirFile.isFile()) {
String str=dirFile.getName();
String tail=str.substring(str.lastIndexOf(".")+1);
for(int i=0;i<forname.length;i++) {
if(tail.equals(forname[i]))
System.out.println(dirFile.getCanonicalFile());
}
}
return;
}
String[] filelist=dirFile.list();
for(int i=0;i<filelist.length;i++) {
String string=filelist[i];
File file=new File(dirFile.getPath(),string);
String name=file.getName();
if(file.isDirectory()) {
findtail(file.getAbsolutePath(),forname);
}
else {
String tail=name.substring(name.lastIndexOf(".")+1);
for(int j=0;j<forname.length;j++) {
if(tail.equals(forname[j]))
System.out.println(file.getCanonicalFile());
}
}
}
}
//file的copy
public static void filecopy(String Orpath,String Topath) throws IOException {
File source=new File(Orpath);
if(!source.exists()) {
System.out.println("不存在"+Orpath+"该目录或文件");
return;
}
File des=new File(Topath);
if(!des.exists())
{
des.mkdir();
}
File[] file=source.listFiles();
FileInputStream input = null;
FileOutputStream output = null;
for(int i=0;i<file.length;i++)
{
try {
if(file[i].isFile()) {
input=new FileInputStream(file[i]);
output=new FileOutputStream(new File(Topath+"/"+file[i].getName()));
byte[] b=new byte[1024];
int len;
while((len=input.read(b))!=-1) {
output.write(b,0,len);
}
input.close();
output.flush();
output.close();
}
else if(file[i].isDirectory()) {
filecopy(Orpath+"/"+file[i].getName(), Topath+"/"+file[i].getName());
}
}catch(FileNotFoundException e) {
e.printStackTrace();
}
}
if(output!=null)
output.close();
if(output!=null)
output.close();
}
}
听说打“null”在博客上被吃掉了直接没显示,你们自行补上
java模拟文件管理操作
最新推荐文章于 2023-05-22 22:32:00 发布