博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Io流字符流和字符流的相互转换
阅读量:5208 次
发布时间:2019-06-14

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

package 字节流转换为字符流;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;public class Demo1 {    public static void main(String[] args) {        //System类中的属性  in返回值是 static  InputStream也就是数据类型                InputStream is = System.in;        BufferedReader br = new BufferedReader(new InputStreamReader(is));        BufferedWriter bw = null;        String line = null;                try {            bw = new BufferedWriter(new FileWriter("b.txt"));            //不为null就可以一直输入            while((line = br.readLine())!=null) {                //输入exit就退出                if("exit".equals(line)) {                    break;                }                //输入一行数据                bw.write(line);                //输入之后换一行                bw.newLine();                //一定要清除缓存                bw.flush();                            }        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }finally {            if(bw != null) {                try {                    bw.close();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        }    }}
package 字符流转换为字节流;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.io.OutputStreamWriter;public class Demo1 {        public static void main(String[] args) {        BufferedReader br = null;        BufferedWriter bw = null;        try {            br = new BufferedReader(new FileReader("b.txt"));            bw = new BufferedWriter(new OutputStreamWriter(System.out));            String line =     null;                        while((line = br.readLine())!=null) {                bw.write(line);                bw.newLine();                bw.flush();            }        } catch (FileNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }finally {            try {                if(bw!=null) {                    bw.close();                }                if(br!=null) {                    br.close();                }            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }            }}

 

转载于:https://www.cnblogs.com/java-jiangtao-home/p/9374985.html

你可能感兴趣的文章