package homework5;
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.io.InputStreamReader;
import java.util.Scanner;
public class Ch02 {
public static void main(String[] args) throws Exception {
System.out.println("欢迎来到我的资源管理器");
System.out.println("请选择:1.我的电脑;2.访问某个文件夹;3.复制;4.剪切;5删除;6.重命名;7.新建;8.属性");
int choose=new Scanner(System.in).nextInt();
switch (choose) {
case 1:
System.out.println("请选择:1.C盘;2.D盘;");
int choose1=new Scanner(System.in).nextInt();
switch (choose1) {
case 1:
Mycomp("C:\\");
break;
case 2:
Mycomp("D:\\");
break;
}
break;
case 2:
System.out.println("请输入要访问的文件夹的绝对路径:");
String abpath=new Scanner(System.in).next();
searchfile(abpath);
break;
case 3:
System.out.println("请输入要拷贝复制文件的绝对路径:");
String abpath1=new Scanner(System.in).next();
System.out.println("请输入要拷贝粘贴文件的绝对路径并命名:");
String abpath2=new Scanner(System.in).next();
copyfile(abpath1,abpath2);
break;
case 4:
System.out.println("请输入要剪切文件的绝对路径:");
String abpath4=new Scanner(System.in).next();
System.out.println("请输入要粘贴文件的绝对路径:");
String abpath5=new Scanner(System.in).next();
copyfile(abpath4,abpath5);
deleteDir(abpath4);
break;
case 5:
System.out.println("请输入要删除的文件的绝对路径:");
String abpath3=new Scanner(System.in).next();
deleteDir(abpath3);
break;
case 6:
System.out.println("请输入要重命名的文件的绝对路径:");
String abpath6=new Scanner(System.in).next();
System.out.println("请输入要重命名的文件旧名字:");
String oldname=new Scanner(System.in).next();
System.out.println("请输入要重命名的文件新名字:");
String newname=new Scanner(System.in).next();
rename(abpath6,oldname,newname);
break;
case 7:
System.out.println("请输入要创建文件的路径:");
String abpath7=new Scanner(System.in).next();
createFile(abpath7);
break;
case 8:
System.out.println("请输入要访问属性文件的路径:");
String abpath8=new Scanner(System.in).next();
shuxing(abpath8);
break;
default:
break;
}
}
//访问文件属性
private static void shuxing(String abpath8) {
File f=new File(abpath8);
System.out.println("文件名"+f.getName());
System.out.println("文件绝对路径"+f.getAbsolutePath());
System.out.println("文件上级目录"+f.getParent());
System.out.println("文件是否为隐藏文件"+f.isHidden());
System.out.println("文件长度"+f.length());
}
//创建文件
private static void createFile(String abpath7) throws Exception {
File f=new File(abpath7);
if (f.exists()) {
return;
}else {
f.createNewFile();
System.out.println("创建成功");
}
}
//重命名
private static void rename(String abpath6,String oldname, String newname) {
File f=new File(abpath6+ "/"+oldname);
File f1=new File(abpath6+ "/" + newname);
if (f.equals(f1)) {//如果新名和旧名一样即返回
return;
}if (f1.exists()) {//如果新名存在命名失败
System.out.println(f1+"已存在");
}else {
f.renameTo(f1);
}System.out.println("命名成功");
}
//删除文件
private static void deleteDir(String f1) throws Exception{
File f=new File(f1);
f.delete();
System.out.println("删除成功");
}
//拷贝文件
private static void copyfile(String oldpath, String newpath) throws Exception {
BufferedReader br=new BufferedReader(new FileReader(oldpath));
BufferedWriter bw=new BufferedWriter(new FileWriter(newpath));
String line;
while ((line=br.readLine())!=null) {
bw.write(line);
}
br.close();
bw.close();
System.out.println("拷贝成功");
}
//访问某个文件夹
private static void searchfile(String abpath) {
int i =0;
File f=new File(abpath);
String[] file=f.list();
for (String string : file) {
System.out.println(++i+"."+string);
}
}
//访问我的电脑中两个磁盘中的所有文件名
private static void Mycomp(String path) {
File f=new File(path);
File []file=f.listFiles();
int i=0;
for (File file1 : file) {
System.out.println(++i+"."+file1.getName()+(file1.isHidden()==true?"(隐藏文件)":""));
}
}
}