IO流之FileReader和FileWriter

IO流之FileReader和FileWriter

FileReader

以下代码使用FileReader读取数据:

package com.javalearn.io.filereader;

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

public class TestForFileReader {
    public static void main(String[] args) {
        FileReader reader = null;  // 以防忘记,这里注意,Reader以字符为单位读取数据
        try {
            reader = new FileReader("file1");
            char[] chars = new char[4];  // 字符数组,与字节数组类似
            int count = 0;
            while ((count = reader.read(chars))!=-1) {  // 每次读多个字符
                System.out.print(new String(chars,0,count));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

输出结果:

abcdef

FileWriter

以下代码使用FileWriter把数据写入硬盘:

package com.javalearn.io.filewriter;

import java.io.FileWriter;
import java.io.IOException;

public class TestForJavaWriter {
    public static void main(String[] args) {
        FileWriter writer = null;
        try {
            writer = new FileWriter("file1");  // 无此文件则新建。默认先清空原文件再写入,有参数true则追加,不清空
            char[] chars = {'我','想','打','游','戏'};
            writer.write(chars);  //可传入字符数组
            writer.write("我是什么都会的猪大肠");  //也可传入字符串
            writer.flush();  //别忘了刷新
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

文件输出结果:

我想打游戏我是什么都会的猪大肠

文本拷贝

以下代码使用FileReader和FileWriter来拷贝文件:

package com.javalearn.io.copy2;

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

public class TestForCopy {
    public static void main(String[] args) {
        FileReader in = null;
        FileWriter out = null;
        try {
            in = new FileReader("D:\\javaProject\\Hello\\src\\Hello.java");
            out = new FileWriter("D:\\typora笔记\\java\\io流\\临时文件夹\\新.java");
            char[] chars = new char[1024*512]; // 1MB
            int count = 0;
            while ((count = in.read(chars))!=-1) {
                out.write(chars,0,count);
            }
            out.flush();  // 别忘了刷新
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

posted on 2021-12-04 16:20  菜小疯  阅读(33)  评论(0编辑  收藏  举报