java用流實現建立檔案夾, 檔案改名, 檔案刪除, 檔案複製

來源:互聯網
上載者:User

標籤:copyfile   director   com   ++   for   hash   trace   get   tor   

    建立TestFileManager類,在本類中分別建立factFile()方法(遍曆檔案夾中所有的檔案)、console()方法(換行)、createFolder()方法(建立檔案夾)、

    renameFile()方法(改名)、deleteFile()方法(刪除檔案)、copyFile()方法(複製檔案)、showContent()(每一次操作完成重新輸出所有檔案);

package com.maya.hanqi.filemanager;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.HashMap;import java.util.Map;import java.util.Scanner;public class TestFileManager {    private static Map<String, File> fileMap = new HashMap<String, File>();    private static boolean lock = true;    public static void main(String[] args) {        Scanner scanner = null;        while (lock) {            showContent();            scanner = new Scanner(System.in);            System.out.println("1---在選定的目錄下建立檔案夾");            System.out.println("2---將選定的檔案改名");            System.out.println("3---將選定的檔案刪除");            System.out.println("4---將選定的檔案複製到另一個路徑");            System.out.println("5---退出");            String console = scanner.nextLine();            if ("1".equals(console)) {                System.out.println("想在哪個檔案夾下建立 ?");                String path = scanner.nextLine();                System.out.println("輸入新檔案夾的名字...");                String folderName = scanner.nextLine();                createFolder(path, folderName);            }            if ("2".equals(console)) {                System.out.println("說 ! 改誰 ?");                String fname = scanner.nextLine();                System.out.println("改成什麼 ?");                String targetName = scanner.nextLine();                renameFile(fname, targetName);            }            if ("3".equals(console)) {                System.out.println("又看誰不順眼了啊 ?");                String fname = scanner.nextLine();                deleteFile(fname);            }            if ("4".equals(console)) {                System.out.println("複製哪個 ?");                String fname = scanner.nextLine();                System.out.println("複製到哪裡 ?");                String targetPath = scanner.nextLine();                copyFile(fname, targetPath);            }            if ("5".equals(console)) {                lock = false;                System.err.println("系統已經退出 !");            }        }        scanner.close();    }    public static void showContent() {        File file = new File("E:\\java\\folder");        System.out.println(file.getName());        fileMap.put(file.getName(), file);        factFile(file, 1);    }    public static void copyFile(String fname, String targetPath) {        File file = fileMap.get(fname);        BufferedReader br = null;        BufferedWriter bw = null;        String sc = null;        if (!(file == null)) {            if (fileMap.get(targetPath).isDirectory()) {                try {                    File newFile = new File(fileMap.get(targetPath).getPath() + "\\" + fname);                    br = new BufferedReader(new FileReader(fileMap.get(fname)));                    bw = new BufferedWriter(new FileWriter(newFile));                    while ((sc = br.readLine()) != null) {                        bw.write(sc);                    }                    br.close();                    bw.close();                    System.err.println("檔案複製成功 !");                } catch (FileNotFoundException e) {                    e.printStackTrace();                } catch (IOException e) {                    e.printStackTrace();                }            } else {                System.err.println("目標路徑不是檔案夾, 不能複製 !");                return;            }        } else {            System.err.println("輸入的路徑不存在 !");        }    }    public static void deleteFile(String fname) {        File file = fileMap.get(fname);        if (!(file == null)) {            if(file.listFiles().length > 0) {                System.err.println("不是空檔案夾, 無法刪除 !");                return;            }            file.delete();            fileMap.remove(fname);            System.err.println("檔案已刪除 !");        } else {            System.err.println("輸入的路徑不存在 !");        }    }    public static void renameFile(String fname, String targetName) {        File file = fileMap.get(fname);        if (!(file == null)) {            File newFile = new File(file.getParentFile().getPath() + "\\" + targetName);            file.renameTo(newFile);            System.out.println("檔案名稱已經更改 !");        } else {            System.err.println("輸入的路徑不存在 !");        }    }    public static void createFolder(String address, String folderName) {        File file = fileMap.get(address);        if (!(file == null)) {            File newFile = new File(file.getPath() + "\\" + folderName);            if (newFile.mkdirs()) {                System.err.println("檔案夾成功建立");            } else {                System.err.println("建立檔案夾失敗");            }        } else {            System.err.println("輸入的路徑不存在 !");        }    }    public static void console(int k) {        for (int a = 0; a < k; a++) {            System.out.print("   ");        }    }    public static void factFile(File file, int length) {        if (file.exists() && file.isDirectory()) {            File[] fs = file.listFiles();            for (int i = 0; i < fs.length; i++) {                console(length);                System.out.println(fs[i].getName());                factFile(fs[i], length + 1);                fileMap.put(fs[i].getName(), fs[i]);            }        }    }}

 

java用流實現建立檔案夾, 檔案改名, 檔案刪除, 檔案複製

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.