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


相关推荐

  • Nuke Python脚本读取文件渲染所需要的分辨率

    Nuke Python脚本读取文件渲染所需要的分辨率

    2021年9月8日
    76
  • 简述Activity生命周期「建议收藏」

    简述Activity生命周期「建议收藏」Activity显示方式Android是使用任务(Task)管理活动的,一个任务就是一组存放在栈里的活动的集合,这个栈也 被称为返回栈。新活动启动进入栈,处于栈顶,当Back或finish()销毁一个活动时,栈顶的活动会出栈,前一个入栈的活动重新处于栈顶位置,显示给用户。活动状态运行状态:处于栈顶。暂停状态:不再处于栈顶但仍可见。(内存极低时系统会考虑回收这种活动)停止状态:不再处于栈顶,并且完全不可见。(系统会保存相应的状态和成员变量,但是这并不是完全可靠的,当其他地方需要内存时,处于停止状态

    2022年8月16日
    5
  • 蜗牛星际NAS安装黑群晖V6.2.1+洗白+免U盘引导启动

    蜗牛星际NAS安装黑群晖V6.2.1+洗白+免U盘引导启动推上看到有dalao分享蜗牛星际的车,作为伪“资深垃圾佬”自然不能错过,于是去咸鱼上收了一台。基本配置如下(性价比相当高):4盘位机箱+J1900处理器+4GRAM+16GMSATASSD+150W电源其中,我购买的是A款(带防尘罩),机箱背部为两个USB3.0接口与一个RJ45接口,主板为绿色,网卡inteli211,也是目前翻车最少的版本(双网口翻车严重…

    2022年6月4日
    1.1K
  • 详解网站WEB日志格式

    详解网站WEB日志格式WEB日志是网站的WEB服务处理程序,根据一定的规范生成的ASCII文本。它主要记录了网站访问记录数据内容,是网站分析和网站数据仓库的数据基础来源,而网站分析和数据分析也将对SEO产生一定的影响,所以了解WEB日志的格式和组成将有利于我们更好地进行网站数据的收集、处理和分析,从而更好的进行网站优化。一、WEB日志格式分类目前常见的WEB日志格式主要由两类,一类是Apache的NCSA日志格

    2022年5月18日
    42
  • Verilog HDL 快速入门

    VerilogHDL快速入门VerilogHDL是一种硬件描述语言(HDL:HardwareDescriptionLanguage),它是以文本形式来描述数字系统硬件的结构和行为的语言。世界上最流行的两种硬件描述语言是VerilogHDL和VHDL。注意,VerilogHDL是一种描述语言,它和常见的编程语言C有根本的不同。C语言,让计算机的CPU从上往下按顺序执行每一条指令,执行完…

    2022年4月1日
    98
  • linux怎么关闭防火墙命令,Linux怎么用命令永久关闭防火墙

    有时防火墙会限制我们下载上传操作等,而Linux操作系统想要关闭防火墙有很多命令。具体有哪些呢?下面由学习啦小编为大家整理了linux中永久关闭防火墙命令的相关知识,希望对大家有帮助!Linux永久关闭防火墙命令1)永久性生效,重启后不会复原开启:chkconfigiptableson关闭:chkconfigiptablesoff2)即时生效,重启后复原开启:serviceiptab…

    2022年4月8日
    221

发表回复

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

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