本文共 1944 字,大约阅读时间需要 6 分钟。
字节数组输入流 ByteArrayInputStream
字节数组输出流 ByteArrayOutputStream将字节数组转化成流
ByteArrayInputStream(byte [] ) 不用关闭 toByteArray() 输出到字节数组
输入流:DataInputStream(InputStream x) readXXX()
输出流:DataOutputStream(OutputStream x) writeXXX()// DataInputStream/DataOutputStream 写入到文件/从文件读取 public static void main (String [] args) throws IOException{ File file = new File("D:/桌面/DataInputStream.txt"); DataInputStream input = new DataInputStream(new FileInputStream(file));// DataOutputStream output = new DataOutputStream(new FileOutputStream(file)); int a = 100; double b = 200; String c = "string";// output.writeInt(a);// output.writeDouble(b);// output.writeUTF(c); int d = input.readInt(); double e = input.readDouble(); String f = input.readUTF(); System.out.println(d); System.out.println(e); System.out.println(f);// output.close();// input.close(); }
ObjectInputStream -> readObject()
ObjectOutputStream -> writeObject() 先序列化后反序列化,对象必须使用java.io.Seriallizable接口 不是所有属性都需要序列化,用transient表示不需要序列化的属性可变参数只能放在新蔡的最后一个位置(Closeable …)
…可变参数,处理方式与数组一致 try- with-resource
system.out
system.in printStreamPrintStream ps = System.outps.println();
ps = new PrintStream(new BufferedOutputStream(new FileOutputStream(src)))ps.println(); //输出到文件
System.out
System.err System.in 键盘输入
PrintStream output = new PrintStream(new FileOutputStream(new File("D:/桌面/2.txt")));output.print("printStream");
InputStream in = System.in;//system.in 实际上是一个InputStream流
setIn()
setOut() setErr()System.setOut(new PrintStream(new FileOutputStream(“”)));
System.out.println(“2333”); System.setOut(new PrintStream(new FileOutputStream(new FileDescripter.out)));//返回控制台
System.setOut(new PrintStream(new FileOutputStream("D:/桌面/3.txt")));System.out.println("重定向");