Java中的I/O流

目录

I/O流

输入流(FileInputStream)

字节流的读取

字符流的读取

输出流(OutputStream)

带缓冲区的输入输出流(BufferedInput/OutputStream)


I/O流

I/O流是数据传送的通道,I/O流分为输入流(Input)和输出流(Ouput)。程序从外部读取称为输入(Input),程序向外部写成为输出(Output)。

  • Java.io 包中几乎包含了所有操作 输入、输出需要的类。所有这些类代表了输入源和输出目标。
  • Java.io 包中的流支持很多种格式,比如:基本类型、对象、本地化字符集等等。

一个流可以理解为一个数据的序列。输入流表示从一个源读取数据,输出流表示向一个目标写数据。

Java 为 I/O 提供了强大的而灵活的支持,使其更广泛地应用到文件传输和网络编程中。

根据操作类型的不同可以分为:字节流 和 字符流

输入流(FileInputStream)

父类:InputStream

常用的字节输入流:FileInputStream ,继承于InputStream

File file = new File("1.txt");       
FileInputStream ff = new FileInputStream(file);

如果要读取的文件不存在,会报错: java.io.FileNotFoundException 

字节流的读取

read()方法,读取一个字节,返回该字节的值,如果到达文件末尾,则返回-1。read()方法和迭代器一样,会自动下移。

字符流的读取

read(byte[])方法,从输入流中读取至多一个数组长度的内容,如果到达文件末尾,则返回-1。read()方法和迭代器一样,会自动下移。

  • 数组称为缓冲区数组,大小一般取值为1024的整数倍。
  • 转换为字符时,使用String(byte [ ]   bytes,int offset,int length)
  • available()没有读取的剩余字节数,如果该文件还从未被读取,就返回该文件的长度。
  • close() 关闭流并释放资源
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class Main {
    public static void main(String[] args) throws IOException {
        File file = new File("1.txt");

        System.out.println("读取字节流的第一个字符:");
        FileInputStream ff = new FileInputStream(file);
        int a = ff.read();  //读取的是1.txt文件的第一个字符的ascii值
        System.out.println(a);
        ff.close();

        //字节流的循环读取
        System.out.println("读取字节流:");
        FileInputStream mm = new FileInputStream(file);
        int b;
        while( (b=mm.read())!=-1){
            System.out.print(b+"-");
        }
        System.out.println("");
        mm.close();

        //字符流的循环读取
        System.out.println("读取字符流:");
        FileInputStream nn = new FileInputStream(file);
        byte[] by = new byte[1024];
        int c;
        while((c= nn.read(by))!=-1){
            String str = new String(by,0,c);
            System.out.println(str);
        }
        nn.close();
    }
}

输出流(OutputStream)

父类:OutputStream

常用的字节输出流:FileOutputStream ,OutputStream

File file = new File("1.txt");
//覆盖
FileOutputStream ff = new FileOutputStream(file,false);
//追加
FileOutputStream ff = new FileOutputStream(file,true);

如果父目录不存在,会报FileNotFoundException异常,如果父目录存在,会创建一个新的文件,如果此时已经有文件存在,会覆盖原文件

常用方法:

//向文件中写入一个字节的值
write(int x) 
//向文件中写入一个数组的数据
write(byte[]) 
//将偏移量为offset的索引位置长度为len的数据写入文件中
write(byte[],offset,len)


//关闭该输出流
close()

示例代码

import java.io.*;
public class Main {
    public static void main(String[] args) throws IOException {
        File file = new File("1.txt");   //如果本目录下无1.txt文件,则会新建一个1.txt文件
        //覆盖
        FileOutputStream ff = new FileOutputStream(file,false);
        ff.write("hello,word!".getBytes());
        ff.close();
        //追加
        FileOutputStream fff = new FileOutputStream(file,true);
        fff.write("\n".getBytes());          //写入换行符
        fff.write("加油,中国!".getBytes());
        fff.close();

        //文件复制,将1.txt的内容复制到2.txt内容去
        FileInputStream aa = new FileInputStream(file);
        FileOutputStream bb = new FileOutputStream(new File("2.txt"));
        byte[] cc = new byte[1024];
        int count;
        while((count=aa.read(cc))!=-1){
            bb.write(cc,0,count);
        }
        aa.close();
        bb.close();
    }
}

 带缓冲区的输入输出流(BufferedInput/OutputStream)

带缓冲区的输入输出流读写文件速度快于字节流,java 上提供了专门带缓冲区的字节流,用以提高读写速度。

BufferedInputStream / BufferedOutputStream 带缓冲区的字节输入 / 输出流

代码:

import java.io.*;
public class Main {
    public static void main(String[] args) throws IOException {
        //读取
        File f = new File("1.txt");

            //读取字节
        BufferedInputStream fff = new BufferedInputStream(new FileInputStream(f));
        int b;
        while( (b=fff.read())!=-1){
            System.out.print(b+"-");
        }
        System.out.println("");
            //读取字符
        BufferedInputStream mmm = new BufferedInputStream(new FileInputStream(f));
        byte[] by = new byte[1024];
        int c;
        while((c= mmm.read(by))!=-1){
            String str = new String(by,0,c);
            System.out.println(str);
        }

        //写入字符
        File f2 = new File("2.txt");

        BufferedOutputStream nnn = new BufferedOutputStream(new FileOutputStream(f2,false));
        nnn.write("hello,word".getBytes());
        nnn.close();
        BufferedOutputStream ppp = new BufferedOutputStream(new FileOutputStream(f2,true));
        ppp.write("\n".getBytes());          //写入换行符
        ppp.write("加油,中国!".getBytes());
        ppp.close();

        //文件复制
        BufferedInputStream  rrr = new BufferedInputStream(new FileInputStream(f2));
        BufferedOutputStream qqq = new BufferedOutputStream(new FileOutputStream(new File("3.txt")));
        byte[] cc = new byte[1024];
        int count;
        while((count=rrr.read(cc))!=-1){
            qqq.write(cc,0,count);
        }
        rrr.close();
        qqq.close();
    }
}

参考文章:JAVA中IO流详解

相关文章:Java文件系统操作

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

谢公子

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

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

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

打赏作者

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

抵扣说明:

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

余额充值