09java进阶——IO

1.File类

1.1目录及路径分隔符

package cn.jxufe.java.chapter09.demo01;

import java.io.File;

public class Test01FileSeparator {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // File类静态成员变量
        // 与系统有关的路径分隔符
        String separator = File.pathSeparator;
        System.out.println(separator);// 是一个分号,目录的分割 Linux :

        // 与系统有关的默认名称分隔符
        separator = File.separator;
        System.out.println(separator);// 向右 \ 目录名称分割 Linux /
    }

}

1.2三种构造方法

package cn.jxufe.java.chapter09.demo01;

import java.io.File;

public class Test02Constructor {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        function();
        System.out.println();
        function_1();
        System.out.println();
        function_2();
    }

    /*
     *  File(File parent,String child)
     *  传递路径,传递File类型父路径,字符串子路径
     *  好处: 父路径是File类型,父路径可以直接调用File类方法
     */
    public static void function_2() {
        File parent = new File("d:");
        File file = new File(parent, "eclipse");
        System.out.println(file);
    }

    /*
     *  File(String parent,String child)
     *  传递路径,传递字符串父路径,字符串子路径
     *  好处: 单独操作父路径和子路径
     */
    public static void function_1() {
        File file = new File("d:", "eclipse");
        System.out.println(file);
    }

    /*
     *  File(String pathname)
     *  传递路径名: 可以写到文件夹,可以写到一个文件
     *  c:\\abc   c:\\abc\\Demo.java
     *  将路径封装File类型对象
     */
    public static void function() {
        File file = new File("d:\\eclipse");
        System.out.println(file);
    }
}

1.3创建、删除文件和文件夹

package cn.jxufe.java.chapter09.demo01;

import java.io.File;
import java.io.IOException;

public class Test03DeleteAndCreate {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        function();
        function_1();
        function_2();
    }

    /*
     *  File类的删除功能
     *  boolean delete()
     *  删除的文件或者是文件夹,在File构造方法中给出
     *  删除成功返回true,删除失败返回false
     *  删除方法,不走回收站,直接从硬盘中删除
     *  删除有风险,运行需谨慎
     */
    public static void function_2() {
        File file = new File("d:\\a.txt");
        boolean b = file.delete();
        System.out.println(b);
    }

    /*
     *  File创建文件夹功能
     *  boolean mkdirs() 创建多层文件夹,也可以创建单层文件夹(推荐使用)
     *  boolean mkdir() 只能创建单层文件夹
     *  创建的路径也在File构造方法中给出
     *  文件夹已经存在了,不在创建
     */
    public static void function_1() {
        File file = new File("d:\\abc");
        boolean b = file.mkdirs();
        System.out.println(b);
    }

    /*
     *  File创建文件的功能
     *  boolean createNewFile()
     *  创建的文件路径和文件名,在File构造方法中给出
     *  文件已经存在了,不在创建
     */
    public static void function() throws IOException {
        File file = new File("d:\\a.txt");
        boolean b = file.createNewFile();
        System.out.println(b);
    }
}

1.4获取功能

package cn.jxufe.java.chapter09.demo01;

import java.io.File;

/*
 *  File类的获取功能
 */
public class Test04Get {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        function();
        function_1();
        function_2();
        function_3();
    }

    /*
     * File类的获取功能
     * String getParent() 返回String对象
     * File getParentFile()返回File对象
     * 获取父路径
     */
    public static void function_3() {
        File file = new File("d:\\eclipse\\eclipse.exe");
        File parent = file.getParentFile();
        System.out.println(parent);
    }

    /*
     * File类获取功能
     * String getAbsolutePath() 返回String对象
     * File   getAbsoluteFile() 返回File对象
     * 获取绝对路径
     * eclipse环境中,写的是一个相对路径,绝对位置工程根目录
     */
    public static void function_2() {
        File file = new File("src");
        File absolute = file.getAbsoluteFile();
        System.out.println(absolute);
    }

    /*
     * File类获取功能
     * long length()
     * 返回路径中表示的文件的字节数
     */
    public static void function_1() {
        File file = new File("d:\\eclipse\\eclipse.exe");
        long length = file.length();
        System.out.println(length);
    }

    /*
     *  File类的获取功能
     *  String getName()
     *  返回路径中表示的文件或者文件夹名
     *  获取路径中的最后部分的名字
     */
    public static void function() {
        File file = new File("d:\\eclipse\\eclipse.exe");
        String name = file.getName();
        System.out.println(name);

        /*String path = file.getPath();
        System.out.println(path);*/
//        System.out.println(file);
    }
}

1.5判断功能

package cn.jxufe.java.chapter09.demo01;

import java.io.File;

public class Test05JudgingFunction {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        function();
        function_1();
    }

    /*
     *  File判断功能
     *  boolean isDirectory()
     *  判断File构造方法中封装的路径是不是文件夹
     *  如果是文件夹,返回true,不是文件返回false
     *  
     *  boolean isFile()
     *  判断File构造方法中封装的路径是不是文件
     */
    public static void function_1() {
        File file = new File("d:\\eclipse\\eclipse.exe");
        if (file.exists()) {
            boolean b = file.isDirectory();
            System.out.println(b);
        }
    }

    /*
     *  File判断功能
     *  boolean exists()
     *  判断File构造方法中封装路径是否存在
     *  存在返回true,不存在返回false
     */
    public static void function() {
        File file = new File("src"); // 如果用相对路径,默认的路径为当前项目的工作路径
        boolean b = file.exists();
        System.out.println(b);
    }
}

猜你喜欢

转载自www.cnblogs.com/xinmomoyan/p/10964339.html