package IO流基本规律应用; /** * 转换流使用: * 1、操作文本数据,但是接受或者输出的是字节流,为了操作方便转换到字符流 * 2、涉及到编码表,只有转换流才能指定编码表,这也是字符流跟字节流的不同,字符流=字节流+编码表*/ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; /** * 1、从键盘接受数据,保存到本地文件中; * 2、复制本地文件; * 3、本地文件内容读取到控制台 * 4、从键盘接受数据,显示在控制台上 * 5、将一个中文字符串,按照指定的编码表存入文件中。 * * */ public class IOTest { public static void main(String[] args) throws IOException { test_5(); } public static void test_5() throws IOException { BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw1 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("KEY_1.txt"),"UTF-8")); String line = null; while((line = br1.readLine())!=null){ bw1.write(line); bw1.flush(); bw1.newLine(); } br1.close(); bw1.close(); } public static void test_4() throws IOException{ BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw1 = new BufferedWriter(new OutputStreamWriter(System.out)); String line = null; while((line = br1.readLine())!=null){ bw1.write(line); bw1.flush(); bw1.newLine(); } } public static void test_3() throws IOException { BufferedReader br1 = new BufferedReader(new FileReader("KEY.txt")); BufferedWriter bw1 = new BufferedWriter(new OutputStreamWriter(System.out)); String line=null; while((line = br1.readLine())!=null){ bw1.write(line); bw1.flush(); bw1.newLine(); } br1.close(); bw1.close(); } public static void test_2() throws IOException { BufferedReader br1 = new BufferedReader(new FileReader("demo.txt")); BufferedWriter bw1 = new BufferedWriter(new FileWriter("copy_2.txt")); String line=null; while((line=br1.readLine())!=null){ bw1.write(line); bw1.flush(); bw1.newLine(); } br1.close(); bw1.close(); } public static void test_1() throws IOException { BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw1 = new BufferedWriter(new FileWriter("D:\workspace\IO流\KEY.txt")); String line=null; while((line=br1.readLine())!=null){ bw1.write(line); bw1.flush(); bw1.newLine(); } br1.close(); bw1.close(); } }
package 键盘录入; import java.io.IOException; import java.io.InputStream; public class ReadKey { public static void main(String[] args) throws IOException { InputStream in = System.in;//系统获取的流对象唯一,一旦关闭,不可再创建;所以不需要关闭,默认的输入设备 int ch = 0; while((ch=in.read())!=-1) System.out.println(ch); } }
/** * */ package 操作基本数据类型流; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /** * @author Administrator * */ public class DataStreamDemo { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { //writeData(); readData(); } public static void readData() throws IOException { DataInputStream dis = new DataInputStream(new FileInputStream("data.txt")); String string = dis.readUTF(); System.out.println(string); dis.close(); } public static void writeData() throws IOException { DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt")); dos.writeUTF("你好"); dos.close(); } }
/** * CharArrayInputStream与CharArrayOutputStream用法同ByteArrayInputStream和ByteArrayoutputStream; * writeTo();方法可以写入一个流中; */ package 操作内存的流; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; /** * @author Administrator * */ public class ByteArrayStreamDemo { /** * @param args */ public static void main(String[] args) { ByteArrayInputStream bis = new ByteArrayInputStream("nba".getBytes()); ByteArrayOutputStream bos = new ByteArrayOutputStream();//存储到内存中 int len = 0; while((len = bis.read())!=-1){ bos.write(len); } System.out.println(bos.toString());//通过toString()方法查看缓冲区内容 } }
package 打印流; import java.io.IOException; /** * printStream: * 1、提供了多种方法对多种数据类型值,进行打印。并保持数据的表示形式。 * 2、他不抛出IOException * 构造函数,接受三种类型值: * 1、字符串路径 * 2、File对象 * 3、字节输出流 * */ import java.io.PrintStream; public class PrintStreamDemo { public static void main(String[] args) throws IOException { PrintStream out = new PrintStream("print.txt"); out.write(97);//写出字节的8个低位0110-0001,写了一个字节 out.print(97);//会保持数据的原样性,打印的是什么,目的地就是什么,其实就是变了字符串。写了2个字节 out.close(); } }
package 打印流; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; /** * PrintWriter: * 构造函数: * 1、字符串路径 * 2、File对象 * 3、字节输出流 * 4、字符输出流 * */ public class PrintWriterDemo { public static void main(String[] args) throws IOException { BufferedReader buff = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); String line = null; while((line = buff.readLine())!=null){ if("over".equals(line)){ break; } out.println(line.toUpperCase()); out.flush(); } out.close(); buff.close(); } }
/** * 管道流: * 特点: * 容易出现死锁现象,如果先读,就无法写了;所以要用多线程一个线程读一个线程写; * 管道连接的两种方式: * 输入流构造函数中直接传递输出流new PipedInputStream(new PipedOutputStream());或者输入流的connect方法; * */ package 管道流; import java.io.IOException; import java.io.PipedInputStream; import java.io.PipedOutputStream; /** * @author Administrator * */ public class PipedStreamDemo { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { PipedInputStream pis = new PipedInputStream(); PipedOutputStream pos = new PipedOutputStream(); pis.connect(pos); new Thread(new Input(pis)).start(); new Thread(new Output(pos)).start(); } } class Input implements Runnable{ private PipedInputStream pis; public Input(PipedInputStream pis){ this.pis=pis; } public void run(){ try { byte[]buf = new byte[1024]; int len=pis.read(buf); String name =new String(buf,0,len); System.out.println("name="+name); } catch (Exception e) { // TODO: handle exception } } } class Output implements Runnable{ private PipedOutputStream pos; public Output(PipedOutputStream pos){ this.pos=pos; } public void run(){ try { pos.write("管道流连接了!!".getBytes()); } catch (Exception e) { // TODO: handle exception } } }