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


相关推荐

  • StretchDIBits函数显示图片

    StretchDIBits函数显示图片注:转载请注明出处。函数原型intStretchDIBits(HDChdc,intXDest,intYDest,intnDestWidth,intnDestHeight,intXSrc,intYsrc,intnSrcWidth,intnSrcHeight,CONSTVOID*lpBits,CONSTBITMAPINFO*lpBitsInfo,UINTiUs…

    2022年6月16日
    28
  • pageoffice生成离线license.lic

    pageoffice生成离线license.lic1、单位名称、联系人、联系电话按情况填写2、序列号:PageOfficeV4.0标准版试用序列号:IMTG6-BSXJ-JGZ6-3BIWMPageOfficeV4.0专业版试用序列号:C

    2022年7月3日
    57
  • mysql数据库旅游管理系统_JSP+MySQL基于ssm的旅游管理系统[通俗易懂]

    mysql数据库旅游管理系统_JSP+MySQL基于ssm的旅游管理系统[通俗易懂]本旅游管理系统主要包括系统用户管理模块、景点信息管理模块、变幻图管理、旅游线路管理、登录模块、和退出模块等多个模块。它帮助旅游管理实现了信息化、网络化,通过测试,实现了系统设计目标,相比传统的管理模式,本系统合理的利用了旅游管理数据资源,有效的减少了旅游管理的经济投入,大大提高了旅游管理的效率。SSM旅游网站前台部分界面SSM旅游网站后台部分界面系统功能结构图本系统主要包含了等系统用户管理、景点信…

    2022年6月9日
    30
  • http,socks5,socks4代理的区别[通俗易懂]

    http,socks5,socks4代理的区别[通俗易懂]HTTP代理:能够代理客户机的HTTP访问,主要是代理浏览器访问网页,它的端口一般为80、8080、3128等;SOCKS代理:SOCKS代理与其他类型的代理不同,它只是简单地传递数据包,而并不关心是何种应用协议,既可以是HTTP请求,所以SOCKS代理服务器比其他类型的代理服务器速度要快得多。SOCKS代理又分为SOCKS4和SOCKS5,二者不同的是SOCKS4代理只支持TCP协议(即传输…

    2022年6月15日
    40
  • pac模式与全局模式哪个好_ppp模式的优缺点

    pac模式与全局模式哪个好_ppp模式的优缺点众所周知,传统有一大弊端,即:只能使用全局模式,这样情况下,虽然可以加速国外网站,但打开国内网站速度会变慢;加速度不但支持全局模式,同时也支持PAC模式(智能分流,绕过中国大陆),这样情况下,既可以加速国外网站,也不会影响国内网站打开速度!温馨提示:安卓手机客户端、IOS客户端、MAC客户端均可根据自己实际使用需求,自由切换【PAC模式】或者【全局模式】,具体如何切换可查看相应客户端设置教程;…

    2022年10月19日
    2
  • 二叉树的层序遍历(两种方法实现)

    二叉树的层序遍历(两种方法实现)两种方法实现二叉树的层序遍历1、说明二叉树的层序遍历是面试经常会被考察的知识点,甚至要求当场写出实现过程。层序遍历所要解决的问题很好理解,就是按二叉树从上到下,从左到右依次打印每个节点中存储的数据。如下图:先序遍历:A→B→D→C中序遍历:B→D→A→C后续遍历:D→B→C→A层序遍历:A→B→C→…

    2022年5月11日
    37

发表回复

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

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