Java之I/O 流学习笔记

本文详细介绍了Java中文件操作的基本方法,包括文件流的概念、创建文件的三种方式、获取文件信息、删除文件及目录操作。此外,还探讨了I/O流的基本原理,如字节流和字符流的分类,以及如何使用字节流和字符流读写文件。最后,展示了文件拷贝的实现代码,强调了效率问题。
摘要由CSDN通过智能技术生成

学习大纲:

文件流:

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

**流:**数据在数据源(文件)和程序(内存)之间经历的路径
**输入流:**数据从数据源(文件)到程序(内存)的路径
**输出流:**数据从程序(内存)到数据源(文件)的路径

创建文件对象相关的方法:

new File(String pathname) //根据路径构建一个File对象
new File(File parent,String child) //根据父目录文件+子路径
new File(String parent,String child) //根据父目录+子路径构建

创建文件:

    //方式1:new File(String pathname)
    @Test
    public void CreatFile01() throws IOException {
        String filePath="D:\\new1.txt";
        File file = new File(filePath);
        file.createNewFile();
    }
    //方式2:new File(File parent,String child)
    @Test
    public void CreatFile02() throws IOException {
        File parentfile = new File("D:\\");
        String filename="new2.txt";
        File file = new File(parentfile, filename);
        file.createNewFile();
    }
    //方式3:new File(String parent,String child)
    @Test
    public void CreatFile03() throws IOException {
        String parentPath = "D:\\";
        String fileName = "new3.txt";
        File file = new File(parentPath, fileName);
        file.createNewFile();

    }

常用的文件操作:

获取文件信息:

//获取文件信息
    @Test
    public void info() {
        //创建文件
        File file = new File("D:\\new1.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());
    }

image.png

删除文件:

判断D:\new1.txt文件是否存在,如果在则删除文件:

    //判断D:\new1.txt文件是否存在,如果在则删除文件
    @Test
    public void m1() {
        File file = new File("D:\\new1.txt");
        if (file.exists()) {
            if (file.delete()) {
                System.out.println(file.getAbsolutePath()+"删除成功!");
            }else {
                System.out.println(file.getAbsolutePath()+"删除失败!");
            }
        } else {
            System.out.println("文件不存在!");
        }
    }
}

目录操作:

判断D:\demo目录是否存在,如果在则删除文件

    //判断D:\demo目录是否存在,如果在则删除文件
    @Test
    public void m2() {
        File file = new File("D:\\demo");
        if (file.exists()) {
            if (file.delete()) {
                System.out.println(file.getAbsolutePath()+"删除成功!");
            }else {
                System.out.println(file.getAbsolutePath()+"删除失败!");
            }
        } else {
            System.out.println("目录不存在!");
        }
    }

创建目录:

mkdir:创建单个文件夹
mkdirs:创建多个文件夹

    //创建目录
    @Test
    public void m3() {
        File file = new File("D:\\demo1\\a\\b\\c");
        if (file.mkdirs()){
            System.out.println(file.getAbsolutePath()+"创建成功!");
        }
    }

I/O流原理:

I/O是Input/Output的缩写,I/O技术是非常实用的技术,用于处理设备之间的数据传输,如:读写文件,网络通讯等。
Java程序中,对于数据的输入/输出操作以流Stream的方式进行,java.io包下提供了各种流类和接口,用以获取不同类型的数据,并通过标准的方式输入或输出数据。
输入(Input):以内存为中心,读取外部的数据(磁盘,光盘等存储设备中的数据)到程序(内存)中。
输出(Output):以内存为中心,将程序(内存)中的数据输出到磁盘,光盘等存储设备中。
输入流:文件-------------->>>程序(内存)
输出流:文件<<<--------------程序(内存)
字符流不能处理图片文件等字节文件,处理图片,视频等字节文件需要输入输出字节流来处理。
字节流可以复制文本文件,但是字符流不能复制非文本文件。

I/O流分类:

1: 按照操作数据单位的不同分为:字节流(8bit){图片,视频等}byte, 字符流(16bit){文本等}char。
2: 根据流的流向不同分为:输入流,输出流。
3: 按流的角色不同分为:节点流,处理流。
节点流:可以从或向一个特定的地方(地点)读取数据。
处理流:是一个一存在的流的连接或封装,通过所封装的流的功能调用实现数据调用。

抽象基类--------字节流-----------字符流
输入流--------InputStream------Reader
输出流-------OutputStream------Writer

(抽象基类)字节流字符流
输入流InputStreamReader
输出流OutputStreamWriter

InputStream:字节输入流

image.png

字节流读取文件:

缺点:效率低

    @Test
    public void readFile1() throws IOException {
        int readData = 0;
        //创建FileInputStream对象读取文件
        FileInputStream fileInputStream = new FileInputStream("D:\\new1.txt");
        while (readData != -1) {
            readData = fileInputStream.read();
            System.out.print((char) readData);
        }
        //关闭资源
        fileInputStream.close();
    }

字符流读取文件:

    @Test
    public void readFile2() throws IOException {
        int readData = 0;
        //定义一个字节数组,一次取八个字节
        byte[] buf=new byte[8];
        //创建FileInputStream对象读取文件
        FileInputStream fileInputStream = new FileInputStream("D:\\new1.txt");
        //从该输入流读取最多b.length字节的数据到字节数组。此方法将阻塞,直到某些输入可用。
        while (fileInputStream.read(buf) != -1) {
            System.out.print(new String(buf,0,fileInputStream.read(buf)));
        }
        //关闭资源
        fileInputStream.close();
    }

InputStream:字节输出流

image.png

写入文件内容:

    @Test
    public void writeFile() throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream("D:\\new1.txt");
        //写入一个字节
        fileOutputStream.write('H');
        //写入字符串
        String str="hello,word";
        fileOutputStream.write(str.getBytes());

        fileOutputStream.close();
    }

image.png

实现文件的拷贝:

import java.io.InputStream;
import java.io.OutputStream;

public class File_copy {
    public static void main(String[] args) throws Exception {
        InputStream in =new FileInputStream("D:\\img.jpg");
        OutputStream out=new FileOutputStream("D:\\img1.jpg");
        int len;	//定义整型变量len,记住每次读取的一个字节
        long begintime=System.currentTimeMillis();	//获取拷贝文件前的系统时间
        while((len=in.read())!=-1) {	//读取一个字节,并判断是否读到文件末尾
            out.write(len);	//将读到的字节写入文件
        }
        long endtime=System.currentTimeMillis();	//获取文件拷贝结束时的系统时间
        System.out.println("文件拷贝所消耗的时间是:"+(endtime-begintime)+"毫秒");
        in.close();
        out.close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小秋LY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值