读取文件和写入文件操作

        在java中会涉及到对文件进行读取和写入操作,以下将介绍如何用java对文件进行读取和写入

读取

        通过Readr读取字符流文件中的数据

读取字符流文件中的数据表示以字符为单位进行读取

package 文件操作;

import java.io.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: wuyulin
 * Date: 2023-08-06
 * Time: 15:36
 */
//用Readr读取字符流文件中的数据
public class Demo1 {
    public static void main(String[] args) throws IOException {
        //由于Reader类是虚拟类所以我们不能直接实例化Reader类的对象,只能实例化继承于Reader类的子类
        //由于实例化FileReader类型的对象后就会自动打开对应的文件
        //而考虑到很可能会由于忘记调用close方法导致使用完文件后没有关闭文件导致严重后果
        //所以采用try(){}的结构,在()中实例化FileReader类型的对象,在执行完{}中的内容后便会自动关闭打开的文件
        //读取字符流文件中数据的多种方式
       try(Reader reader=new FileReader("./text.txt")){
           //一
           //Read是读取字符流的数据,调用read()可以读取一个字符的数据,读取失败返回-1
           //int n1=reader.read();

           //二
           //char[] chs=new char[1024];
           //读取到的字符放到chs数组中,要是文件有>=1024个字符则读取满数组,要是文件有<1024个字符则将文件的所有字符读到数组中
           //返回的值n2表示实际读取到数组的字符个数
           //int n2=reader.read(chs);

           //三
           //表示读取数据到数组cbuf中,读取的数据填充到数组的off下标,填充len个字符
           //reader.read(char[] cbuf,int off,int len)

           //一般要读取并打印完文件中的内容采用的方法
            while (true){
                char[] chs=new char[5];
                int n=reader.read(chs);
                //文件中的数据已经读取完毕
                if(n==-1){
                    break;
                }

                //将当前读取到的字符进行打印
                for(int i=0;i<n;i++){
                    System.out.print(chs[i]);
                }
            }


       }
    }
}

        通过inputStream读取字节流文件中的数据

读取字节流文件中的数据表示以字节为单位进行读取

//用字节流读取文件中的数据
//InputStream和Readr的使用方式大致相同
public class Demo2 {
    public static void main(String[] args) throws IOException {
        try(InputStream inputStream=new FileInputStream("./text.txt")){
            //一
            //调用不带参数的read方法只读取一个字节,当返回-1表示文件中没有数据了
            //inputStream.read();

            //二
            //read方法的参数输入一个byte类型的数组,表示从文件中读到的字节数据都放到bytes数组中
            //返回一个int类型的值表示实际读到数组bytes中的字节数据个数
//            byte[] bytes=new byte[4];
//            int n=inputStream.read(bytes);

            //三
            //read方法填入三个参数依次表示,1.将从文件中读到的字节数据放入bytes数组中,从bytes数组下标0开始放数据,一共要放4个数据
//            byte[] bytes=new byte[4];
//            inputStream.read(bytes,0,4);

            //遍历并输出文件中所有数据的普遍方法
            while (true){
                byte[] bytes=new byte[4];
                int n=inputStream.read(bytes);
                //读到了文件末尾
                if(n==-1){
                    break;
                }
                //没有读完文件中的数据
                //打印数组中读到的数据
                for(int i=0;i<n;i++){
                    System.out.printf("%x",bytes[i]);
                }
            }
        }
    }
}

读取及其写入

读取字符流文件 file1 中的内容到 file2文件中

public class FileTest {
    private static String sourceFile="C:/Users/wuyulin/Desktop/file1.txt"; //原文件
    private static String objectFile="C:/Users/wuyulin/Desktop/file2.txt"; //目标文件

    /**
     * 读取字符流文件 file1 中的内容到 file2文件中
     * */
    public static void charStream() throws IOException {
        FileReader fileReader=new FileReader(sourceFile);
        FileWriter fileWriter=new FileWriter(objectFile);

        while (true){
            int ch=fileReader.read();

            if(ch==-1){
                //源文件读取完了
                break;
            }

            fileWriter.write(ch);
        }



        fileReader.close();
        fileWriter.close();
    }
}

读取字节流文件 file1 中的内容到 file2文件中

public class FileTest {
    private static String sourceFile="C:/Users/wuyulin/Desktop/file1.txt"; //原文件
    private static String objectFile="C:/Users/wuyulin/Desktop/file2.txt"; //目标文件

    /**
     * 读取字节流文件 file1 中的内容到 file2文件中
     * */
    public static void byteStream() throws IOException {
        //在实例化 fileInputStream 和 fileOutputStream 时就相当于打开了对应的文件
        FileInputStream fileInputStream=new FileInputStream(sourceFile);
        FileOutputStream fileOutputStream=new FileOutputStream(objectFile);

        while (true){
            int bit=fileInputStream.read();

            if(bit==-1){
                //源文件读取完了
                break;
            }

            //把读取到的字节写入到文件中
            fileOutputStream.write(bit);
        }


        //要将打开的文件进行关闭,要不然会导致文件资源泄露
        fileInputStream.close();
        fileOutputStream.close();
    }
}

read和write方法的参数

返回值类型是 int 的原因

        按道理读取一个字符,返回值类型应该是 char ,为什么返回值类型却是 int 呢,因为读取字符时,字符不需要进行算术运算,所以就不需要表示负数,既要不表示负数,又要表示出所以的字符,就需要用范围更大的类型,所以用 int 类型,虽然返回值是 int 类型,但实际上读取到的还是一个字符

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小林想被监督学习

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值