Java学习Day16 IO流基础

Java学习Day16 IO流基础

File

package com.yuhuw.basic.IO;

import org.junit.jupiter.api.Test;

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

/*
 *@author   yuhuw
 *@date     2023/10/13 13:27
 *@version  1.0
 */
/*
1.文件是保存数据的地方 也有很多格式 excel word
视频类的 mp4 rmvb
音频类的 mp3 bg
图片类的 jpg mpg
2.文件流 文件在程序中是以流的方式来操作的
java程序 内存 到文件 磁盘 输出流   因为java程序和内存对外输出
文件 磁盘 到 java程序 内存 输入流  因为文件磁盘对内输入
3.创建文件的方法和构造器
File()实现了Serializable 和 Comparable

 */
public class FileBasic {
    public static void main(String[] args) {

    }
//创建文件的三种方法
    @Test
    public void create01() throws IOException {
        String filePath = "g:\\news1.txt";//在g盘创建new1.txt
        File file = new File(filePath);
        file.createNewFile();
        System.out.println("OK");
    }

    @Test
    public void create02() {
        File parentFile = new File("g:\\");
        String fileName = "news2.txt";
        File file = new File(parentFile, fileName);

        try {
            file.createNewFile();
            System.out.println("OK");
        } catch (IOException e) {//使用抛出异常也可以
            e.printStackTrace();
        }
    }

    @Test
    public void create03() throws IOException {
//        String parentPath = "g:\\";
        String parentPath = "g:/";
        //这两种路径在java里是等效的
        String fileName = "news3.txt";
        File file = new File(parentPath, fileName);
        file.createNewFile();

    }
}

FileMethod

package com.yuhuw.basic.IO;

import org.junit.jupiter.api.Test;

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

/*
 *@author   yuhuw
 *@date     2023/10/13 13:27
 *@version  1.0
 */
/*
1.文件是保存数据的地方 也有很多格式 excel word
视频类的 mp4 rmvb
音频类的 mp3 bg
图片类的 jpg mpg
2.文件流 文件在程序中是以流的方式来操作的
java程序 内存 到文件 磁盘 输出流   因为java程序和内存对外输出
文件 磁盘 到 java程序 内存 输入流  因为文件磁盘对内输入
3.创建文件的方法和构造器
File()实现了Serializable 和 Comparable

 */
public class FileBasic {
    public static void main(String[] args) {

    }
//创建文件的三种方法
    @Test
    public void create01() throws IOException {
        String filePath = "g:\\news1.txt";//在g盘创建new1.txt
        File file = new File(filePath);
        file.createNewFile();
        System.out.println("OK");
    }

    @Test
    public void create02() {
        File parentFile = new File("g:\\");
        String fileName = "news2.txt";
        File file = new File(parentFile, fileName);

        try {
            file.createNewFile();
            System.out.println("OK");
        } catch (IOException e) {//使用抛出异常也可以
            e.printStackTrace();
        }
    }

    @Test
    public void create03() throws IOException {
//        String parentPath = "g:\\";
        String parentPath = "g:/";
        //这两种路径在java里是等效的
        String fileName = "news3.txt";
        File file = new File(parentPath, fileName);
        file.createNewFile();

    }
}

Directory

public class DirectoryOperate {
    public static void main(String[] args) {

    }

    //判断news1.txt是否存在 存在就删除
    @Test
    public void m1() {
        String filePath = "g:\\news1.txt";
        File file = new File(filePath);
        if (file.exists()) {
            if (file.delete()) {
                System.out.println("Ok");
            } else {
                System.out.println("失败了");
            }
        } else {
            System.out.println("文件不存在");
        }
    }

    //判断目录是否存在 存在就删除
    //目录同样也是文件
    @Test
    public void m2() {
        String filePath = "G:\\新建文件夹";
        File file = new File(filePath);
        if (file.exists()) {
            if (file.delete()) {
                System.out.println("Ok");
            } else {
                System.out.println("失败了");
            }
        } else {
            System.out.println("目录不存在");
        }
    }

    //创建目录
    @Test
    public void m3() {
        String directoryPath = "G:\\新建文件夹";
        File file = new File(directoryPath);
        if (file.exists()) {
            System.out.println("目录已经存在");

        } else {
            if (file.mkdirs()) {//mkdirs返回多级目录//mkdir只能一级
                System.out.println("创建成功");

            } else {
                System.out.println("创建失败");
            }
        }
    }
}

IO

/*
1.IO Input Output 用于处理数据 读写文件 网络通讯
2.对数据的操作 以'流'来处理 stream
3.java.io 提供了各种类和接口
4.input 读取外部数据 到程序 内存中
5.output 将程序 内存的数据输出的外部存储中

stream分类
数据单位不同 字节流(8bit)操作二进制文件无损操作 字符流(对应字节大小根据文件编码)效率高
流向不同 输入流 输出流
角色不同 节点流 处理流/包装流

四个关键抽象类
字节流 inputStream outputStream
字符流 Reader Writer
 */
public class IOBasic {
    public static void main(String[] args) {

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值