//3. 从内存取出格式化输入 try{ DataInputStream in3 = new DataInputStream( new ByteArrayInputStream(s2.getBytes())); while(true) System.out.println((char)in3.readByte()); } catch(EOFException e){ System.out.println("End of stream"); } //4. 输出到文件 try{ BufferedReader in4 = new BufferedReader( new StringReader(s2)); PrintWriter out1 = new PrintWriter( new BufferedWriter( new FileWriter("F:\\nepalon\\ TestIO.out"))); int lineCount = 1; while((s = in4.readLine()) != null) out1.println(lineCount++ + ":" + s); out1.close(); in4.close(); } catch(EOFException ex){ System.out.println("End of stream"); } //5. 数据的存储和恢复 try{ DataOutputStream out2 = new DataOutputStream( new BufferedOutputStream( new FileOutputStream("F:\\nepalon\\ Data.txt"))); out2.writeDouble(3.1415926); out2.writeChars("\nThas was pi:writeChars\n"); out2.writeBytes("Thas was pi:writeByte\n"); out2.close(); DataInputStream in5 = new DataInputStream( new BufferedInputStream( new FileInputStream("F:\\nepalon\\ Data.txt"))); BufferedReader in5br = new BufferedReader( new InputStreamReader(in5)); System.out.println(in5.readDouble()); System.out.println(in5br.readLine()); System.out.println(in5br.readLine()); } |