字符输入输出流

package FileT.Day4;
/**
 * @author CCQ
 *  构造方法有两个参数,一个是字节流对象,一个是字符串类型的编码格式。可以只写字节流对象
 *
 *  void	close()
 *           关闭该流并释放与之关联的所有资源。
 *  String	getEncoding()
 *           返回此流使用的字符编码的名称。
 *  int	read()
 *           读取单个字符。
 *  int	read(char[] cbuf, int offset, int length)
 *           将字符读入数组中的某一部分。
 *  boolean	ready()
 *           判断此流是否已经准备好用于读取。
 */

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class InputStreamReadTest {
    public static void main(String[] args) throws IOException {
        InputStreamReader isr =new InputStreamReader(new FileInputStream("fox.txt"));
        System.out.println(isr.getEncoding());
        System.out.println(isr.ready());
        int ch;
        while ((ch =isr.read())!=-1){
            System.out.print((char)ch);
        }

        int len;
        char[] chars =new char[1024];
        while ((len =isr.read(chars))!=-1){
            System.out.println(new String(chars,0,len));
        }

        isr.close();
    }
}

package FileT.Day4;
/**
 * @author CCQ
 * 构造方法有两个参数,一个是字节流对象,一个是字符串类型的编码格式。可以只写字节流对象
 *
 *  void	close()
 *           关闭此流,但要先刷新它。
 *  void	flush()
 *           刷新该流的缓冲。
 *  String	getEncoding()
 *           返回此流使用的字符编码的名称。
 *  void	write(char[] cbuf, int off, int len)
 *           写入字符数组的某一部分。
 *  void	write(int c)
 *           写入单个字符。
 *  void	write(String str, int off, int len)
 *           写入字符串的某一部分。
 */

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class OutputStreamWriterTest {
    public static void main(String[] args) throws IOException {
        OutputStreamWriter osw =new OutputStreamWriter(new FileOutputStream("fox.txt"));
        System.out.println(osw.getEncoding());
        osw.write('c');
        osw.write(new char[]{'a','b','c','d'});
        osw.write("大傻逼");
        osw.flush();
        osw.write("你是大傻逼吗",0,5);
        osw.close();

    }
}


字符流的编码:

package FileT.Day4;
/**
 * 字符流的编码
 */

import java.io.*;

public class InOutStreamRWTest {
    public static void main(String[] args) throws IOException {
        OutputStreamWriter os =new OutputStreamWriter(new FileOutputStream("fox.txt"));
        os.write("朱雀");
        os.write("终麦");
        os.close();
        OutputStreamWriter os2 =new OutputStreamWriter(new FileOutputStream("fox.txt"),"GBK");
        os2.write("C国");
        os2.write("愚蠢");
        os2.close();

        InputStreamReader is =new InputStreamReader(new FileInputStream("fox.txt"),"GBK");
        int ch;
        while ((ch =is.read())!=-1){
            System.out.println((char)ch);
        }
        is.close();

        InputStreamReader is1 =new InputStreamReader(new FileInputStream("fox.txt"),"GBK");
        char[] chars =new char[1024];
        int len;
        while ((len =is1.read(chars,0,chars.length))!=-1){
            System.out.println(new String(chars,0,len));
        }
    }
}


小demo:

package FileT.Day4;
/**
 * 字符流不能像字节流那样什么文件都可以复制
 *  例如音像音频什么的都不能
 *  以下例子是失败的,因为传输的是图片
 */

import java.io.*;

public class Copydemo {
    public static void main(String[] args) throws IOException {
        InputStreamReader isr =new InputStreamReader(new FileInputStream("D:\\picture\\捕获1.PNG"));
        OutputStreamWriter osw =new OutputStreamWriter(new FileOutputStream("D:\\picture\\J.PNG"));
        char[] chars =new char[1024];
        int len;
        while ((len =isr.read(chars,0,chars.length))!=-1){
            osw.write(chars,0,len);
        }
        isr.close();
        osw.close();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中面向字符输入输出流的根类是抽象类`Reader`和`Writer`,它们是所有字符输入输出流的基类。`Reader`和`Writer`分别表示输入和输出字符流。它们都是抽象类,不能直接使用,需要使用其具体子类。 下面是`Reader`和`Writer`的子类层次结构: ``` Reader ├── InputStreamReader ├── CharArrayReader ├── StringReader ├── FileReader ├── PipedReader ├── FilterReader │ ├── PushbackReader │ ├── BufferedReader │ └── LineNumberReader └── InputStreamReader Writer ├── OutputStreamWriter ├── CharArrayWriter ├── StringWriter ├── FileWriter ├── PipedWriter └── FilterWriter ├── PrintWriter └── BufferedWriter ``` 常用的类及其主要功能如下: 1. `InputStreamReader`:将字节流转换为字符流; 2. `CharArrayReader`、`StringReader`、`FileReader`:从字符数组、字符串、文件中读取字符; 3. `PipedReader`:从与`PipedWriter`相关联的管道中读取字符; 4. `FilterReader`及其子类`PushbackReader`、`BufferedReader`、`LineNumberReader`:提供额外的功能,如推回字符、缓冲字符和记录行号等; 5. `OutputStreamWriter`:将字符流转换为字节流; 6. `CharArrayWriter`、`StringWriter`、`FileWriter`:将字符写入字符数组、字符串、文件; 7. `PipedWriter`:将字符写入与`PipedReader`相关联的管道中; 8. `FilterWriter`及其子类`PrintWriter`、`BufferedWriter`:提供额外的功能,如打印、缓冲字符等。 这些类提供了非常方便的字符输入输出流操作功能,可以轻松地进行文件读写、管道通信等操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值