JavaSE-IO

文件的基础知识

基本概念

文件:保存文字,视频,音频,图片等内容

文件流:文件在程序中以流的形式来操作

              Java程序(内存)---输出流-->文件(磁盘)

              Java程序(内存)<-输入流----文件(磁盘)

个人理解:程序员操作的是程序,数据到了程序中,也就是到了我们可以操作的范围内,就是输入流,反之就是输出流

常用文件操作

创建文件的几种方式

总结:三种方式只是创建文件时的构造器传入参数不同,方式1是传路径加文件名(一个String),方式3是将路径和文件名分开(两个String),方式2是将文件路径封装为一个对象,文件名还是String

public static void main(String[] args) {
        new UseFile().create01();//方式1创建文件
        new UseFile().create02();//方式2创建文件
        new UseFile().create03();//方式3创建文件
    }
    //方式1:new File();
    public void create01(){
        String filePath = "D:\\演示.txt";
        File file = new File(filePath);//一个文件对象就代表着一个文件,这里的对象只是在内存中
        try {
            file.createNewFile();     //去创建这个文件,现在这个文件才进入了磁盘
            System.out.println("文件创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //方式2:new File (File parent,String child) 根据父目录文件+子路径构建
    public void create02(){
        File parentFile = new File("D:\\");
        String fileName = "演示2.txt";
        File file = new File(parentFile,fileName);
        try {
            file.createNewFile();
            System.out.println("创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //方式3:new File(String parent,String child) 根据父目录+子路径构建
    public void create03(){
        String parentPath = "D:\\";//这样写e:/也可以
        String fileName = "演示3.txt";
        File file = new File(parentPath,fileName);
        try {
            file.createNewFile();
            System.out.println("创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

获取文件相关的信息

public void info(){
        File file = new File("D:\\演示2.txt");
        //调用相应方法去得到相应信息
        System.out.println("文件名:"+file.getName());
        System.out.println("绝对路径:"+file.getAbsolutePath());
        System.out.println("文件父级目录:"+file.getParent());
        System.out.println("文件大小(字节):"+file.length());
        System.out.println("文件是否存在:"+file.exists());
        System.out.println("是不是一个文件:"+file.isFile());
        System.out.println("是不是一个目录:"+file.isDirectory());
    }

常用的文件操作(删除创建)

 File file = new File("D:\\演示.txt");
        //1.判断D:\\演示.txt是否存在,若存在则删除
        if (file.exists()) {
            if (file.delete())
                System.out.println("删除成功");
            else
                System.out.println("删除失败");
        } else
            System.out.println("文件不存在");

        //2.判断目录是否存在,若存在则删除,否则提示不存在,这里说明目录也看作是文件对象的一种
        File directory = new File("D:\\demo");
        if (directory.exists()) {
            if (directory.delete())
                System.out.println("删除成功");
            else
                System.out.println("删除失败了");
        } else
            System.out.println("目录不存在");
        //3.判断目录是否存在,如果不存在则创建
        File directorys = new File("D:\\A\\B");
        if (directorys.exists()) {
            System.out.println("目录存在");
        } else {
            if (directorys.mkdirs()) {//mkdir是创建单级目录
                System.out.println("创建成功");
            } else {
                System.out.println("创建失败");
            }
        }

 文件的创建和删除,目录的创建和删除

IO流分类

流的介绍

按照流向

输入input:读取外部数据(磁盘)到程序(内存)中

输出output:将程序(内存)数据输出到磁盘

按照流处理的数据单位

字节流:按字节(8bit),常处理二进制文件

字符流:按字符,常处理文本文件

按照流的角色

节点流

你可能感兴趣的:(Java基础,java)