java 文件操作

查看文件夹中的文件List:

    public static void selectFilesList() {

        File file = new File("G:/qq文件");

        System.out.println("G:\\qq文件 下目录:" + file.listFiles().length + "个:");

        for (Object fileTem : file.listFiles()) { // file.list()不包含详细路径。
            System.out.println(fileTem); // file.listFiles()包含详细路径。
        }

        System.out.println("该分区大小" + file.getTotalSpace()
                / (1024 * 1024 * 1024) + "G");// 磁盘的总大小

        System.out.println("listRoots:" + File.listRoots().length + "个:");

        for (Object fileTem : File.listRoots()) {
            System.out.println(fileTem); // File.listRoots 遍历:电脑磁盘根目录列表。
        }
    }

文件夹的创建、重命名、删除、查看父目录路径:

    public static void optionFolder() {
        File file1 = new File("G:/ceshi/ceshi");
        file1.mkdirs(); // 创建文件夹,包括不存在的中间目录。
        file1.renameTo(new File("G:/ceshi/ceshi_reName"));// 重命名为:ceshi_reName

        File file2 = new File("G:/ceshi/ceshi2");
        file2.mkdirs();
        file2.delete(); // 删除指定、没有子目录的目录。

        File file3 = new File("G:/qq文件/Image");
        System.out.println("文件名  " + file3.getName()); // 返回由文件或目录的名称。
        System.out.println("文件父目录字符串 " + file3.getParent());// 返回父目录的路径名字符串;如果没有,则返回null。
    }

文件的创建、重命名、删除 --和文件夹操作类似:

    public static void optionFile() throws IOException {
        File file4 = new File("G:/ceshi.txt");
        file4.createNewFile();// 创建文件,不存在则创建。
        file4.renameTo(new File("G:/重命名.txt"));
    }

文件读写,以字节形式。适用于多有文件:

    public static void copyFileByByte() throws IOException {
        File file5 = new File("G:/购物车.jpg");
        File file6 = new File("G:/购物车_copy.jpg");

        InputStream in = new FileInputStream(file5);
        OutputStream out = new FileOutputStream(file6);
        // OutputStream op = new FileOutputStream(File file, true); 追加形式-写入

        byte[] buffer = new byte[1024];

        int count = 0;
        while ((count = in.read(buffer)) != -1) { // 读取文件--按字节,并递增指针到下一个字节
            out.write(buffer, 0, count);
            out.flush();
            System.out.println("---长度: " + count + " 字节");
        }

        in.close();
        out.close();
    }

文本文件读写,字符串形式。仅适用于纯文本文件:

    public static void copyFileByChar() throws IOException {
        FileReader file7 = new FileReader("G:/1.txt");
        FileWriter file8 = new FileWriter("G:/1_copy.txt");

        BufferedReader buffIn = new BufferedReader(file7);
        BufferedWriter buffOut = new BufferedWriter(file8);

        char[] ch = new char[10]; // 字符串--读写文件

        int count = 0;
        while ((count = buffIn.read(ch)) != -1) {// 读取文件--按字符串
            buffOut.write(ch, 0, count);
            buffOut.flush();
            System.out.println(count);
        }

        buffIn.close();
        buffOut.close();
    }

文本文件读写---追加形式-写入:

    public static void appendFile() throws IOException {
        File fileText = new File("D:/springTask.txt");
        FileWriter fileWriter = new FileWriter(fileText, true);
        PrintWriter pw = new PrintWriter(fileWriter);
        pw.println(":任务执行中..."); // 字符串末尾不需要换行符
        pw.close();
        fileWriter.close();
    }
posted @ 2017-12-07 14:35  游园拾忆  阅读(34)  评论(0编辑  收藏  举报