JAVA字符流实践教程

在Java中,字符流用于处理字符数据,对文本数据而言,字符流比字节流更加高效。两个最基本的字符流类是ReaderWriter,分别代表输入字符流和输出字符流。以下是一些常用的字符流实践例子:

FileReader 和 FileWriter

这些是用于文件I/O的字符流类。FileReader用于读取文件中的文本数据,FileWriter用于将文本数据写入文件。

读取文件示例
import java.io.FileReader;
import java.io.IOException;

public class FileReaderExample {
    public static void main(String[] args) {
        FileReader fr = null;
        try {
            fr = new FileReader("example.txt");
            int content;
            while ((content = fr.read()) != -1) {
                // content 是读取的字符数据
                System.out.print((char) content);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
写入文件示例
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterExample {
    public static void main(String[] args) {
        FileWriter fw = null;
        try {
            fw = new FileWriter("example.txt");
            String text = "Hello, World!";
            fw.write(text);
            System.out.println("Write to file completed.");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

BufferedReader 和 BufferedWriter

这些类提供缓冲功能来提升I/O性能。BufferedReaderBufferedWriter分别包装一个ReaderWriter,并为它们提供缓冲区。

使用缓冲读取文件示例
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class BufferedReaderExample {
    public static void main(String[] args) {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader("example.txt"));
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
使用缓冲写入文件示例
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class BufferedWriterExample {
    public static void main(String[] args) {
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter("example.txt"));
            bw.write("Hello, World!");
            bw.newLine(); // 写入一个新行
            bw.write("Hello, BufferedWriter!");
            System.out.println("Write to file completed.");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

PrintWriter

PrintWriter是另一个用于输出文本数据的类,可以很方便地写入各种数据类型,包括对象。

使用PrintWriter写入文件示例
import java.io.PrintWriter;
import java.io.IOException;

public class PrintWriterExample {
    public static void main(String[] args) {
        PrintWriter pw = null;
        try {
            pw = new PrintWriter("example.txt");
            pw.println("Hello, PrintWriter!");
            pw.println(123);
            pw.printf("You have %d new messages", 5);
            System.out.println("Write to file completed.");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (pw != null) {
                pw.close();
            }
        }
    }
}

在使用字符流时,要注意以下几点:

  • 使用合适的字符集编码,尤其是当处理不同国家或地区的文本数据时。
  • 总是在一个finally块中关闭流,以确保即使发生异常也会释放资源。
  • 对于输出流,调用flush()可以确保所有数据都被写
  • 13
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序员爱学习

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

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

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

打赏作者

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

抵扣说明:

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

余额充值