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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 易文档-快速编写专业漂亮的API文档,产品文档,使用手册

    易文档https://easydoc.top让您轻松编写和维护高质量的文档。从需求文档、API文档、部署文档到使用手册,多种定制文档编辑器,满足您整个开发周期需求;支持接口在线测试,一键生成mock配置。极致的编写体验,优雅的排版,让文档成为一种乐趣。查看示例文档查看使用技巧…

    2022年4月5日
    340
  • Iocomp ActiveX VCL Ultra 5.12 SP6

    Iocomp ActiveX VCL Ultra 5.12 SP6Upto63Controls,choosethepacklevelyouneed!Real-Time,High-Speedcontrols!Professionallydesignedtofunctionlikereal-worldcontrols!Built-Incustompropertyeditorsforeaseofsetup!GDIbasedgraphics!RoyaltyFreeDistribution(ExcludesHMI

    2022年7月17日
    17
  • Hadoop mapreduce过程key 和value分别存什么值

    Hadoop mapreduce过程key 和value分别存什么值Hadoop mapreduce过程key 和value分别存什么值

    2022年4月23日
    51
  • 基于Qt的音乐播放器(二)切换歌曲,调节音量,调节语速,暂停

    基于Qt的音乐播放器(二)切换歌曲,调节音量,调节语速,暂停切换歌曲,调节音量,调节语速,暂停先说一下,针对上一次的ui界面,这次做了重新设计,第一张是以前的,第二张是现在的设计,不要喷我按钮的ui,都是临时的,后面会用一种风格整体替换,我还加入了皮肤切换,不过还没有实现功能,这个ui也不是最终设计,后期还是会更新的,争取做到最好,说实话,这个设计真是让人头疼,毕竟是把美工的活抢了,哈哈,然后这个ui的设计,我们先不讲,如果需求高的话,会考虑再写一篇有关ui的,完整项目已上传github,自行下载,其他就没有了,我们赶紧进入今天的正题。

    2022年5月24日
    37
  • MySQL(数据类型和完整约束)

    MySQL数据类型MySQL支持多种数据类型,主要有数值类型、日期/时间类型和字符串类型。1.数值数据类型2.日期/时间类型3.字符串类型1.整数类型数值型数据类型主要用来存储数字,MY

    2022年3月29日
    35
  • mongobd Databus

    mongobd Databushttp://www.cnblogs.com/huangxincheng/archive/2012/02/19/2357846.html–8天学习http://www.xuebuyuan.com/554017.htmlhttp://www.habadog.com/2011/08/03/mongodb-shell-1-overview/ –mongodb与jshttp

    2022年10月16日
    1

发表回复

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

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