InputStreamReader & OutputStreamWriter
InputStreamReader 是字节流通向字符流的桥梁:它使用指定的 charset
读取字节并将其解码为字符。
OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的
charset
将要写入流中的字符编码成字节。public class Client8 { public static void main(String[] args) throws IOException { BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("b.txt"))); String line = null; while((line=bufr.readLine())!=null){ if("over".equals(line)) break; bufw.write(line.toUpperCase()); bufw.newLine(); bufw.flush(); } } }
public class TransStreamDemo { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { //字节流。 InputStream in = System.in; // int ch = in.read(); // System.out.println(ch); // int ch1 = in.read(); // System.out.println(ch1); //将字节转成字符的桥梁。装换流。 InputStreamReader isr = new InputStreamReader(in); // int ch = isr.read(); // System.out.println((char)ch); //字符流。 BufferedReader bufr = new BufferedReader(isr); OutputStream out = System.out; OutputStreamWriter osw = new OutputStreamWriter(out); BufferedWriter bufw = new BufferedWriter(osw); String line = null; while((line=bufr.readLine())!=null){ if("over".equals(line)) break; // System.out.println(line.toUpperCase()); // osw.write(line.toUpperCase()+"\r\n"); // osw.flush(); bufw.write(line.toUpperCase()); bufw.newLine(); bufw.flush(); } } }