FileReader和FileOutputStream

1 篇文章 0 订阅

 FileReader相关方法:

  1. new FileReader(File/String)
  2. read: 每次读取单个字符,返回该字符,如果到文件末尾返回-1
  3. read(char [ ]):批量读取多个字符到数组,返回读取到的字符数,如果到文件末尾返回-1

相关API:

  1. new String(char [ ] ):将char[ ]转换成String
  2. new String(char, off, len):将char[ ]的指定部分转换成String
package IO;

import org.junit.jupiter.api.Test;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ReadFile {
    public static void main(String[] args) {
    }
    /*
    * 单个字符读取文件
    * */
    @Test
    public void read(){
        String filepath = "d:\\hello.txt";
        FileReader fileReader = null;
        int data = 0;
        try {
            fileReader = new FileReader(filepath);
            while ((data = fileReader.read()) != -1){
                System.out.println((char) data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (fileReader != null) {
                    fileReader.close();
                }
            }
            catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    /*
     * 字符数组读取文件
     * */
    @Test
    public void read2() {
        System.out.println("read2");
        String filepath = "d:\\hello.txt";
        FileReader fileReader = null;
        int readLen = 0;
        char[] buf = new char[8];
        try {
            fileReader = new FileReader(filepath);
            while ((readLen = fileReader.read(buf)) != -1){
                System.out.println(new String(buf, 0 ,readLen));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (fileReader != null) {
                    fileReader.close();
                }
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    }

FileOutStream: 向文件输出数据的输出字节流

 使用FileOutputStream要注意的细节:

  1. 使用FileOutputStream 的时候,如果目标文件不存在,那么会自动创建目标文件对象。
  2. 使用FileOutputStream的时候,如果目标文件已存在,那么会先清空目标文件中的数据,然后再写入数据。
  3. 创建文件输入流写入由指定的File对象表示的文件。如果第二个参数是true,则字节将被写入文件的末尾而不是开头。如果文件存在但是是一个目录而不是常规文件,不存在但不能创建,或者由于任何其他原因无法打开,那么将抛出一个FileNotFoundException。
import org.junit.jupiter.api.Test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileOutputStream1 {
    public static void main(String[] args) {

    }
    @Test
    public void writeFile(){
        String filepath = "D:\\ok.txt";
        String data = "abc";
        byte[] buf = data.getBytes();

        try {
            FileOutputStream fileOutputStream = new FileOutputStream(filepath);
            fileOutputStream.write(buf, 0, 3);
            fileOutputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Java码蚁

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

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

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

打赏作者

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

抵扣说明:

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

余额充值