栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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要努力rn";// rn 表示换行
            //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.*;


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要努力rn";// rn 表示换行
            //4.向目标文件写入内容
            try {
                fileOutputStream.write(info.getBytes());
                System.out.println("写入成功");
                //5.关闭流
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }


    }
}

总结


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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1032737.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号