【java】IO基本知识及测试代码

学习笔记-java IO基本知识及测试代码

基本

概念

流:流动、流向 从一段移动到另一端 源头与目的地

程序 与 文件|数组|网络连接|数据库 ,以程序为中心

IO流分类

流向:输入流和输出流

数据:字节流:二进制,可以包含一切文件,包括纯文本、doc、音频、视频等等

​ 字符流:文本文件,只能处理纯文本

功能:节点:包括源头

​ 处理:增强功能,提供性能

字符流与字节流 与 文件

字节流

//输入流
InputStream 
read(byte[] b)
read(byte[] b,int off,int len)  +  close()
FileInputStream()

// 输出流
OutputStream 
write(byte[] b)
write(byte[] b,int off,int len)  +  flush()  +  close()
FileOutputStream

字符流

//输入流
Reader 
read(char[] cbuf)
read(char[] cbuf,int off,int len)  +  close()
FileReader()

//输出流
Writer 
write(charp[] cbuf)
write(char[] cbuf,int off,int len)  +  flush()  +  close()
write(String str,int off,int len)  +  flush()  +  close()
FileWriter()

两个常量

1、路径分隔符
2、文件分隔符

package com.iotest.file;

import java.io.File;

/**
 * 两个常亮
 * 1、路径分隔符
 * 2、文件分隔符
 */
public class Demo01 {

    public static void main(String[] args) {
        System.out.println(File.pathSeparator);
        System.out.println(File.separator);
        // 路径表现形式
        String path = "D:\\REMEM\\lv1111.png";
        path = "D:"+File.separator+"REMEN"+File.separator+"lv1111.png";
        // 推荐方式
        path = "D:/REMEN/lv1111.png";

    }
}

相对路径与绝对路径构造File对象

package com.iotest.file;

import java.io.File;

/**
 * 相对路径与绝对路径构造File对象
 */
public class Demo02 {
    /**
     *
     */
    public static void main(String[] args) {
        String parentPath = "D:/REMEN";
        String name = "test.png";
        // 相对路径
        File src = new File(parentPath, name);
        src = new File(new File(parentPath), name);
        // out
        System.out.println(src.getName());
        System.out.println(src.getPath());


        // 绝对路径
        src = new File("D:/REMEN/test.png");
        // out
        System.out.println(src.getName());
        System.out.println(src.getPath());

        // 没有盘符 以user.dir构建
        src = new File("text.png");
        System.out.println(src.getName());
        System.out.println(src.getPath());
        System.out.println(src.getAbsolutePath());


    }
}

常用方法

1、文件名

扫描二维码关注公众号,回复: 4354324 查看本文章
  • getName() 文件名、路径名
  • getPath() 路径名
  • getAbsoluteFile() 以绝对路径所对应的File对象
  • getAbsolutePath() 绝对路径名
  • getParent() 父目录,相对路径的父目录,可能为null,入,删除本身后的结果

2、判断信息

  • exists() 是否存在
  • canWrite() 文件是否可以写
  • isFile() 是否文件
  • isDirectory() 是否文件夹,没有真实存在的,默认是文件夹
  • isAbsolute() 消除平台差异,以盘符开头,其他以/开头

3、长度

  • length() 字节数,单位为字节。不能读取文件夹的长处

4、创建和删除文件

  • createNewFile() 不存在创建新文件,con系统关键字无法创建
  • delete() 删除文件
  • static createTempFile(前缀3个字节长,后缀默认.temp) 默认临时空间
  • static createTempFIle(前缀3个人字节长,后缀默认.,temp,目录)
  • deleteOnExit() 退出虚拟机删除,常用语删除临时文件。
package com.iotest.file;

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

/**
 * 常用方法
 * 1、文件名
 * getName() 文件名、路径名
 * getPath() 路径名
 * getAbsoluteFile() 以绝对路径所对应的File对象
 * getAbsolutePath() 绝对路径名
 * getParent() 父目录,相对路径的父目录,可能为null,入,删除本身后的结果
 * 2、判断信息
 * exists() 是否存在
 * canWrite() 文件是否可以写
 * isFile() 是否文件
 * isDirectory() 是否文件夹,没有真实存在的,默认是文件夹
 * isAbsolute() 消除平台差异,以盘符开头,其他以/开头
 * 3、长度
 * length() 字节数,单位为字节。不能读取文件夹的长处
 * 4、创建和删除文件
 * createNewFile() 不存在创建新文件,con系统关键字无法创建
 * delete() 删除文件
 * static createTempFile(前缀3个字节长,后缀默认.temp)  默认临时空间
 * static createTempFIle(前缀3个人字节长,后缀默认.,temp,目录)
 * deleteOnExit() 退出虚拟机删除,常用语删除临时文件。
 */
public class Demo03 {

    public static void main(String[] args) {
        test3();
        //test1();

    }

    public static void test1() {
        // 建立联系
        File src = new File("2t.txt");
        //File src = new File("D:/REMEN/test.png");
        System.out.println(src.getName()); // 返回名称
        System.out.println(src.getPath()); // 如果是绝对路径,返回完成路径,否则相对路径
        System.out.println(src.getAbsolutePath()); // 返回绝对路径
        System.out.println(src.getParent()); // 返回上一级目录,可能为null
    }

    public static void test2() {
        String path = "D:/Code/Java/IO/src/main/resources/test.png";
        File src = new File(path);
        System.out.println("是否存在" + src.exists());
        System.out.println("是否可写" + src.canWrite());
        if (src.isFile()) {
            System.out.println("文件");
        } else {
            System.out.println("文件夹");
        }
        System.out.println("是否是绝对路径" + src.isAbsolute());

        System.out.println("长度" + src.length());
    }

    public static void test3() {
        String path = "D:/Code/Java/IO/src/main/resources/200.png";
        File src = new File(path);
        if (!src.exists()) {
            boolean flag = false;
            try {
                flag = src.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("create file fail");
            }
            System.out.println(flag ? "success" : "fail");
        }
        boolean flag = src.delete();
        System.out.println(flag ? "delete success" : "d false");

        try {
            File temp = File.createTempFile("tes", ".temp", new File("D:/Code/Java/IO/src/main/resources"));
            try {
                // 2s后删除
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 退出即删除
            temp.deleteOnExit();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

5、操作目录

  • mkdir() 创建目录 必须确保父目录存在 如果不存在则创建失败
  • mkdirs() 创建目录 如果父目录不存在 一同创建
  • list() 子目录-文件名 字符串形式
  • listFiles() 子目录-文件file对象 file形式;后可以加filter过滤器
  • static listRoots() 根路径
package com.iotest.file;

import java.io.File;
import java.io.FileFilter;

/**
 * 5、操作目录
 *  mkdir() 创建目录 必须确保父目录存在 如果不存在则创建失败
 *  mkdirs() 创建目录 如果父目录不存在 一同创建
 *  list() 子目录-文件名 字符串形式
 *  listFiles() 子目录-文件file对象 file形式;后可以加filter过滤器
 *  static listRoots() 根路径
 *
 */
public class Demo04 {

    public static void main(String[] args) {
        test1();
        test2();
    }
    public static void test1() {
        String path = "D:/Code/Java/IO/src/main/resources/testfile/test";
        File src = new File(path);
        //src.mkdir();
        src.mkdirs();
    }
    public static void test2(){

        String path = "D:/Code/Java/IO/src/main/resources";
        File src = new File(path);
        System.out.println("=========子目录-文件名=========");
        if (src.isDirectory()){
            String[] subNames = src.list();
            for (String temp:subNames){
                System.out.println(temp);
            }
        }
        System.out.println("=========子目录-文件File对象=========");
        if (src.isDirectory()){
            File[] subFiles = src.listFiles();
            for (File temp:subFiles){
                System.out.println(temp.getAbsolutePath());
            }
        }
        System.out.println("=========子目录-.java对象=========");
        if (src.isDirectory()){
            File[] subFiles = src.listFiles(new FileFilter() {
                public boolean accept(File pathname) {
                    return pathname.isFile() && pathname.getName().endsWith(".java");
                }
            });
            for (File temp:subFiles){
                System.out.println(temp.getAbsolutePath());
            }
        }
    }
}

输出子孙级目录-文件的名称

package com.iotest.file;

import java.io.File;
import java.util.Arrays;

/**
 * 输出子孙级目录-文件的名称
 * 1、listFiles()
 * 2、递归
 */
public class Demo05 {
    public static void main(String[] args) {
        String path = "D:/Code/Java/IO/src/main/resources";
        File parent = new File(path);
        //printName(parent);

        File[] roots = File.listRoots();
        System.out.println(Arrays.toString(roots));

    }

    public static void printName(File src) {
        if (null == src || !src.exists()){
            return;
        }
        System.out.println(src.getAbsolutePath());
        if (src.isDirectory()) {
            for (File sub:src.listFiles()){
                printName(sub);
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43140314/article/details/83591201