Java输入输出流

Java中的字节输入输出流

一、输入输出流

在这里,文件抽象成管道对象,文件中的数据抽象成水流对象,根据分类,分为字节输入输出流(能处理各种文件),字符输入输出流(只能处理文本文件)。

1.输入流

inputstream:从电脑硬盘上的文件读取数据到电脑内存中。

2.输出流

outputstream:将电脑上内存数据写入到电脑硬盘的文件。

关系如图所示:
在这里插入图片描述

二、字节输入输出流

在这里,有两个小案例演示字节输入流FileInputStream、字节输出流FileOutputStream用法。

1.FileInputStream案例

这里将从电脑硬盘上的poets.txt读取内容并打印输出

package exercise;
import javax.imageio.IIOException;
import java.io.*;
import java.util.*;

public class One {
    public static void main(String args[]){
        FileInputStream file=null;//先初始化为空

        try{
            file=new FileInputStream("C:\\Users\\lenovo\\Desktop\\java笔记\\poet.txt");
            //返回读取文件字节数
            int readByte=file.read();
            System.out.println("该文件字节数为:"+readByte);
            System.out.println("\n");
            //建立字节数组,并将文件内容返回数组和转成字符串
            byte[] bytes = new byte[file.available()];
            int readcount= file.read(bytes);
            System.out.println(new String(bytes));
        }
        catch (FileNotFoundException e){ e.printStackTrace(); }
        catch (IOException e){ e.printStackTrace(); }
        //最后关闭文件
        finally {
            if(file!=null){
                    try { file.close(); }
                    catch (IOException e) { e.printStackTrace(); }
            }
        }
    }
}

输出结果:
在这里插入图片描述

2.FileOutputStream案例

这里将将字符串写入电脑硬盘上的new.txt中

package exercise;
import java.io.*;
import java.nio.charset.StandardCharsets;

public class Two {
    public static void main(String args[]){
        FileOutputStream file=null;//先初始化为空

        try{//如果目标文件不存在,自动创建
            //file=new OutputStream("C:\\Users\\lenovo\\Desktop\\java笔记\\new.txt");这样会清空文件在写入
            file=new FileOutputStream("C:\\Users\\lenovo\\Desktop\\java笔记\\new.txt",true);//可追加写入
            //写入内容
            String poets="  静夜思  \n床前明月光,\n疑似地上霜。\n举头望明月,\n低头思故乡。\n";
            //字符串转bytes数组
            byte[] bytes=poets.getBytes(StandardCharsets.UTF_8);
            file.write(bytes);
            //写完刷新文件
            file.flush();
            System.out.println("成功写入文件!");
        }
        catch (FileNotFoundException e){ e.printStackTrace(); }
        catch (IOException e){ e.printStackTrace(); }
        //最后关闭文件
        finally {
            if(file!=null){
                try { file.close(); }
                catch (IOException e) { e.printStackTrace(); }
            }
        }
    }
}

输出结果:
在这里插入图片描述

三、字符输入输出流

在这里,通过改写前面的代码,实现相同功能

1.FileReader案例
package exercise;
import javax.imageio.IIOException;
import java.io.*;
import java.util.*;

public class Three {
    public static void main(String args[]){
        FileReader reader=null;//先初始化为空

        try{
            reader=new FileReader("C:\\Users\\lenovo\\Desktop\\java笔记\\poet.txt");
            //一次读取的字节数
            char[] chars=new char[4];
            //当读完时返回-1
            int readCount=0;
            while((readCount=reader.read(chars))!=-1){
                //避免换行
                System.out.print(new String(chars,0,readCount));
            }
        }
        catch (FileNotFoundException e){ e.printStackTrace(); }
        catch (IOException e){ e.printStackTrace(); }
        finally {
            if(reader!=null){
                try { reader.close(); }
                catch (IOException e) { e.printStackTrace(); }
            }
        }
    }
}

输出结果同inputstream案例。

2.FileWriter案例
package exercise;
import javax.imageio.IIOException;
import java.io.*;

public class Four {
    public static void main(String args[]){
        FileWriter writer=null;//先初始化为空

        try{
            //如果目标文件不存在,自动创建
            writer=new FileWriter("C:\\Users\\lenovo\\Desktop\\java笔记\\new1.txt",true);//可追加写入
            //写入内容
            String poets="  静夜思  \n床前明月光,\n疑似地上霜。\n举头望明月,\n低头思故乡。\n";
            writer.write(poets);
            System.out.println("成功写入文件!");
        }
        catch (FileNotFoundException e){ e.printStackTrace(); }
        catch (IOException e){ e.printStackTrace(); }
        finally {
            if(writer!=null){
                try { writer.close(); }
                catch (IOException e) { e.printStackTrace(); }
            }
        }
    }
}

输出结果同outputstream案例

四、缓冲流

1.缓冲流

与前面的流不同,缓冲流是包装流,需要建立前面的输入输出流作为对象作为实例缓冲流的参数,读取缓冲流还有按行读取readline(),写入缓冲流按行写入的方法newline()。

2.缓冲流使用案例

在《蜀道难》文章中每行前加行号

package exercise;
import java.util.*;
import java.io.*;

public class Five {
    public static void main(String args[]) {
        FileReader reads = null;
        BufferedReader reader = null;//读取器
        FileWriter writes = null;
        BufferedWriter writer = null;//编写器

        try {
            reads = new FileReader("C:\\Users\\lenovo\\Desktop\\java笔记\\poet.txt");
            reader = new BufferedReader(reads);

            writes = new FileWriter("C:\\Users\\lenovo\\Desktop\\java笔记\\poet_too.txt");
            writer = new BufferedWriter(writes);

            for (int i = 1; true; i++) {
                //按行读取
                //到达文件末位返回null
                if (reader.readLine() == null) break;
                String readData = reader.readLine();
                String writeData = i + " " + readData;
                writer.write(writeData);
                writer.newLine();
                writer.flush();
            }
            System.out.println("成功写入文件!");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

输出结果:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

有几何

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

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

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

打赏作者

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

抵扣说明:

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

余额充值