JavaSE基础学习——File、方法递归、字符集、IO流(基础流)

1. File类概述

  • File类在包java.io.File下、代表操作系统的文件对象(文件、文件夹
  • File提供了诸如:定位文件,获取文件本身的信息、删除文件、创建文件(文件夹)等功能

1.1 File类创建对象

在这里插入图片描述

  • File对象可以定位文件和文件夹
  • File封装的对象仅仅是一个路径名,这个路径可以是存在的,也可以是不存在的。

1.2 绝对路径和相对路径

  • 绝对路径:从盘符开始
    在这里插入图片描述
  • 相对路径:不带盘符默认直接到当前工程下的目录寻找文件
    在这里插入图片描述

2. File类常见API

2.1 判断文件类型、获取文件信息

在这里插入图片描述

/**
 目标:File类的获取功能的API
 - public String getAbsolutePath()  :返回此File的绝对路径名字符串。
 - public String getPath()  : 获取创建文件对象的时候用的路径
 - public String getName()  : 返回由此File表示的文件或目录的名称。
 - public long length()  :    返回由此File表示的文件的长度。
 */
public class FileDemo02 {
    public static void main(String[] args) {
        // 1.绝对路径创建一个文件对象
        File f1 = new File("/Users/lenyoo/Desktop/IMG_9381.jpg");
        // a.获取它的绝对路径。
        System.out.println(f1.getAbsoluteFile());
        // b.获取文件定义的时候使用的路径。
        System.out.println(f1.getPath());
        // c.获取文件的名称:带后缀。
        System.out.println(f1.getName());
        // d.获取文件的大小:字节个数。
        System.out.println(f1.length()); //字节大小
        // e.获取文件的最后修改时间
        long time = f1.lastModified();
        System.out.println("最后修改时间:" + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(time));
        // f.判断文件是文件还是文件夹
        System.out.println(f1.isFile()); // true
        System.out.println(f1.isDirectory()); // false

        System.out.println("-------------------------");

        // 1.绝对路径创建一个文件对象
        File f2 = new File("file-io-app/src/data.txt");
        // a.获取它的绝对路径。
        System.out.println(f2.getAbsoluteFile());
        // b.获取文件定义的时候使用的路径。
        System.out.println(f2.getPath());
        // c.获取文件的名称:带后缀。
        System.out.println(f2.getName());
        // d.获取文件的大小:字节个数。
        System.out.println(f2.length()); //字节大小
        // e.获取文件的最后修改时间
        long time1 = f2.lastModified();
        System.out.println("最后修改时间:" + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(time1));
        // f.判断文件是文件还是文件夹
        System.out.println(f2.isFile()); // true
        System.out.println(f2.isDirectory()); // false

        File file = new File("/Users");
        System.out.println(file.isFile()); // false
        System.out.println(file.isDirectory()); // true
        System.out.println(file.exists()); // true
    }
}

2.2 创建、删除文件功能

在这里插入图片描述
在这里插入图片描述

  • delete方法默认只能删除文件和空文件夹
  • delete方法直接删除不走回收站
/**
 目标:File类的创建和删除的方法
 - public boolean createNewFile() :当且仅当具有该名称的文件尚不存在时,
 创建一个新的空文件。 (几乎不用的,因为以后文件都是自动创建的!)
 - public boolean delete() :删除由此File表示的文件或目录。 (只能删除空目录)
 - public boolean mkdir() :创建由此File表示的目录。(只能创建一级目录)
 - public boolean mkdirs() :可以创建多级目录(建议使用的)
 */
public class FileDemo03 {
    public static void main(String[] args) throws IOException {
        File f = new File("file-io-app/src/data.txt");
        // a.创建新文件,创建成功返回true ,反之 ,不需要这个,以后文件写出去的时候都会自动创建
        System.out.println(f.createNewFile());
        File f1 = new File("file-io-app/src/data02.txt");
        System.out.println(f1.createNewFile()); // (几乎不用的,因为以后文件都是自动创建的)

        // b.mkdir创建一级目录
        File f2 = new File("/Users/lenyoo/Desktop/test/aaa");
        System.out.println(f2.mkdir());

        // c.mkdirs创建多级目录(重点)
        File f3 = new File("/Users/lenyoo/Desktop/test/ccc/ddd/eee/ffff");
        System.out.println(f3.mkdirs());

        // d.删除文件或者空文件夹
        System.out.println(f1.delete());
        File f4 = new File("/Users/lenyoo/Desktop/test/IMG_9381.jpg");
        System.out.println(f4.delete());

        // 只能删除空文件夹,不能删除非空文件夹
        File f5 = new File("/Users/lenyoo/Desktop/test/aaa");
        System.out.println(f5.delete());
    }
}

2.3 遍历文件夹

在这里插入图片描述
ListFiles方法注意事项

  • 当调用者不存在时,返回null
  • 当调用者是一个文件时,返回null
  • 当调用者是一个空文件夹时,返回一个长度为0的数组
  • 当调用者是一个有内容的文件夹时,将里面所有文件和文件夹的路径放在File数组中返回
  • 当调用者是一个有隐藏文件的文件夹时,将里面所有文件的文件夹的路径放在File数组中返回,包含隐藏内容
  • 当调用者是一个需要权限才能进入的文件夹时,返回null
/**
 目标:File针对目录的遍历
 - public String[] list():
 获取当前目录下所有的"一级文件名称"到一个字符串数组中去返回。
 - public File[] listFiles()(常用):
 获取当前目录下所有的"一级文件对象"到一个文件对象数组中去返回(重点)
 */
public class FileDemo04 {
    public static void main(String[] args) {
        // 1、定位一个目录
        File f1 = new File("/Users/lenyoo/Desktop/test");
        String[] names = f1.list();
        for (String name : names) {
            System.out.println(name);
        }
        // 2.一级文件对象
        // 获取当前目录下所有的"一级文件对象"到一个文件对象数组中去返回(重点)
        File[] files = f1.listFiles();
        for (File f : files) {
            System.out.println(f.getAbsoluteFile());
        }

        // 注意事项
        File dir = new File("/Users/lenyoo/Desktop/aaaaaaaa");
        File[] files1 = dir.listFiles();
        System.out.println(Arrays.toString(files1));
    }
}

3. 方法递归

3.1 什么是递归

  • 方法直接或间接自己调用自己的编程技巧称为递归

3.2 递归算法三要素

  • 递归的公式:f(n) = f(n-1) * n
  • 递归的终结点:f(1)
  • 递归的方向必须走向终结点

3.3 案例

3.3.1 文件搜索

在这里插入图片描述

public class RecursionDemo05 {
    public static void main(String[] args) {
        // 2、传入目录和文件名称
        searchFile(new File("/Users/lenyoo/desktop"),"wly.JPG");
    }

    /**
     * 1、搜索某个目录下的全部文件,找到我们想要的文件
     * @param dir  被搜索的源目录
     * @param fileName  被搜索的文件名称
     */
    public static void searchFile(File dir, String fileName){
        // 3、判断dir是否是目录
        if (dir != null && dir.isDirectory()){
            // 可以找了
            // 4、提取当前目录下的一级文件对象
            File[] files = dir.listFiles(); // null
            // 5、是否存在1级文件对象,存在才可以遍历
            if (files != null && files.length > 0){
                for (File file : files) {
                    // 6、判断当前遍历的一级文件对象是文件还是目录
                    if (file.isFile()){
                        // 7、是不是咱们要找的,是把其路径输出即可
                        if (file.getName().contains(fileName)){
                            System.out.println("找到了:" + file.getAbsoluteFile());
                        }
                    }else{
                        // 8、是文件夹,需要继续递归寻找
                        searchFile(file, fileName);
                    }
                }
            }
        }else{
            System.out.println("对不起,当前搜索的位置不是文件夹!");
        }
    }
}

3.3.2 啤酒问题

/**
     目标:啤酒2元1瓶,4个盖子可以换一瓶,2个空瓶可以换一瓶,
     请问10元钱可以喝多少瓶酒,剩余多少空瓶和盖子。
     答案:15瓶 3盖子 1瓶子

 */
public class RecursionDemo06 {

    // 定义一个静态的成员变量用于存储可以买的酒数量
    public static int totalNumber; // 总数量
    public static int lastBottleNumber; // 记录每次剩余的瓶子个数
    public static int lastCoverNumber; // 记录每次剩余的盖子个数

    public static void main(String[] args) {
        // 1、拿钱买酒
        buy(10);
        System.out.println("总数:" + totalNumber);
        System.out.println("剩余盖子数:" + lastCoverNumber);
        System.out.println("剩余瓶子数:" + lastBottleNumber);
    }

    public static void buy(int money){
        // 2、看可以立马买多少瓶
        int buyNumber = money / 2; // 5
        totalNumber += buyNumber;

        // 3、把盖子 和瓶子换算成钱
        // 统计本轮总的盖子数和瓶子数
        int coverNumber = lastCoverNumber + buyNumber;
        int bottleNumber = lastBottleNumber + buyNumber;

        // 统计可以换算的钱
        int allMoney = 0;
        if (coverNumber >= 4){
            allMoney += (coverNumber / 4) * 2;
        }
        lastCoverNumber = coverNumber % 4;

        if (bottleNumber >= 2){
            allMoney += (bottleNumber / 2) * 2;
        }
        lastBottleNumber = bottleNumber % 2;

        if (allMoney >= 2){
            buy(allMoney);
        }
    }
}

4. 字符集

4.1 字符串常见的字符底层组成

  • 英文和数字在任何国家的字符集中都占1个字节
  • GBK字符中一个中文字符占2个字节
  • UTF-8编码中一个中文一般占3个字节

4.2 编码前的字符集和编码后的字符集有什么要求

  • 必须一致,否则会出现中文字符乱码
  • 英文和数字在任何国家的编码中都不会乱码

4.3 字符集的编码、解码操作

/**
    目标:学会自己学会文字的编码和解码,为可能用到的场景做准备
 */
public class Test {
    public static void main(String[] args) throws UnsupportedEncodingException {
        // 1、编码:把文字转换成字节(使用指定的编码)
        String name = "abc我爱你中国";
        // byte[] bytes = name.getBytes(); // 以当前代码默认字符集进行编码(UTF-8)
        byte[] bytes = name.getBytes("GBK"); // 指定编码
        System.out.println(bytes.length);
        System.out.println(Arrays.toString(bytes));

        // 2、解码:把字节转换成对应的中文形式(编码前和编码后的字符集必须一致,否则乱码)
        // String rs = new String(bytes); //默认的UTF-8
        String rs = new String(bytes,"GBK"); //指定GBK解码
        System.out.println(rs);

    }
}

5. IO流

5.1 IO流概述

  • I表示input,是数据从硬盘文件读入到内存的过程,称之为输入,负责读
  • O表示output,是内存程序的数据从内存写出到硬盘文件的过程,称之输出,负责写
    在这里插入图片描述

5.2 文件字节输入流

5.2.1每次读取一个字节

import java.io.File;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.InputStream;

public class FileInputStreamDemo01 {
    public static void main(String[] args) throws Exception{
        // 1、创建一个文件字节输入流管道与源文件接通
        // InputStream is = new FileInputStream(new File("file-io-app/src/data.txt"));
        // 简化写法
        InputStream is = new FileInputStream("file-io-app/src/data.txt");

        // 2、读取一个字节返回(每次读取一滴水)
//        int b1 = is.read();
//        System.out.println((char) b1);
//
//        int b2 = is.read();
//        System.out.println((char) b2);
//
//        int b3 = is.read();
//        System.out.println((char) b3);
//
//        int b4 = is.read(); // 读取完毕返回-1
//        System.out.println((char) b4);

        // 3、使用循环改进
        // 定义一个变量
        int b;
        while ((b = is.read()) != -1){
            System.out.print((char) b);
        }
    }
}

5.2.2 每次读取一个字节数组

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
    目标:使用文件字节流每次读取一个字节数组的数据
 */
public class FileInputStreamDemo02 {
    public static void main(String[] args) throws Exception {
        // 1、创建一个文件字节输入流管道
        InputStream is = new FileInputStream("file-io-app/src/data02.txt");

        // 2、定义一个字节数组,用于读取字节数组
//        byte[] buffer = new byte[3]; // 3B
//        int len = is.read(buffer);
//        System.out.println("读取了几个字节:" +len);
//        String rs = new String(buffer);
//        System.out.println(rs);
//
//        int len1 = is.read(buffer);
//        System.out.println("读取了几个字节:" +len1);
//        String rs1 = new String(buffer);
//        System.out.println(rs1);
//
//        int len2 = is.read(buffer);
//        System.out.println("读取了几个字节:" +len2);
//        // 读取多少倒出多少
//        String rs2 = new String(buffer, 0, len2);
//        System.out.println(rs2);
//
//        int len3 = is.read(buffer);
//        System.out.println(len3); // 读取完毕返回-1

        // 3、改进使用循环,每次读取一个字节数组
        byte[] buffer = new byte[3];
        int len; // 记录每次读取的字节数
        while ((len = is.read(buffer)) != -1) {
            System.out.print(new String(buffer , 0, len));
        }
    }
}

5.2.3 一次读完全部字节

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 目标:使用文件字节输入流一次读完文件的全部字节。可以解决乱码问题。
 */
public class FileInputStreamDemo03 {
    public static void main(String[] args) throws Exception {
        // 1、创建一个文件字节输入流管道与源文件接通
        File f = new File("file-io-app/src/data03.txt");
        InputStream is = new FileInputStream(f);

        // 2、定义一个字节数组与文件的大小刚刚一样大。
//        byte[] buffer = new byte[(int) f.length()];
//        int len = is.read(buffer);
//        System.out.println("读取了多少个字节:" + len);
//        System.out.println("文件大小:" + f.length());
//        System.out.println(new String(buffer));

        // 读取全部字节数组
        byte[] buffer = is.readAllBytes();
        System.out.println(new String(buffer));

    }
}

5.3 文件字节输出流:写字节数据到文件

public class OutputStreamDemo04 {
    public static void main(String[] args) throws Exception {
        // 1、创建一个文件字节输出流管道与目标文件接通
        OutputStream os = new FileOutputStream("file-io-app/src/out04.txt" , true); // 追加数据管道
//        OutputStream os = new FileOutputStream("file-io-app/src/out04.txt"); // 先清空之前的数据,写新数据进入

        // 2、写数据出去
        // a.public void write(int a):写一个字节出去
        os.write('a');
        os.write(98);
        os.write("\r\n".getBytes()); // 换行
        // os.write('徐'); // [ooo]

        // b.public void write(byte[] buffer):写一个字节数组出去。
        byte[] buffer = {'a' , 97, 98, 99};
        os.write(buffer);
        os.write("\r\n".getBytes()); // 换行

        byte[] buffer2 = "我是中国人".getBytes();
//        byte[] buffer2 = "我是中国人".getBytes("GBK");
        os.write(buffer2);
        os.write("\r\n".getBytes()); // 换行


        // c. public void write(byte[] buffer , int pos , int len):写一个字节数组的一部分出去。
        byte[] buffer3 = {'a',97, 98, 99};
        os.write(buffer3, 0 , 3);
        os.write("\r\n".getBytes()); // 换行

        // os.flush(); // 写数据必须,刷新数据 可以继续使用流
        os.close(); // 释放资源,包含了刷新的!关闭后流不可以使用了
    }
}

5.4 文件拷贝

/**
 *   目标:学会使用字节流完成文件的复制(支持一切文件类型的复制)
 */
public class CopyDemo05 {
    public static void main(String[] args) {
        try {
            // 1、创建一个字节输入流管道与原文件接通
            InputStream is = new FileInputStream("file-io-app/src/out04.txt");

            // 2、创建一个字节输出流管道与目标文件接通
            OutputStream os = new FileOutputStream("file-io-app/src/out05.txt");

            // 3、定义一个字节数组转移数据
            byte[] buffer = new byte[1024];
            int len; // 记录每次读取的字节数。
            while ((len = is.read(buffer)) != -1){
                os.write(buffer, 0 , len);
            }
            System.out.println("复制完成了!");

            // 4、关闭流。
            os.close();
            is.close();
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}

5.5 资源释放的方式

5.5.1 try-catch-finally

  • finally:在异常处理时提供finally块来执行所有清除操作,比如说IO流中的释放资源
  • 特点:被finally控制的语句最终一定会执行,除非JVM退出
  • 异常处理标准格式:try…catch…finally

5.5.2 try-catch-resource

/**
 *   目标:学会使用JDK 7的新方式释放资源
 */
public class TryCatchResourceDemo2 {
    public static void main(String[] args) {

        try (
                // 这里面只能放置资源对象,用完会自动关闭:自动调用资源对象的close方法关闭资源(即使出现异常也会做关闭操作)
                // 1、创建一个字节输入流管道与原视频接通
                InputStream is = new FileInputStream("file-io-app/src/out04.txt");
                // 2、创建一个字节输出流管道与目标文件接通
                OutputStream os = new FileOutputStream("file-io-app/src/out05.txt");

                // int age = 23; // 这里只能放资源
        ) {

            // 3、定义一个字节数组转移数据
            byte[] buffer = new byte[1024];
            int len; // 记录每次读取的字节数。
            while ((len = is.read(buffer)) != -1){
                os.write(buffer, 0 , len);
            }
            System.out.println("复制完成了!");

        } catch (Exception e){
            e.printStackTrace();
        }
    }
}

5.6 文件字符输入流

5.6.1 一次读取一个字符

public class FileReaderDemo01 {
    public static void main(String[] args) throws Exception {
        // 目标:每次读取一个字符。
        // 1、创建一个字符输入流管道与源文件接通
        Reader fr = new FileReader("file-io-app/src/data06.txt");

        // 2、读取一个字符返回,没有可读的字符了返回-1
//        int code = fr.read();
//        System.out.print((char)code);
//
//        int code1 = fr.read();
//        System.out.print((char)code1);

        // 3、使用循环读取字符
        int code;
        while ((code = fr.read()) != -1){
            System.out.print((char) code);
        }
    }
}

5.6.2 一次读取一个字符数组

public class FileReaderDemo02 {
    public static void main(String[] args) throws Exception {
        // 1、创建一个文件字符输入流与源文件接通
        Reader fr = new FileReader("file-io-app/src/data07.txt");

        // 2、用循环,每次读取一个字符数组的数据。  1024 + 1024 + 8
        char[] buffer = new char[1024]; // 1K字符
        int len;
        while ((len = fr.read(buffer)) != -1) {
            String rs = new String(buffer, 0, len);
            System.out.print(rs);
        }

    }
}

5.7 文件字符输出流

public class FileWriterDemo03 {
    public static void main(String[] args) throws Exception {
        // 1、创建一个字符输出流管道与目标文件接通
        // Writer fw = new FileWriter("file-io-app/src/out08.txt"); // 覆盖管道,每次启动都会清空文件之前的数据
        Writer fw = new FileWriter("file-io-app/src/out08.txt", true); // 追加数据

//      a.public void write(int c):写一个字符出去
        fw.write(98);
        fw.write('a');
        fw.write('徐'); // 不会出问题了
        fw.write("\r\n"); // 换行

//       b.public void write(String c)写一个字符串出去
        fw.write("abc我是中国人");
        fw.write("\r\n"); // 换行


//       c.public void write(char[] buffer):写一个字符数组出去
        char[] chars = "abc我是中国人".toCharArray();
        fw.write(chars);
        fw.write("\r\n"); // 换行


//       d.public void write(String c ,int pos ,int len):写字符串的一部分出去
        fw.write("abc我是中国人", 0, 5);
        fw.write("\r\n"); // 换行


//       e.public void write(char[] buffer ,int pos ,int len):写字符数组的一部分出去
        fw.write(chars, 3, 5);
        fw.write("\r\n"); // 换行


        // fw.flush();// 刷新后流可以继续使用
        fw.close(); // 关闭包含刷线,关闭后流不能使用
    }
}

字节流、字符流如何选择使用?

  • 字节流适合做一切文件数据的拷贝(音视频,文本)
  • 字节流不适合读取中文内容输出
  • 字符流适合做文本文件的操作(读,写)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值