java使用字节流对文件进行读写操作


前言

  Java程序中,对于数据的输入/输出操作以”流(stream)” 的方式进行。java.io包下提供了各种“流”类和接口,用以获取不同种类的数据,并通过标准的方法输入或输出数据。 


一、字节流是什么?

  字节流是由字节组成的,字符流是由字符组成的. Java里字符由两个字节组成.字节流是最基本的,所有的InputStream和OutputStream的子类都是,主要用在处理二进制数据,它是按字节来处理的。通过其子类FileInputStream和FileOutputStream来实现对文件的读写。

二、代码实现

1.写文件操作

public static void out(){
        //1.确定目标文件
        File file =new File("E:\\java基础学习\\帅.txt");//如果没有该文件将自动创建,前提是上级目录存在
        //2.新建一个输出流对象
        try {
            FileOutputStream fileOutputStream =new FileOutputStream(file,false);
            //append为true表示追加
            //3.确定要输出的内容
            String info ="学java要努力\r\n";// \r\n 表示换行
            //4.向目标文件写入内容
            try {
                fileOutputStream.write(info.getBytes());
                System.out.println("写入成功");
                //5.关闭流
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }


    }

2.读文件操作

private static void in() {
        //1.确定目标文件
        File file =new File("E:\\java基础学习\\帅.txt");//文件路径写自己的
        //2.新建一个输入流对象
        //jdk新增语法,实现了closeable接口的类可以在try()中实现自动关闭
        try(
                FileInputStream fileInputStream =new FileInputStream(file)
                ) {

            byte [] bytes =new byte[1024*10];
            StringBuffer stringBuffer =new StringBuffer();
            int len =-1;//表示每次读取的字节长度
            //把数据读取到字节数组中,并且返回读取的字节数,若字节数为-1,则表示已经读完
            while((len=fileInputStream.read(bytes))!=-1){
                stringBuffer.append(new String(bytes));
            }
            //关闭流
            // fileInputStream.close();
            System.out.println(stringBuffer);
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }

目录

前言

一、字节流是什么?

二、代码实现

1.写文件操作

2.读文件操作

3.完整代码

总结


3.完整代码

package 字节流;

import java.io.*;

/**
 * 一般操作非文本文件,使用字节流
 * 输出流超类OutputStream,对文件的输入流使用子类FileOutputStream
 * 输入流超类InputStream,对文件的输入流使用子类FileInputStream
 *
 * 每次只操作一个字节
 * 默认每次执行写入操作会直接把数据写入文件
 */
public class ByteStreamDemo {
    public static void main(String[] args) {

        out();
        in();
    }
    //读文件
    private static void in() {
        //1.确定目标文件
        File file =new File("E:\\java基础学习\\帅.txt");//文件路径写自己的
        //2.新建一个输入流对象
        //jdk新增语法,实现了closeable接口的类可以在try()中实现自动关闭
        try(
                FileInputStream fileInputStream =new FileInputStream(file)
                ) {

            byte [] bytes =new byte[1024*10];
            StringBuffer stringBuffer =new StringBuffer();
            int len =-1;//表示每次读取的字节长度
            //把数据读取到字节数组中,并且返回读取的字节数,若字节数为-1,则表示已经读完
            while((len=fileInputStream.read(bytes))!=-1){
                stringBuffer.append(new String(bytes));
            }
            //关闭流
            // fileInputStream.close();
            System.out.println(stringBuffer);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //写文件
    public static void out(){
        //1.确定目标文件
        File file =new File("E:\\java基础学习\\帅.txt");//如果没有该文件将自动创建,前提是上级目录存在
        //2.新建一个输出流对象
        try {
            FileOutputStream fileOutputStream =new FileOutputStream(file,false);
            //append为true表示追加
            //3.确定要输出的内容
            String info ="学java要努力\r\n";// \r\n 表示换行
            //4.向目标文件写入内容
            try {
                fileOutputStream.write(info.getBytes());
                System.out.println("写入成功");
                //5.关闭流
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }


    }
}

总结


例如:以上就是今天要讲的内容,本文仅仅简单介绍了字节流的使用,而java的io流框架中提供了许多对字节,字符的操作方法

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值