【java学习之路】(java SE篇)009.IO

本文深入讲解Java中的IO流概念及其实现方式,包括文件操作、字节流与字符流的读写方法,以及如何处理不同类型的文件如文本、图片和视频。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

IO

File类

什么是文件?

​ 相关记录或放在一起的数据的集合

构造方法

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

/*
    File类提供了对当前文件系统中文件的部分操作
 */
public class FileDemo {
    public static void main(String[] args) throws IOException {
        File file = new File("src/abc.txt");

        //创建文件
        try {//IO部分的异常都进行捕获,而不是抛出
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //判断文件的属性,都会返回boolean类型的值
        file.canExecute();
        file.canRead();
        file.canWrite();

        //判断当前文件是否存在
        System.out.println(file.exists());

        //获取文件的名称
        System.out.println(file.getName());
        //获取文件的绝对路径
        System.out.println(file.getAbsolutePath());

        //获取文件的父路径名称,如果文件的路径中只包含文件名称,则显示为空
        //无论对当前文件是否存在,只要给定具体的路径,都可以返回相应的路径名称
            File file1 = new File("d:/java/java/java.java");
            System.out.println(file1.getAbsolutePath());//d:\java\java\java.java

        System.out.println(file.getParent());
        //返回文件绝对路径的规范形式
        System.out.println(file.getCanonicalPath());
        //返回操作系统的文件分割符
        System.out.println(File.separator);
        //判断此路径名表示的文件是否为目录
        System.out.println(file.isDirectory());
        //判断此路径名表示的文件是否为文件
        System.out.println(file.isFile());

        //打印当前文件系统的所有盘符
        File[] files = File.listRoots();
        for(int i = 0;i<files.length;i++){
            System.out.println(files[i]);//C:\  D:\
        }

        //创建单级目录
//        file1.mkdir();
        //创建多级目录
//        file1.mkdirs();

        //循环遍历输出C盘中所有文件的相对路径(提示:递归)
        printFile(new File("D:\\ideaProject"));
    }

    /**
     * 文件在遍历的时候也可能会出现空指针问题,,原因在于当前文件系统受保护,某些文件没有访问权限,此时会报空指针异常
     * @param file
     */
    public static void printFile(File file){
        if(file.isDirectory()){
            File[] files = file.listFiles();//listFiles:返回一个抽象路径名数组,表示由该抽象路径名表示的目录中的文件
            for(File f : files){
                printFile(f);
            }
        }else {
//            System.out.println("此文件是一个具体的文件,只有一个文件名称");
            System.out.println(file.getAbsolutePath());
        }
    }
}

/*
在java中需要读写文件中的数据的话,需要用到流的概念
    流表示从一个文件将数据发送到另一个文件
        流向如何确定?  选择一个参照物:当前程序
            从一个文件中读取数据到程序叫输入流
            从程序输出数据到另一个文件叫输出流
*/

输入流

输出流

java流的分类

字节流读取和写出

字节流读取

InputStream抽象类

此抽象类是表示字节输入流的所有类的超类/抽象类

常用方法
abstract int read() 从输入流中读取数据的下一个字节
int read(byte[] b) 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中
int read(byte[] b, int off, int len) 将输入流中最多 len 个数据字节读入 byte 数组,off表示存时的偏移量
void close() 关闭此输入流并释放与该流关联的所有系统资源

FileInputStream子类

直接插在文件上,直接读取文件数据

创建对象

FileInputStream(File file)—直接传文件对象
通过打开一个到实际文件的连接来创建一个 FileInputStream, 该文件通过文件系统中的 File 对象 file 指定FileInputStream(String pathname)—传路径
通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定

BufferedInputStream子类

BufferedInputStream 为另一个输入流添加一些功能,在创建BufferedInputStream 时,会创建一个内部缓冲区数组(默认8k大小)。在读取或跳过流中的字节时,可根据需要从包含的输入流再次填充该内部缓冲区,一次填充多个字节。

创建对象
BufferedInputStream(InputStream in)
创建一个 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。

字节流写出

OutputStream抽象类

此抽象类是表示输出字节流的所有类的超类.输出流接受输出字节并将这些字节发送到某个接收器.

常用方法:
​ Void close() 关闭此输出流并释放与此流相关的所有系统资源
​ Void flush() 刷新此输出流并强制写出所有缓冲的输出字节
​ Void write(byte[ ] b) 将b.length个字节从指定的byte数组写入此输出流
​ Void write(byte[ ] b,int off ,int len) 将指定byte数组中从偏移量off开始的len个字节写入输出流
​ Abstract void write(int b) 将指定的字节写入此输出流

FileOutputStream 子类
直接插在文件上,直接写出文件数据

构造方法(创建对象):
​ FileOutputStream(String name)
​ 创建一个向具有指定名称的文件中写入数据的文件输出流
​ FileOutStream(File file)
​ 创建一个向指定File对象表示的文件中写入数据的文件输出流
​ FileOutStream(File file,boolean append)—如果第二个参数为true,表示追加,不覆盖
​ 创建一个向指定File对象表示的文件中写入数据的文件输出流,后面的参数是指是否覆盖原文件内容

BufferedOutputstream 子类
该类实现缓冲的输出流,通过设置这种输出流,应用程序就可以将各个字节写入底层输出流中,而不必每次针对字节写出调用底层系统

构造方法(创建对象):
​ BufferedOutputStream(OutputStream out)
​ 创建一个新的缓冲输出流,用以将数据写入指定的底层输出流

用InputStream从输入流读取数据

1.按字节读

package Stream;

import java.io.*;

/*
    注意当编写IO流的程序的时候,一定要注意关闭流

    步骤:
        1.选择合适的io流对象
        2.创建对象
        3.传输数据
        4.关闭流对象(会占用系统资源)
 */
public class StreamDemo {

    public static void main(String[] args) {
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream("src/abc.txt");
            int read = inputStream.read();//从输入流读取数据的下一个字节
            System.out.println((char)read);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                inputStream.close();//关闭流对象
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

2.通过循环读取全部单个字节

inputStream = new FileInputStream("src/abc.txt");

int read = 0;//循环输出对应文件所有的字节
while ((read = inputStream.read())!=-1){
//从输入流读取数据的下一个字节。值字节被返回作为int范围0至255。如果没有字节可用,因为已经到达流的末尾,则返回值-1 。
    System.out.println((char)read);
}

3.通过缓冲区一次性读取全部数据

inputStream = new FileInputStream("src/abc.txt");

int length = 0;//循环输出对应文件所有的字节
byte[] buffer = new byte[1024];//缓冲区
while ((length = inputStream.read(buffer))!=-1){
//从输入流读取字节的数据到一个字节数组(缓冲区)。   实际读取的字节数作为整数返回
    //添加缓冲区的方法进行读取,每次会将数据添加到缓冲区中,当缓冲区满了之后,一次读取,而不是按每一个字节进行读取
    System.out.println(new String(buffer,0,length));

4.通过缓冲区每次输出指定长度字符直至读完

inputStream = new FileInputStream("src/abc.txt");

int length = 0;//循环输出对应文件所有的字节
byte[] buffer = new byte[1024];//缓冲区
while ((length = inputStream.read(buffer,5,5))!=-1){//off:第一个字节读取存储在元素b[off]
//从输入流读取字节的数据到一个字节数组(缓冲区)。   实际读取的字节数作为整数返回
    //添加缓冲区的方法进行读取,每次会将数据添加到缓冲区中,当缓冲区满了之后,一次读取,而不是按每一个字节进行读取
    System.out.println(new String(buffer,5,length));
OutputStream向文件中写入数据
package Stream;

import java.io.*;

public class OutputStreamDemo {

    public static void main(String[] args) {
        File file = new File("src/aaa.txt");
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(file);
            outputStream.write(99);
            outputStream.write("\r\njava".getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
从一个文件中向另一个文件中写入数据
package Stream;

import java.io.*;

public class CopyFile {

    public static void main(String[] args) {
        //定义源文件
        File src = new File("src/abc.txt");
        //定义目的数据文件
        File dest = new File("src/aaa.txt");
        //创建输入流对象
        InputStream inputStream = null;
        //创建输出流对象
        OutputStream outputStream = null;

        try {
            inputStream = new FileInputStream(src);
            outputStream = new FileOutputStream(dest);

            //带缓冲的输入输出
            byte[] buffer = new byte[1024];
            int length = 0;

            while ((length=inputStream.read(buffer))!=-1){
                outputStream.write(buffer,0,length);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

字符流读取和写出

字符流读取

常用于处理纯文本数据,读写容易出现乱码的现象,在读写时,最好指定编码集为UTF-8

Reader

用于读取字符流的抽象类

​ 常用方法:
​ int read() 读取单个字符
​ int read(char[] cbuf) 将字符读入数组
​ abstract int read(char[] cbuf, int off, int len) 将字符读入数组的某一部分
​ int read(CharBuffer target) 试图将字符读入指定的字符缓冲区
​ abstract void close() 关闭该流并释放与之关联的所有资源

FileReader子类

用来读取字符文件的便捷类。此类的构造方法假定默认字符编码和默认字节缓冲区大小都是适当的。要自己指定这些值,可以先在 FileInputStream 上构造一个 InputStreamReader。

创建对象
FileReader(String fileName) 在给定从中读取数据的文件名的情况下创建一个新 FileReader
FileReader(File file) 在给定从中读取数据的 File 的情况下创建一个新 FileReader

BufferedReader子类

从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
可以指定缓冲区的大小,或者可使用默认的大小。大多数情况下,默认值就足够大了。

创建对象
BufferedReader(Reader in) 创建一个使用默认大小输入缓冲区的缓冲字符输入流

字符流写出

Writer 抽象类
写入字符流的抽象类

常用方法:
​ Abstract void close() 关闭此流,但要先刷新它
​ Void write(char[ ] cbuf) 写入字符数组
​ Void write(int c) 写入单个字符
​ Void write(String str) 写入字符串
​ Void write(String str,int off,int len) 写入字符串的某一部分
​ Abstract void write(char[] cbuf,int off,int len)写入字符数组的某一部分

FileWriter 子类
用来写入字符文件的便捷类,此类的构造方法假定默认字符编码和默认字节缓冲区大小都是可接受的.如果需要自己自定义这些值,可以先在FileOutputStream上构造一个OutputStreamWriter.

构造方法(创建对象):
​ FileWriter(String filename)
​ 根据给定的文件名构造一个FileWriter对象
​ FileWriter(String filename,boolean append)
​ 根据给定的文件名以及指示是否附加写入数据的boolean值来构造FileWriter

BufferedWriter子类
将文本写入字符输出流,缓冲各个字符,从而提供单个字符,数组和字符串的高效写入.可以指定缓冲区的大小,或者接受默认的大小,在大多数情况下,默认值就足够大了

构造方法(创建对象):
​ BufferedWriter(Writer out)
​ 创建一个使用默认大小输出缓冲区的缓冲字符输出流

用Reader从输入流读取数据

1.按字符读

package ReaderOrWriter;
/*
    字符流可以直接读取中文汉字,而字节流在读取中文汉字的时候会出现乱码
 */
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class ReaderDemo {

    public static void main(String[] args) {
        Reader reader = null;
        try {
            reader = new FileReader("src/aaa.txt");
            int read = reader.read();
            System.out.println((char)read);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

2.通过循环读取全部单个字符

package ReaderOrWriter;
/*
    字符流可以直接读取中文汉字,而字节流在读取中文汉字的时候会出现乱码
 */
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class ReaderDemo {

    public static void main(String[] args) {
        Reader reader = null;
        try {
            reader = new FileReader("src/aaa.txt");
            int read = 0;
            while ((read = reader.read())!=-1){
                System.out.println((char)read);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

3.通过缓冲区一次性读取全部数据

package ReaderOrWriter;
/*
    字符流可以直接读取中文汉字,而字节流在读取中文汉字的时候会出现乱码
 */

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class ReaderDemo {

    public static void main(String[] args) {
        Reader reader = null;
        try {
            reader = new FileReader("src/aaa.txt");
            int length = 0;
            char[] ch = new char[1024];
            while ((length = reader.read(ch))!=-1){
                System.out.println(new String(ch,0,length));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

用Writer向另一个文件中写入数据
package ReaderOrWriter;
/*
    什么时候用flash:
        最保险的方式,每次在关闭流之前,都flush一下
 */
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class WriteDemo {

    public static void main(String[] args) {
        File file = new File("src/write.txt");
        Writer writer = null;
        try {
            writer = new FileWriter(file);
            writer.write("www.baidu.com");
            writer.write("中文");
//            writer.flush();//j=将缓冲区中的数据强制刷写到磁盘
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

关于处理图片、视频等文件问题
package ReaderOrWriter;
/*
    在处理纯文本的时候,用字符还是字节都可以
    但是在处理图片,视频的时候,要使用字节来处理,即FileInputStream和FileOutputStream
    
    字节流是万能的
 */
import java.io.*;

public class CopyP {

    public static void main(String[] args) {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;

        try {
            fileInputStream = new FileInputStream("src/1.jpg");
            fileOutputStream = new FileOutputStream("src/2.jpg");

            int length = 0;
            byte[] bytes = new byte[1024];

            while ((length = fileInputStream.read(bytes))!=-1){
                fileOutputStream.write(bytes);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

附:笔记理论部分引用自程序媛 泡泡 新手小白学JAVA IO流 File 字节流 字符流
仅供学习交流使用!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值