专注Java教育14年 全国咨询/投诉热线:400-8080-105
动力节点LOGO图
始于2009,口口相传的Java黄埔军校
首页 hot资讯 让我们来看看字符输出流

让我们来看看字符输出流

更新时间:2023-02-08 16:07:38 来源:动力节点 浏览827次

1.字符输入流

1.java.io.Reader:字符输入流,抽象类
    具体的子类:字节输入流通向字符输入流的桥梁:InputStreamReader
2.字符缓冲输入流:
	构造方法:
		public InputStreamReader(InputStream in):使用默认字符集进行解码
        public InputStreamReader(InputStream in,String charsetName)throws UnsupportedEncodingException:使用指定的字符集解码
    读的方式:
		public int read() throws IOException:一次读取一个字符
        public int read(char[] cbuf)throws IOException:一次读取一个字符数组
3.public class ReaderDemo{
	public static void mian(String[] args) throws Exception{
        //创建字符缓冲输入流对象
        InputStreamReader isr = new InputStreamReader(new FileInputStream("osw.txt"));
        
        //一次读取一个字符数组
        char[] chs = new char[1024];
        int len = 0;
        while((len=isr.read(chs))!=-1){
            sout(new String(chs,0,len));
        }
        isr.close();
    }    
}

2.字符输出流

1.java.io.Writer:字符输出流,抽象类
    具体的子类:OutputStreamWriter:字符转换输出流
2.构造方法:
	public OutputStreamWriter(OutputStream out):使用平台默认字符集编码
    public OutputStreamWriter(OutputStream out,String charsetName):使用指定的字符集进行编码
  写的功能:
	public void write(char[] cbuf) throws IOException:写一个字符数组
    public abstract void write(char[] cbuf,int off,int len)throws IOException:从指定位置开始写入一部分字符数组
    public void write(String str)throws IOExceotion:写入字符串
    public void write(String str,int off,int len)throws IOException:写字符串的一部分
    public void write(int c)throws IOException:写一个字符
3.代码实现:
	public class WriteDemo{
        public static void main(String[] args) throw Exception{
            //创建字符缓冲输出流对象
            OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("osw.txt"));
            //写数据
            osw.write("hello,字符流我来了");
            //写一个字符
            osw.write(97);
            //写一个字符数组
            char[] chs = {"高","圆","圆"};
            osw.write(chs);
            
            //关闭前刷新流
            osw.flush();
            osw.close();
        }
    }

3.使用字符转换输入流和转换输出流来复制一个文本

1.字符流里直接操作文件--->字符转换流的便捷类
    直接子类:public FileReader(String fileName) throws FileNotFoundException
           public FileWriter(String fileName) throws IOException
2.代码实现:
public class CopyFileTest{
    public static void main(String[] args) throws Exception{
        method1("BufferedOutputStreamDemo.java","D:\\EE_2113\\day27_pm\\code\\copy4.java") ;
         method2("BufferedOutputStreamDemo.java","D:\\EE_2113\\day27_pm\\code\\copy2.java") ;
    }
    //使用字符转换流的便捷类来直接操作文件---一次读取一个字符数组
    private static void method1(String sourch,String dest) throws Exception{
        //创建FileReader类对象
        FileReader fr = new FileReader(source);
        //创建FileWriter类对象
        FileWriter fw = new FileWriter(dest);
        
        //一次读取一个字符数组
        char[] chs = new char[1024];
        int len = 0;
        while((len = fr.read(chs))!=-1){
            fw.write(chs,0,len);
        }
        
        fw.close();
        fr.close();
    }
    
    
    //使用字符转换输入流一次读取一个字符数组
    private static void method2(String source,String dest) throws Exception{
        InputStreamReader isr = new InptStreamReader(new FileInputStream(source));
        OutputStreamWriter osw = new OutputStreamReader(new FileOutputStream(dest));
        char[] chs = new char[1024];
        int len = 0 ;
        while((len=isr.read(chs))!= -1){
            osw.write(chs,0,len);
        }
        osw.close();
        isr.close();
    }
}

以上就是动力节点小编介绍的"让我们来看看字符输出流",希望对大家有帮助,如有疑问,请在线咨询,有专业老师随时为您务。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>