本笔记来源于:尚硅谷Java零基础全套视频教程(宋红康2023版,java入门自学必备)
b站视频
1. 标准的输入输出流:
System.in:标准的输入流,默认从键盘输入
System.out:标准的输出流,默认从控制台输出
修改默认的输入和输出行为:
System类的setIn(InputStream is) / setOut(PrintStream ps)方式重新指定输入和输出的流。
2. 打印流:
PrintStream 和PrintWriter
说明:
提供了一系列重载的print()和println()方法,用于多种数据类型的输出
System.out返回的是PrintStream的实例
3. 数据流:
DataInputStream 和 DataOutputStream
作用:
用于读取或写出基本数据类型的变量或字符串
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
@Test public void test3() throws IOException { DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt")); dos.writeUTF("刘建辰"); dos.flush(); dos.writeInt(23); dos.flush(); dos.writeBoolean(true); dos.flush(); dos.close(); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
@Test public void test4() throws IOException { DataInputStream dis = new DataInputStream(new FileInputStream("data.txt")); String name = dis.readUTF(); int age = dis.readInt(); boolean isMale = dis.readBoolean();
System.out.println("name = " + name); System.out.println("age = " + age); System.out.println("isMale = " + isMale);
dis.close(); }
|