Java文件读写操作

Java文件读写操作Java中I/O流对文件的读写有很多种方法,在这里我主要介绍三种方式,供大家参考。第一种方式:使用FileWriter和FileReader,对文件内容按字符读取,代码如下Stringdir="E:\\soft\\aaa\\a.txt";Filefile=newFile(dir);//如果文件不存在,创建文件if(!file.exists())file.c…

大家好,又见面了,我是你们的朋友全栈君。

Java中I/O流对文件的读写有很多种方法,在这里我主要介绍三种方式,供大家参考。

第一种方式:使用FileWriter和FileReader,对文件内容按字符读取,代码如下

String dir = "E:\\soft\\aaa\\a.txt";
File file = new File(dir);
//如果文件不存在,创建文件
if (!file.exists()) 
    file.createNewFile();
//创建FileWriter对象
FileWriter writer = new FileWriter(file);
//向文件中写入内容
writer.write("the first way to write and read");
writer.flush();
writer.close();

//创建FileReader对象,读取文件中的内容
FileReader reader = new FileReader(file);
char[] ch = new char[100];
reader.read(ch);
for(char c:ch) {
    System.out.print(c);
}
System.out.println();
reader.close();

第二种方式:使用包装类BuffredReader和BufferedWriter,对文件内容进行整行读取,代码如下

String dir = "E:\\soft\\aaa\\b.txt";
File file = new File(dir);
//如果文件不存在,创建文件
if (!file.exists()) 
    file.createNewFile();
//创建BufferedWriter对象并向文件写入内容
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
//向文件中写入内容
bw.write("the second way to write and read");
bw.flush();
bw.close();
//创建BufferedReader读取文件内容
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line=br.readLine())!=null) {
    System.out.println(line);
}
br.close();

第三种方式:使用FileInputStream和FileOutputStream,这种方法以字节的形式写入文件,读取文件时先读取字节数组,再将字节数组转换为字符串形式,代码如下:

String dir = "E:\\soft\\aaa\\c.txt";
File file = new File(dir);
//如果文件不存在,创建文件
if (!file.exists()) 
    file.createNewFile();
//创建FileOutputStream对象,写入内容
FileOutputStream fos = new FileOutputStream(file);
//向文件中写入内容
fos.write("the third way to write and read".getBytes());
fos.close();
//创建FileInputStream对象,读取文件内容
FileInputStream fis = new FileInputStream(file);
byte[] bys = new byte[100];
while (fis.read(bys, 0, bys.length)!=-1) {
    //将字节数组转换为字符串
    System.out.println(new String(bys));
}
fis.close();

类中的整体代码:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileRW { 
   

    //第一种方式:使用FileWriter和FileReader
    public void firstWay() throws IOException {
        String dir = "E:\\soft\\aaa\\a.txt";
        File file = new File(dir);
        //如果文件不存在,创建文件
        if (!file.exists()) 
            file.createNewFile();
        //创建FileWriter对象
        FileWriter writer = new FileWriter(file);
        //向文件中写入内容
        writer.write("the first way to write and read");
        writer.flush();
        writer.close();

        //创建FileReader对象,读取文件中的内容
        FileReader reader = new FileReader(file);
        char[] ch = new char[100];
        reader.read(ch);
        for(char c:ch) {
            System.out.print(c);
        }
        System.out.println();
        reader.close();
    }

    //第二种方式:使用BuffredReader和BufferedWriter
    public void secondWay() throws IOException {
        String dir = "E:\\soft\\aaa\\b.txt";
        File file = new File(dir);
        //如果文件不存在,创建文件
        if (!file.exists()) 
            file.createNewFile();
        //创建BufferedWriter对象并向文件写入内容
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        //向文件中写入内容
        bw.write("the second way to write and read");
        bw.flush();
        bw.close();
        //创建BufferedReader读取文件内容
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        while ((line=br.readLine())!=null) {
            System.out.println(line);
        }
        br.close();
    }

    //第三种方式:使用FileInputStream和FileOutputStream
    public void thirdWay() throws IOException {
        String dir = "E:\\soft\\aaa\\c.txt";
        File file = new File(dir);
        //如果文件不存在,创建文件
        if (!file.exists()) 
            file.createNewFile();
        //创建FileOutputStream对象,写入内容
        FileOutputStream fos = new FileOutputStream(file);
        //向文件中写入内容
        fos.write("the third way to write and read".getBytes());
        fos.close();
        //创建FileInputStream对象,读取文件内容
        FileInputStream fis = new FileInputStream(file);
        byte[] bys = new byte[100];
        while (fis.read(bys, 0, bys.length)!=-1) {
            //将字节数组转换为字符串
            System.out.println(new String(bys));
        }
        fis.close();
    }

    public static void main(String[] args) throws IOException {
        FileRW fileRW = new FileRW();
        fileRW.firstWay();
        fileRW.secondWay();
        fileRW.thirdWay();
    }
}

运行结果如下:

the first way to write and read
the second way to write and read
the third way to write and read

希望对大家有帮助,有什么问题欢迎提问

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/159368.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • hibernate 未明确定义列 小记[通俗易懂]

    hibernate 未明确定义列 小记[通俗易懂] 在写关联表的实体类时,用测试代码去运行,出现16:00:30,817ERRORJDBCExceptionReporter:72-ORA-00918:未明确定义列16:00:30,833ERRORPersistSpringImpl:244-[PersistImpl][find(queryString,offset,length)]hql:fromcom.esse.pro

    2022年9月1日
    5
  • 深入理解机器学习——基于决策树的模型(一):分类树和回归树

    深入理解机器学习——基于决策树的模型(一):分类树和回归树决策树(DecisionTree)是一种基本的分类与回归方法。本文会讨论决策树中的分类树与回归树,后续文章会继续讨论决策树的Boosting和Bagging的相关方法。决策树由结点和有向边组成。结点有两种类型:内部结点和叶结点,其中内部结点表示一个特征或属性,叶结点表示一个类。分类树分类树是一种描述对实例进行分类的树形结构。用决策树分类,从根结点开始,对实例的某一特征进行测试,根据测…

    2022年6月23日
    42
  • python生兔子问题(递归算法)_用python函数写斐波那契数列

    python生兔子问题(递归算法)_用python函数写斐波那契数列兔子产子1.问题描述有一对兔子,从出生后的第3个月起每个月都生一对兔子。小兔子长到第3个月后每个月又生一对兔子,假设所有的兔子都不死,问30个月内每个月的兔子总对数为多少?2.问题分析兔子产子

    2022年7月31日
    6
  • 深度学习之GAN对抗神经网络

    深度学习之GAN对抗神经网络1、结构图2、知识点3、代码及案例#coding:utf-8###对抗生成网络案例#####<imgsrc="jpg/3.png"alt=&qu

    2022年8月4日
    3
  • vue父子组件传值方法_vue父组件向子组件传递对象

    vue父子组件传值方法_vue父组件向子组件传递对象前言在业务场景中经常会遇到子组件向父组件传递数值,或是父组件向子组件传递数值,下面将结合vue富文本框一起来了解一下父与子组件之间的传值业务场景在vue项目中创建了一个可以重复使用的富文本编辑器(可参考【vue】vue富文本编辑器(可重复使用组件)vue-quill-editor),由于是新闻编辑页面,首先需要把已经保存好的新闻内容展示在富文本编辑器中(父组件向子组件传值),其次需要把更新后的新闻内容保存到数据库中(子组件向父组件传值)父组件向子组件传值(v-bind:child.

    2025年9月14日
    6
  • Java中常用的API[通俗易懂]

    Java中常用的API[通俗易懂]1.Calendar类(日期与时间处理)使用情况:publicstaticvoidmain(String[]args){Calendarc=Calendar.getInstance();intyear=c.get(Calendar.YEAR);intmonth=c.get(Calendar.MONTH)+1;intday=c.get(Calendar.DATE);intweek

    2022年7月8日
    23

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号