-
PrintStream
-
Printwriter类
-
package com.lagou.task17; import java.io.*; import java.text.SimpleDateFormat; import java.util.Date; public class PrintStreamChatTest { public static void main(String[] args) { //由手册可知:构造方法是Reader类型的引用,但Reader是个抽象类,实参只能传递子类的对象,,字符流 //由手册可知:System.in代表键盘输入,而且是InputStream类型的 字节流 //1.提示用户输入要发送的聊天内容并使用变量记录 BufferedReader br = null; PrintStream ps = null; try { br = new BufferedReader(new InputStreamReader(System.in)); ps = new PrintStream(new FileOutputStream("d:/a.txt",true)); //声明一个boolean类型的变量作为发送方的代表 Boolean flag = true; while (true) { System.out.println("请"+(flag?"张三":"李四")+ "输入要发送的聊天内容:"); String str = br.readLine(); //2.判断用户输入是否为"bye",若是,则聊天结束 if ("bye".equals(str)) { System.out.println("聊天结束!"); break; } //3.若不是,则将用户输入的内容写入到d:/a.txt文件中 //获取当前系统时间并调整格式 Date d1 = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); ps.println(sdf.format(d1)+(flag?"张三说:":"李四说:")+str); flag = !flag; } ps.println(); ps.println(); ps.println(); } catch (IOException e) { e.printStackTrace(); } finally { //4.关闭流对象并释放相关资源 ps.close(); try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } }
- 转换流与打印流的实现
-
字符编码
-
package com.lagou.task17; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.IOException; public class DataInputStreamTest { public static void main(String[] args) { DataInputStream dis = null; try { //1.创建DataInPutStream类型的对象与d:/a.txt文件关联 dis = new DataInputStream(new FileInputStream("d:/a.txt")); //2.从输入流读取一个整数并打印 int i = dis.readInt(); System.out.println("读取的数据是:" + i); } catch (IOException e) { e.printStackTrace(); } finally { //3.关闭输入流对象并释放相关资源 if (null != dis) { try { dis.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
package com.lagou.task17; import java.io.DataOutputStream; import java.io.FileOutputStream; import java.io.IOException; public class DataOutputStreamTest { public static void main(String[] args) { DataOutputStream dos = null; try { //1.创建DataOutputStream类型的对象与d:/a.txt文件关联 dos = new DataOutputStream(new FileOutputStream("d:/a.txt")); //2.从输出流写入一个整数并打印 int num = 66; dos.writeInt(num); System.out.println("写入数据成功!"); } catch (IOException e) { e.printStackTrace(); } finally { //3.关闭输入流对象并释放相关资源 if (null != dos) { try { dos.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
搜索
-
-
ObjectOutputStream类
-
任务总结
- IO流
- 概念、分类、体系结构、相关类的详解等、
-
程序和进程的概念
- 程序:存放在硬盘上的课执行文件 (数据结构+算法)
- 进程:运行在内存中的可执行文件
- 为了让操作系统执行多个任务
-
线程:
- 进程内部可以开多线程,线程是轻量的。
- 多线程是采用时间片轮转法来保证多个线程的并发执行,所谓并发就是指宏观并行门微观串行的机制。