简单的文件系统

Main类

package com.baidu.MyFile;

import java.io.IOException;

public class Main {
	public static void main(String[] args) throws IOException {
		MyFile myFile = new MyFile();
		myFile.run();
	}

}

 

MyFile类

package com.baidu.MyFile;

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.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

import javax.swing.text.AbstractDocument.BranchElement;


//dir cd mkdir copy type rename
public final class MyFile {
	private String currentPath;
	private File currentDir;
	private String driver = "c:";
	private String rootString = File.separator;
	
	public MyFile() {
		this.currentPath = System.getProperty("user.dir");//没有参数则是当前路径
		this.currentDir = new File(currentPath);
	}
	
	public MyFile(String dirPath) {
		this.currentPath = dirPath;
		this.currentDir = new File(dirPath);
	}
	
	public MyFile(File dir) {
		this.currentDir = dir;
		this.currentPath = dir.getAbsolutePath();//获取文件的绝对路径
	}
	//启动命令
	public void run() throws IOException {
		HashMap<String, Integer> cmdMap = new HashMap<String, Integer>();
		cmdMap.put("dir", 1);
		cmdMap.put("cd", 2);
		cmdMap.put("mkdir", 3);
		cmdMap.put("copy", 4);
		cmdMap.put("type", 5);
		cmdMap.put("rename", 6);
		cmdMap.put("exit", 7);
		cmdMap.put("ren", 8);
		
		Scanner input = new Scanner(System.in);
		String cmdString = null;
		System.out.println(currentPath + ">");
		while (!"exit".equals((cmdString = input.nextLine()))) {
			switch (cmdMap.get(getCmd(cmdString))) {
			case 1:
				showDir(currentDir);
				break;
			case 2:
				changeDir(getParas(cmdString) [1]);
				break;
				
			case 3:
				File file = new File(currentPath + File.separator + getParas(cmdString)[1]);
				makeDir(file);
				break;
				
			case 4:
				copyFile(getParas(cmdString)[1], getParas(cmdString)[2]);
				break;
				
			case 5:
				File file2 = new File(currentPath + File.separator + getParas(cmdString)[1]);
				typeFile1(file2);
				break;
			
			case 6:
			case 8:
				rename(getParas(cmdString)[1], getParas(cmdString)[2]);
				break;

			default:
				break;
			}
			System.out.println(currentPath + ">");
		}
		
		input.close();
		
		System.out.println("Bye!!!");
	}
	//查目录
	private void showDir(File dir) {
		System.out.println(dir.getAbsolutePath());
		//获取当前目录下的所有文件(不包括子目录的文件)
		File[] list = dir.listFiles();
		for (File file : list) {
			System.out.println(file.getName());
		}
		
	}
	//获取命令
	String getCmd(String cmdString) {
		//返回参数中的第0个
		return getParas(cmdString) [0];
	}
	
	//获取参数
	 ArrayList<String> getParameters(String cmdString) {
		StringBuilder sb = new StringBuilder(cmdString);
		ArrayList<String> paras = new ArrayList<String>();
		
		//命令字符串是否含有空格
		while (cmdString.indexOf("") >= 0) {
			//删除前导空格
			while (sb.indexOf(" ") == 0) {
				sb.delete(0, 1);
				continue;
			}
			//如果删除所有的前导空格后没有空格,退出循环
			if (sb.indexOf(" ") < 0) {
				break;
			}
			//获取第一个空格前的字符串
			String p1 = sb.substring(0, sb.indexOf(" "));
			//加入参数数组
			paras.add(p1);
			//删除第一个空格之前的字符串(+1包括删除空格)
			sb.delete(0, sb.indexOf(" ") + 1);
		} 
		//添加最后一个后面没有空格的字符串
		paras.add(sb.toString());
		return paras;
	}
	
	 //把命令字符串切成参数数组
	 String[] getParas(String cmdString ) {
		 return cmdString.trim().split(" +");
	 }
	private void changeDir(String subPath) {
		//相对路径 绝对路径 ..(上一级)
		if ("..".equals(subPath)) {
			//回到上一级
			currentPath = currentPath.substring(0, currentPath.lastIndexOf(File.separator));
			//equalsIgnoreCase  忽略大小写         s1.toUpperCase().equals(s1.toUpperCase())比较大小写方法
			if (driver.equalsIgnoreCase(currentPath)) {
				currentPath += File.separator;
				
			}
			//startsWith判断开始字符是否为rootString字符串,  toLowerCase()转换字符串大小写
		} else if (subPath.startsWith(rootString) || subPath.toLowerCase().startsWith(driver)) {
			currentPath = subPath;
		} else {
			//相对路径
			currentPath += File.separator + subPath;
		}
		File newDir = new File(currentPath);
		if (!newDir.exists() || !newDir.isDirectory()) {
			System.out.println("找不到指定目录");
			currentPath = currentDir.getAbsolutePath();
			return;
			
		}
		currentDir = newDir;
		//获取标准的绝对路径
		currentPath = currentDir.getAbsolutePath();
		
	}
	
	//创建文件方法
	private void makeDir(File dir) {
		//mkdir系统直接创建文件方法
		if (dir.mkdir()) { 
			System.out.println("创建了一个目录");
		} else {
			System.out.println("目录创建失败");
		}
		
	}
	
	private void copyFile(String src, String tag) {
		File srcFile = new File(currentDir + File.separator + src);
		File tagFile = new File(currentDir + File.separator + tag);
		
		if (!srcFile.exists()) {
			System.out.println("源文件沒有找到");
			return;
		}
		
		FileInputStream fis = null; //字节流
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream(srcFile);
			fos = new FileOutputStream(tagFile);
			int len = 0;
			byte[] buff = new byte[1024];
			while ((len = fis.read(buff)) > 0) {
				//刷新缓冲区
				fos.write(buff, 0, len);
			}
			//刷新缓冲区
			fos.flush();
			System.out.println("拷貝成功!!!");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("文件沒有找到");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if (null != fis) {
					fis.close();
					fos.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	//读取文件方法1
	private void typeFile(File file) throws IOException {
		FileReader reader = null;
		try {
			reader = new FileReader(file);
			int len = 0;
			char[] buff = new char[1024];
			while ((len = reader.read(buff)) > 0) {
				String s = new String(buff, 0, len);//把一个char数组直接转换为String数组输出
				System.out.print(s);
//				for (int i = 0; i < len; i++) {
//					System.out.print(buff[i]);
//				}
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("文件打开失败!!");
		}finally {
			if (null != reader) {
				try {
					reader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	//读文件2    一行一行读,效率没有提高
	private void typeFile1(File file) {
		FileReader reader = null;
		BufferedReader br = null;
		try {
			 reader = new FileReader(file);
			 br = new BufferedReader(reader); //一次能读一行
			String s = null;
			while ((s = br.readLine()) != null) {
				System.out.println(s);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			if (null != br) {
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		}
	}
	
	private void rename(String src, String tag) {
		File srcFile = new File(currentPath + File.separator + src);
		File tagFile = new File(currentPath + File.separator + tag);
		if (srcFile.renameTo(tagFile)) {
			System.out.println("文件名已修改!!");
		} else {
			System.out.println("文件修改失败!!");
		}
		
		}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值