博客
关于我
java字符流入门
阅读量:663 次
发布时间:2019-03-15

本文共 1053 字,大约阅读时间需要 3 分钟。

字符流

字符流是Java IO中用于读写文本文件的核心流类型。它专门处理字符数据,适用于处理文本文件的读写操作。

将数据写入文件

在Java中,使用FileWriter类来写文本文件。以下是示例代码:

import java.io.*; public class Demo {     public static void main(String[] args) throws IOException {         FileWriter fw = null;         try {             fw = new FileWriter("./tmp.txt");             fw.write("abc");         } catch (IOException e) {             e.printStackTrace();         } finally {             if (fw != null) {                 fw.close();             }         }     } }

从文件读取数据

使用FileReader类来读取文本文件。以下是两个读取示例:

public static void ReadDemo() throws IOException {    FileReader fr = new FileReader("./tmp.txt");    int num;    while ((num = fr.read()) != -1) {        System.out.print((char) num);    }}public static void ReadDemo1() throws IOException {    FileReader fr = new FileReader("./tmp.txt");     char[] arr = new char[1024];    int num;    while ((num = fr.read(arr)) != -1) {        System.out.println(new String(arr));    }}

总结

-字符流用于文本文件的读写操作。-FileWriter用于写入文本文件,FileReader用于读取文本文件。-字符流的读写操作可以支持一次性读取或写入多个字符,提高效率。

转载地址:http://nosmz.baihongyu.com/

你可能感兴趣的文章
Python numpy插入、读取至postgreSQL数据库中bytea类型字段
查看>>
Python numpy数据的保存和读取
查看>>
python numpy矩阵索引_python – 在2D numpy ndarray或numpy矩阵中获取前N个值的索引
查看>>
Python OCR库:自动化测试验证码识别神器!
查看>>
python opencv - 斑点检测或圆形检测
查看>>
Python还可以做情感分析你不看看吗?
查看>>
Python OpenCV HoughLinesP 无法检测线
查看>>
Python OpenCV-使用透明度覆盖图像
查看>>
python Opencv图像基础操作
查看>>
Python OpenCV将图像转换为字节字符串?
查看>>
python OpenCV视频的读取及保存
查看>>
python open和file的区别
查看>>
python open读取文件并对换行进行处理
查看>>
python os, os.path和sys模块
查看>>
Python os.environ 处理环境变量
查看>>
python os.path模块常用方法详解
查看>>
python os.system
查看>>
Python os.system执行多条语句,os.system的返回值以及与os.popen的区别
查看>>
Python os和sys模块
查看>>
python os文件/目录
查看>>