java的IO流1

package exercise;

import java.io.File;

/**File类的构造方法

* 三种形式

* @author Pandy

* @date 2018/5/18 19:57

*/

public class FileDemo1 {

public static void function1(){

File file =new File("E:\\03");

        System.out.println(file);

        boolean a = file.exists();

        System.out.println(a);

    }

/*

* File(String,pathname)

* 传递路径名:可以写到文件夹 可以写到一个文件

* c:\\abc

* 将路径封装成File对象

* */

    public static void main(String[] args) {

function1();

        function2();

        function3();

    }

public static void function2(){

File file =new File("c:","windows");

        System.out.println(file);

    }

public static void function3(){

File parent =new File("d:");

        File file =new File(parent,"window");

        System.out.println(file);

    }

}




package exercise;

import java.io.File;

import java.io.IOException;

/**

* @ author Pandy

* @ date 2018/5/18 20:18

*/

public class FileDemo2 {

public static void main(String[] args)throws IOException{

function();

        function1();

        function2();

        function_3();

    }

/*

* 创建文件的功能

* boolean creatNewFile()

* 创建的文件路径和文件名是在file 的构造方法中给出的

*文件已经存在就不再创建

* 如果没有指定文件形式  会创建一个文件  就是普通的文本

* */

    public static void function()throws IOException{

File file =new File("E:\\新建文件夹");

        boolean b = file.createNewFile();

        System.out.println(b);

    }

/*

* 创建文件夹的功能

* boolean mkdir()

* 创建的路径也在File的构造方法中给出

* 文件夹存在  则不会创建

* */

    public static void function1(){

File file =new File("E:\\创建新的文件夹");

        boolean c = file.mkdir();

        System.out.println(c);

    }

/*

* 创建多层文件夹

* mkdirs

* */

    public static void function2(){

File file =new File("E:\\6666");

        boolean d = file.mkdirs();

        System.out.println(d);

    }

/*

* 删除文件

* boolean delete

* 删除的文件或者是文件夹 在file构造方法中给出

* 删除方法  不走回收站  直接从硬盘删除

* 删除有风险  使用会谨慎

* */

    public static void function_3(){

File file =new File("E:\\6666");

        boolean e = file.delete();

        System.out.println(e);

    }

}



package exercise;

import jdk.jfr.FlightRecorderListener;

import java.io.File;

/**

* @author Pandy

* @date 2018/5/18 20:36

*/

public class FileDemo3 {

public static void main(String[] args) {

function();

        function2();

        function3();

    }

/*

* File类的获取功能

* String getName()

* 返回路径中的文件或者文件夹名

* 这只是一个获取文件夹功能

* */

    public static void function(){

File file =new File("c:\\system32");

        String name = file.getName();

        System.out.println(name);

//        String path = file.getPath();

//        //将抽象路径名转化成一个字符串

//        System.out.println(path);

    }

/*

* File类中的一个获取功能

* long length()

* 返回路径中表示文件的字节数

* */

    public static void function2(){

File file =new File("E:\\JaveDemo\\out\\production");

        long length = file.length();

        System.out.println(length);

    }

/*

* 获取绝对路径

* String getAbsolutePath()

* File getAbsoluteFile()

* */

    public static void function3(){

File file =new File("c:\\system32");

        File absolute = file.getAbsoluteFile();

        System.out.println(absolute);

    }

}

你可能感兴趣的:(java的IO流1)