/** * IO管道处理集合 */ public void OutFile() throws FileNotFoundException { /** * 使用PrintStream进行输出 */ PrintStream print = new PrintStream(new FileOutputStream(new File("d:" + File.separator + "hello.txt"))); print.println(true); print.println("Rollen"); print.close(); /** *****格式化输出******** */ String name = "Rollen"; int age = 20; print.printf("姓名:%s. 年龄:%d.", name, age); print.close(); /** * 使用OutputStream向屏幕上输出内容 * */ OutputStream out = System.out; try { out.write("hello".getBytes()); } catch (Exception e) { e.printStackTrace(); } try { out.close(); } catch (Exception e) { e.printStackTrace(); } /** * 为System.out.println()重定向输出 * */ // 此刻直接输出到屏幕 System.out.println("hello"); File file = new File("d:" + File.separator + "hello.txt"); try { System.setOut(new PrintStream(new FileOutputStream(file))); } catch (FileNotFoundException e) { e.printStackTrace(); } System.out.println("这些内容在文件中才能看到哦!"); /** * System.err重定向 这个例子也提示我们可以使用这种方法保存错误信息 * */ System.err.println("这些在控制台输出"); try { System.setErr(new PrintStream(new FileOutputStream(file))); } catch (FileNotFoundException e) { e.printStackTrace(); } System.err.println("这些在文件中才能看到哦!"); /** * System.in重定向 前提是文件中提前要有值* */ if (!file.exists()) { return; } else { try { System.setIn(new FileInputStream(file)); } catch (FileNotFoundException e) { e.printStackTrace(); } byte[] bytes = new byte[1024]; int len = 0; try { len = System.in.read(bytes); } catch (IOException e) { e.printStackTrace(); } System.out.println("读入的内容为:" + new String(bytes, 0, len)); } /** * 使用缓冲区从键盘上读入内容 * */ BufferedReader buf = new BufferedReader( new InputStreamReader(System.in)); String str = null; System.out.println("请输入内容"); try { str = buf.readLine(); } catch (IOException e) { e.printStackTrace(); } System.out.println("你输入的内容是:" + str); } /** * 消息发送类 */ class Send implements Runnable { private PipedOutputStream out = null; public Send() { out = new PipedOutputStream(); } public PipedOutputStream getOut() { return this.out; } public void run() { String message = "hello , Rollen"; try { out.write(message.getBytes()); } catch (Exception e) { e.printStackTrace(); } try { out.close(); } catch (Exception e) { e.printStackTrace(); } } } /** * 接受消息类 * */ class Recive implements Runnable { private PipedInputStream input = null; public Recive() { this.input = new PipedInputStream(); } public PipedInputStream getInput() { return this.input; } public void run() { byte[] b = new byte[1000]; int len = 0; try { len = this.input.read(b); } catch (Exception e) { e.printStackTrace(); } try { input.close(); } catch (Exception e) { e.printStackTrace(); } System.out.println("接受的内容为 " + (new String(b, 0, len))); } /** Scanner读取数据 */ public void OutFile() { File file = new File("d:" + File.separator + "hello.txt"); Scanner sca = null; try { sca = new Scanner(file); } catch (FileNotFoundException e) { e.printStackTrace(); } String str = sca.next(); System.out.println("从文件中读取的内容是:" + str); } /** * 数据操作流DataOutputStream、DataInputStream类 * @throws IOException */ public void DataOutFile() throws IOException { File file = new File("d:" + File.separator + "hello.txt"); char[] ch = { 'A', 'B', 'C' }; DataOutputStream out = null; out = new DataOutputStream(new FileOutputStream(file)); for (char temp : ch) { out.writeChar(temp); } out.close(); } /** * DataInputStream读取数据 * @throws IOException */ public void DataInputStreamFile() throws IOException { File file = new File("d:" + File.separator + "hello.txt"); DataInputStream input = new DataInputStream(new FileInputStream(file)); char[] ch = new char[10]; int count = 0; char temp; while ((temp = input.readChar()) != 'C') { ch[count++] = temp; } System.out.println(ch); } /** * 合并流 SequenceInputStream合并文件 合并hello1/hello2到hello中 * @throws IOException */ public void SequenceInputStreamFile() throws IOException { File file1 = new File("d:" + File.separator + "hello1.txt"); File file2 = new File("d:" + File.separator + "hello2.txt"); File file3 = new File("d:" + File.separator + "hello.txt"); InputStream input1 = new FileInputStream(file1); InputStream input2 = new FileInputStream(file2); OutputStream output = new FileOutputStream(file3); // 合并流 SequenceInputStream sis = new SequenceInputStream(input1, input2); int temp = 0; while ((temp = sis.read()) != -1) { output.write(temp); } input1.close(); input2.close(); output.close(); sis.close(); }

转载于:https://www.cnblogs.com/JPAORM/archive/2012/04/29/2509854.html

Java IO实战操作(三)相关推荐

  1. Java IO 体系(三):Reader与Writer

    正文 本篇讲述的是Java IO中的Reader类和Writer类.跟之前讲述的InputStream和OutputStream一样为IO流中的抽象父类之一,不过Reader和Writer的操作对象不 ...

  2. Java IO实战操作(一)

    /** * 创建一个新文件 */ public void NewFiles() { File file = new File("D:\\IO.txt"); try { file.c ...

  3. Java IO实战操作(二)

    /** * 向文件中追加新内容 * @throws IOException */ public void NewInserNum() throws IOException { String fileN ...

  4. Java IO实战操作(四)

    /** * 文件压缩 ZipOutputStream类 * @throws IOException */ public void ZipOutputStreamFile() throws IOExce ...

  5. java复习系列[4] - Java IO

    文章目录 Java IO IO传输 IO读写流程 IO类型 IO的访问方式 缓存IO(标准IO.传统IO) 直接IO 内存映射 总结 Java中IO与NIO的区别 Java NIO 流与缓冲 管道 为 ...

  6. Java IO流学习总结三:缓冲流-BufferedInputStream、BufferedOutputStream

    Java IO流学习总结三:缓冲流-BufferedInputStream.BufferedOutputStream 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/ ...

  7. Java IO流(三)

    字节 字符 输入 InputStream Reader 输出 OutputStream Writer **********************字节写************************ ...

  8. Java io流学习总结(三)

    转载于:https://www.cnblogs.com/ll409546297/p/7197911.html java.io几种读写文件的方式 一.Java把这些不同来源和目标的数据都统一抽象为数据流 ...

  9. java 常用类库_JAVA(三)JAVA常用类库/JAVA IO

    成鹏致远 |lcw.cnblog.com|2014-02-01 JAVA常用类库 1.StringBuffer StringBuffer是使用缓冲区的,本身也是操作字符串的,但是与String类不同, ...

最新文章

  1. ListView详解(二)
  2. git bash命令_?你可能不太会用的10个Git命令
  3. 设计微服务架构需要掌握的基础知识
  4. hyperledge环境安装
  5. 2021暑假实习-SSM超市积分管理系统-day02笔记
  6. 【HDOJ】3948 The Number of Palindromes
  7. centos7安装kubernetes1.9集群
  8. AWS AI 全面助力视频理解,GluonCV 0.6 轻松复现前沿模型
  9. Android 系统(232)---减小 OTA 大小
  10. 王腾晒Redmi K30 Pro拆机视频:创新“三明治”主板
  11. 川土微 | CA-IS3105W 全集成 DC-DC 转换器
  12. 螺旋模型的概念简答题
  13. No serializer found for class JSONNull and no properties discovered to create BeanSerializer
  14. 轻量级Qt键盘-介绍篇
  15. matlab解方java_Java:调用window的matlab遇到的问题和解决方案
  16. 数据包接收系列 — 数据包的接收过程(宏观整体)
  17. 了解B2B、C2C、B2C、C2B、O2O、F2C、B2B2C,以及走进电商
  18. 如何轻松审核通过马甲包
  19. 淘宝运营 淘金币的定义 怎样利用淘金币引流 怎样推广
  20. 【国产】大数据自动化运维调度工具TASKCTL流程触发方式

热门文章

  1. SSH整合(1)异常
  2. 如何使用alt键+数字键盘上的数字键打出特殊符号
  3. oracle学用命令大全 笔记
  4. js获取本地文件夹和文件 .
  5. mplayer error opening/initializing the selected video_out (-vo) device
  6. 【计算机网络】Internet原理与技术2(因特网的路由协议RIP、OSPF、BGP,网络地址转换NAT,网络协议IPv6)
  7. SELinux系列(九)——SELinux auditd日志系统的安装与启动
  8. 软件_mongo占用磁盘空间过大
  9. ubuntu18下vnpy1.9.2的安装
  10. java中mouselistener的用法_关于MouseListener接口的简单使用