Java基础(八)——IO流5_其他流 Craftsman

一、其他

1、System.in、System.out(标准输入、输出流)

  System.in:标准的输入流,默认从键盘输入。
  System.out:标准的输出流,默认从控制台输出。
  改变标准输入输出(System下的静态方法)。

  void setIn(InputStream in):重新分配"标准"输入流
  void setOut(PrintStream out):重新分配"标准"输出流

2、PrintStream、PrintWriter(打印流)

  代码示例:将内存数据打印到文件

 1 public class Main {
 2     public static void main(String[] args) {
 3         try {
 4             FileOutputStream fos = new FileOutputStream(new File("F:\\hello.txt"));
 5 
 6             // 创建打印输出流,设置为自动刷新模式(写入换行符或字节 '\n' 时都会刷新输出缓冲区)
 7             PrintStream ps = new PrintStream(fos, true);
 8 
 9             // 把标准输出流(控制台输出)改成文件
10             System.setOut(ps);
11 
12             for (int i = 0; i <= 15; i++) {
13                 System.out.print(i);
14                 if (i % 5 == 0) {
15                     System.out.println();
16                 }
17             }
18         } catch (Exception e) {
19         }
20     }
21 }
22 
23 // 结果.文件:F:\\hello.txt
24 // 内容:
25 0
26 12345
27 678910
28 1112131415

  代码示例:打印系统环境信息

1 public class Main {
2     public static void main(String[] args) throws Exception {
3         Properties properties = System.getProperties();
4         properties.list(new PrintStream("F:\\hello.txt"));
5     }
6 }

3、DataInputStream 、DataOutputStream(数据流)

  作用:用于读取或写出基本数据类型的变量或字符串。
  代码示例:写。将内存中的字符串、基本数据类型的变量写出到文件中。

 1 public class Main {
 2     public static void main(String[] args) {
 3         try (DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.dat"));) {
 4 
 5             dos.writeUTF("张三");
 6             dos.writeInt(23);
 7             dos.writeBoolean(true);
 8 
 9             dos.flush(); // 刷新操作,将内存中的数据写入文件
10         } catch (Exception e) {
11         }
12     }
13 }

  代码示例:读。将文件中的数据读到内存中。需要与保存的数据的顺序一致!

 1 public class Main {
 2     public static void main(String[] args) {
 3         try (DataInputStream dis = new DataInputStream(new FileInputStream("data.dat"));) {
 4             String name = dis.readUTF();
 5             int age = dis.readInt();
 6             boolean isMale = dis.readBoolean();
 7 
 8             System.out.println("name = " + name);
 9             System.out.println("age = " + age);
10             System.out.println("isMale = " + isMale);
11         } catch (Exception e) {
12         }
13     }
14 }

4、管道输入流、管道输出流

  管道输入/输出流和普通的文件输入/输出流或者网络输入/输出流不同之处在于,它主要用于线程之间的数据传输,而传输的媒介为内存。
  管道输入/输出流主要包括了如下4种具体实现:PipedOutputStream、PipedInputStream、PipedReader和PipedWriter,前两种面向字节,而后两种面向字符。

  代码示例:

 1 public class Main {
 2 
 3     public static void main(String[] args) {
 4         try (PipedWriter out = new PipedWriter();
 5              PipedReader in = new PipedReader();) {
 6 
 7             // 将输入流和输出流进行连接,否则会出现IO错误
 8             out.connect(in);
 9 
10             // 开启一个线程用于读取管道流
11             new Thread(new Print(in), "printThread").start();
12 
13             // 接收键盘的输入,并写入到管道流中
14             int receive = 0;
15             while ((receive = System.in.read()) != -1) {
16                 out.write(receive);
17             }
18         } catch (Exception e) {
19         }
20     }
21 
22 
23     static class Print implements Runnable {
24         private final PipedReader in;
25 
26         public Print(PipedReader in) {
27             this.in = in;
28         }
29 
30         @Override
31         public void run() {
32             int receive = 0;
33             try {
34                 while ((receive = in.read()) != -1) {
35                     System.out.print((char) receive);
36                 }
37             } catch (IOException ex) {
38             }
39         }
40     }
41 }

  上述代码,创建了线程printThread,它用来接收main线程的输入,任何main线程的输入均通过PipedWriter写入,而printThread在另一端通过PipedReader将内容读出并打印。
  注意:对于Piped类型的流,必须先要进行绑定,也就是调用connect()方法,如果没有将输入/输出流绑定起来,对于该流的访问将会抛出异常。

  参考文档:《Java并发编程的艺术》

作者:Craftsman-L

本博客所有文章仅用于学习、研究和交流目的,版权归作者所有,欢迎非商业性质转载。

如果本篇博客给您带来帮助,请作者喝杯咖啡吧!点击下面打赏,您的支持是我最大的动力!

原文地址:https://www.cnblogs.com/originator/p/15736978.html