展开全部
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Scanner;
import javax.imageio.stream.FileImageInputStream;
public class Abc {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
start();
}
public static void start() throws IOException
{
showSelection();
int i=0;
Scanner scan=new Scanner(System.in);
i=scan.nextInt();
select(i);
}
public static void showSelection()
{
System.out.print("1.创建");
System.out.print(" 2.显示e68a843231313335323631343130323136353331333332626132");
System.out.print(" 3.重命名");
System.out.print(" 4.复制");
System.out.print(" 5.删除");
System.out.println(" 6.退出");
System.out.print("请输入");
}
public static void select(int i) throws IOException
{
switch (i) {
case 1:System.out.println("创建");creatFile();start();break;
case 2:viewFile();start();break;
case 3:renameFile();start();break;
case 4:copyFile();start();break;
case 5:delFile();start();break;
case 6: System.exit(0);break;
default:
break;
}
}
public static boolean creatFile()
{
Scanner scans=new Scanner(System.in);
System.out.println("请输入要创建的文件名");
String name=scans.nextLine();
File file=new File("D:\\"+name);
try{
file.createNewFile();
System.out.println("创建成功");
}
catch (Exception e) {
// TODO: handle exception
}
return true;
}
public static boolean viewFile() throws IOException
{
Scanner scans=new Scanner(System.in);
System.out.println("请输入要读取的文件名");
String name=scans.nextLine();
File file=new File("D:\\"+name);
BufferedReader br1=new BufferedReader(new FileReader("D:\\"+name));
String str="";
while((str=br1.readLine())!=null)
System.out.println(str);
br1.close();
br1=null;
return true;
}
public static boolean renameFile()
{
Scanner scans=new Scanner(System.in);
System.out.println("请输入要重命名的文件名");
String name=scans.nextLine();
System.out.println("请输入新的文件名");
String newname=scans.nextLine();
File newfile=new File("D:\\"+newname);
File file=new File("D:\\"+name);
try{
file.renameTo(newfile);
System.out.println("重命名成功");
}
catch (Exception e) {
// TODO: handle exception
}
return true;
}
public static boolean copyFile() throws IOException
{int byteread=0;
int bytesum=0;
Scanner scans=new Scanner(System.in);
System.out.println("请输入要复制的文件名");
String name=scans.nextLine();
File file=new File("D:\\"+name);
System.out.println("请输入新复制后的文件名");
String newname=scans.nextLine();
File newfile=new File("D:\\"+newname);
InputStream in=new FileInputStream(file);
FileOutputStream fs=new FileOutputStream(newname);
byte[] buffer=new byte[1444];
while((byteread=in.read(buffer))!=-1)
{
bytesum+=byteread;
fs.write(buffer,0,byteread);
}
in.close();
return true;
}
public static boolean delFile()
{
Scanner scans=new Scanner(System.in);
System.out.println("请输入要删除的文件名");
String name=scans.nextLine();
File file=new File("D:\\"+name);
try{
file.delete();
System.out.println("删除成功");
}
catch (Exception e) {
// TODO: handle exception
}
return true;
}
}