字符流

5 篇文章 0 订阅

1.字符输入流【Reader】

java.io.Reader抽象类是表示用于读取字符流的所有类的超类,可以读取字符信息到内存中。
它定义了字符输入流的基本共性功能方法。
1.public void close()关闭此流并释放与此流相关联的任何系统资源
2.public void read()从输⼊流读取⼀个字符
3.public int read(char[] cbuf)从输⼊流中读取⼀些字符,并将它们存储到字符数组cbuf 中

1.1 InputStreamReader类

1.构造方法 :
InputStreamReader(InputStream in)
创建一个使用默认字符集的 InputStreamReader。
InputStreamReader(InputStream in, String charsetName)
创建使用指定字符集的 InputStreamReader。
2.常用API
int read()
读取单个字符。
int read(char[] cbuf, int offset, int length)
将字符读入数组中的某一部分

public class   InputStreamReaderConstructor throws IOException{
 public static void main(String[] args) {
 // 使⽤File对象创建流对象
 File file = new File("a.txt");
   InputStreamReader isr = new InputStreamReader(new FileInputStream(file));
 // 使⽤⽂件名称创建流对象
  InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"));
 }
}

读取字符数据

1.读取字符

read ⽅法,每次可以读取⼀个字符的数据,提升为int类型,读取到⽂件末尾,返回 -1 ,循环读取

public class InputStreamReaderDemo {
 public static void main(String[] args) throws IOException {
 // 使⽤⽂件名称创建流对象
  InputStreamReader isr = new InputStreamReader(new FileInputStream("read.txt"));
 int b;
 // 循环读取
 while ((b = isr.read()) != -1) {
 System.out.println((char)b);
 }
 // 关闭资源
 isr.close();
 }
}
输出结果:
程序员
2. 使⽤字符数组读取:

read(char[] cbuf) ,每次读取b的⻓度个字符到数组中,返回读取到
的有效字符个数,读取到末尾时,返回 -1

public class  InputStreamReaderDemo {
 public static void main(String[] args) throws IOException {
 // 使⽤⽂件名称创建流对象
 InputStreamReader isr = new InputStreamReader(new FileInputStream("read.txt"));
 // 定义变量,保存有效字符个数
  int len;
 // 定义字符数组,作为装字符数据的容器
 char[] cbuf = new char[2];
 // 循环读取
 while ((len = isr.read(cbuf)) != -1) {
 System.out.println(new String(cbuf));
 }
 // 关闭资源
isr.close();
 }
}
输出结果:
程序
员序

改进代码

public class InputStreamReaderDemo {
 public static void main(String[] args) throws IOException {
 // 使⽤⽂件名称创建流对象
 InputStreamReader isr = new InputStreamReader(new FileInputStream("read.txt"));
 // 定义变量,保存有效字符个数
 int len;
 // 定义字符数组,作为装字符数据的容器
 char[] cbuf = new char[2];
 // 循环读取
 while ((len = isr .read(cbuf)) != -1) {
 System.out.println(new String(cbuf,0,len));
 }
 // 关闭资源
isr.close();
 }
}
输出结果:
程序
员

2.字符输出流【Writer】

java.io.Writer 抽象类是表示⽤于写出字符流的所有类的超类,将指定的字符信息写出到⽬的
地。它定义了字节输出流的基本共性功能⽅法。
void write(int c) :写⼊单个字符。
void write(char[] cbuf) :写⼊字符数组。
abstract void write(char[] cbuf, int off, int len) :写⼊字符数组的某⼀部分,off
数组的开始索引,len写的字符个数。
void write(String str) :写⼊字符串。
void write(String str, int off, int len) :写⼊字符串的某⼀部分,off字符串的开始
索引,len写的字符个数。
void flush() :刷新该流的缓冲。
void close() :关闭此流,但要先刷新它。

2.1 OutputStreamWriter

  构造方法
    OutputStreamWriter(OutputStream out)
    创建使用默认字符编码的 OutputStreamWriter。
    OutputStreamWriter(OutputStream out, String charsetName)
    创建使用指定字符集的 OutputStreamWriter。

常用API
void write(char[] cbuf, int off, int len)
写入字符数组的某一部分。
void write(int c)
写入单个字符。
void write(String str, int off, int len)
写入字符串的某一部分。

public class   OutputStreamWriterConstructor throws IOException{
 public static void main(String[] args) {
 // 使⽤File对象创建流对象
 File file = new File("a.txt");
    OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file));
 // 使⽤⽂件名称创建流对象
    OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("a.txt"));
 }
}

基本写出数据

1.写出字符:

write(int b) ⽅法,每次可以写出⼀个字符数据

public class   OutputStreamWriterDemo {
 public static void main(String[] args) throws IOException {
 // 使⽤⽂件名称创建流对象
  OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("a.txt"));
 // 写出数据
osw.write(97); // 写出第1个字符
osw.write('b'); // 写出第2个字符
osw.write('C'); // 写出第3个字符
osw.write(30000); // 写出第4个字符,中⽂编码表中30000对应⼀个汉字。
 /*
 *【注意】关闭资源时,与FileOutputStream不同。
 * 如果不关闭,数据只是保存到缓冲区,并未保存到⽂件。
 */
 // osw.close();
 }
}
输出结果:
abC⽥

⼩贴⼠:

  1. 虽然参数为int类型四个字节,但是只会保留⼀个字符的信息写出。
  2. 未调⽤close⽅法,数据只是保存到了缓冲区,并未写出到⽂件中。

关闭和刷新

因为内置缓冲区的原因,如果不关闭输出流,⽆法写出字符到⽂件中。但是关闭的流对象,是⽆法继续写出数据的。如果我们既想写出数据,⼜想继续使⽤流,就需要 flush ⽅法了
1.flush :刷新缓冲区,流对象可以继续使⽤。
2.close :先刷新缓冲区,然后通知系统释放资源。流对象不可以再被使⽤了。

public class OutputStreamWriterDemo {
 public static void main(String[] args) throws IOException {
 // 使⽤⽂件名称创建流对象
 OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("a.txt"));
 // 写出数据,通过flush
 osw.write('刷'); // 写出第1个字符
 osw.flush();
 osw.write('新'); // 继续写出第2个字符,写出成功
 osw.flush();
 // 写出数据,通过close
osw.write('关'); // 写出第1个字符
osw.close();
osw.write('闭'); // 继续写出第2个字符,【报错】java.io.IOException: Stream closed
osw.close();
 }
}

写出其他数据

1. 写出字符数组

write(char[] cbuf) 和 write(char[] cbuf, int off, int len) ,每次可以写出字符数组中的数据,⽤法类似FileOutputStream

public class OutputStreamWriterDemo {
 public static void main(String[] args) throws IOException {
 // 使⽤⽂件名称创建流对象
 OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("a.txt"));
 // 字符串转换为字节数组
 char[] chars = "程序员".toCharArray();
 // 写出字符数组
 osw.write(chars); // 程序员
 // 写出从索引1开始,2个字符。索引1是'序',两个字符,也就是'序员'。
 osw.write(chars, 1, 2); // 序员
 // 关闭资源
 osw.close();
 }
}
2. 写出字符串

write(String str) 和 write(String str, int off, int len) ,每次可以写出字符串中的数据

public class OutputStreamWriterDemo {
 public static void main(String[] args) throws IOException {
 // 使⽤⽂件名称创建流对象
 OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("a.txt"));
 // 字符串
 String msg = "程序员";
 // 写出字符数组
 osw.write(msg); //程序员
 // 写出从索引1开始,2个字符。索引1是'序',两个字符,也就是'序员'。
 osw.write(msg, 1, 2); // 序员
 // 关闭资源
osw.close();
 }
}
3. . 续写和换⾏

操作类似于FileOutputStream。

public class OutputStreamWriterDemo {
 public static void main(String[] args) throws IOException {
 // 使⽤⽂件名称创建流对象,可以续写数据
 OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("a.txt",true));
 // 写出字符串
 osw.write("Java");
 // 写出换⾏
osw.write("\r\n");
 // 写出字符串
osw.write("程序员");
 // 关闭资源
osw.close();
 }
}
输出结果:
Java
程序员

3 缓冲字符流

1缓冲字符输出流:PrintWriter

构造方法:
PrintWriter(String fileName)指定文件和默认字符集
PrintWriter(String fileName, String csn)指定文件名和字符集
作用:只能清空源文件内容,但是可以指定字符集
PrintWriter(OutputStream out, boolean autoFlush)
OutputStream:底层字节流,可以指定是否进行追加
作用:可以在源文件上再追加内容,但是不可以指定字符集,可以自动刷新缓冲区
PrintWriter(Writer out, boolean autoFlush)
Writer :底层字符流,是用底层字节流,可以指定是否进行追加,
底层字符流有编码问题,所以可以指定字符集


public class PrintDemo {
 public static void main(String[] args) throws IOException {
 // 调⽤系统的打印流,控制台直接输出97
 System.out.println(97);
 // 创建打印流,指定⽂件的名称
 PrintStream ps = new PrintStream("ps.txt");
 // 设置系统的打印流流向,输出到ps.txt
 System.setOut(ps);
 // 调⽤系统的打印流,ps.txt中输出97
 System.out.println(97);
 }
}

2缓冲字符输入流:BufferedReader

构造方法:
BufferedReader(new InputStreamReader(new FileInputStream(new File())))
常用方法:
void close() 关闭该流并释放与之关联的所有资源。
void mark(int readAheadLimit) 标记流中的当前位置。
boolean markSupported() 判断此流是否支持 mark() 操作(它一定支持)。
int read() 读取单个字符。
int read(char[] cbuf, int off, int len) 将字符读入数组的某一部分。
String readLine() 读取一个文本行。

public class BufferedReaderDemo{
    public static void main(String[] args) throws IOException {
        // 创建流对象
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("a.txt")));
        // 定义字符串,保存读取的⼀⾏⽂字
        String line = null;
        // 循环读取,读取到最后返回null
        while ((line = br.readLine()) != null) {
            System.out.print(line);
            System.out.println("------");
        }
        // 释放资源
        br.close();
    }
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值