OutputStreamWriter:字节输出流转成字符输出流
public class OutputStreamWriterDemo { public static void main(String[] args) throws IOException { //主要作用将字节流转成字符流输出 //要写出的数据先放到字符输出流,底层真正做数据传输的是字节输出流 //字符输出流---字节输出流 OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("D:\12.txt")); //写出数据---字符串---字符 通过字符流写出数据 osw.write("abc"); //关流 osw.close(); }
InputStreamReader :字节输入流转成字符输入流
public static void main(String[] args) throws IOException { //底层从文件读取的是字节输入流 //展示的时候使用字符输入流 //字节流---字符流 InputStreamReader isr=new InputStreamReader(new FileInputStream("D:\12.txt")); //读取数据---字符数组--字符---字符流 char[] cs=new char[5]; int len=-1; while((len=isr.read(cs))!=-1){ System.out.println(new String(cs,0,len)); } }
系统流
系统流都是静态全局唯一所以不能关流
//out---字节输出流的对象 //黑色---表明输出结果 System.out.println(1); //err---字节输出流的对象 //红色---表明输出错误/异常 System.err.println(1); //in---字节输入流 new Scanner(System.in);
public static void main(String[] args) { // Scanner sc1=new Scanner(System.in); String s1=sc1.nextLine(); //关流 sc1.close(); //系统流都是静态的---全局唯一所以关流了以后后面的会报异常 //系统流都是静态的不能关流 Scanner sc2=new Scanner(System.in); String s2=sc2.nextLine(); System.out.println(s1); System.out.println(s2); }
结果
1 Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Unknown Source) at cn.tedu.io.system.SystemDemo1.main(SystemDemo1.java:15)