1.内存操作流

之前学习的IO操作输入和输出都是从文件中来的,当然,也可以将输入和输出的位置设置在内存上,这就需要用到内存操作流,java提供两类内存操作流

  • 字节内存操作流:ByteArrayOutputStream:将内存中数据输出                      ByteArrayInputStream:将内容写入到内存中
  • 字符内存操作流:CharArrayWriter                                                                   CharArrayReader

使用场景:

假设现在需要实现IO操作,又不希望产生文件,则就可以以内存为终端进行处理,这个时候的流程
与文件IO不同,InputStream是往内存里写,OutputStream是读内存。

ByteArrayOutputStream类的定义:

  1. public class ByteArrayOutputStream extends OutputStream

主要方法:

ByteArrayInputStream类的定义:

  1. public class ByteArrayInputStream extends InputStream

代码实例:利用内存操作流实现一个小写字母转大写字母的操作

  1. import java.io.*;
  2.  
  3. public class ByteArrayStreamDemo {
  4.  
  5. public static void main(String[] args) throws Exception {
  6. // TODO Auto-generated method stub
  7. String str = "i believe i can fly";
  8. ByteArrayOutputStream output = new ByteArrayOutputStream();
  9. InputStream input = new ByteArrayInputStream(str.getBytes());
  10. int data = 0;
  11. while((data = input.read()) != -1) {
  12. output.write(Character.toUpperCase(data));
  13. }
  14. byte res[] = output.toByteArray();
  15. System.out.println(new String(res));
  16. input.close();
  17. output.close();
  18. }
  19.  
  20. }

2.管道流

管道流主要作用是实现两个线程之间的IO处理操作,java中提供两类管道流

  • 字节管道流:PipedInputStream(管道输入流)    PipedOutputStream(管道输出流)
  • 字符管道流:PipedReader                                     PipedWriter

如果想要进行管道输出,则必须把输出流连在输入流之上,在PipeOutputStream上有如下方法用于连接管道。

  1. public void connect​(PipedInputStream src) throws IOException

在PipedReader上有如下方法用于连接管道。

  1. public void connect​(PipedWriter snk) throws IOException

PipedOutputStream类的定义:

  1. public class PipedOutputStream extends OutputStream

构造方法:

  1. public PipedOutputStream()

主要方法:

  1. public void write​(int b) throws IOException//输出方法

PipedInputStream类的定义:

  1. public class PipedInputStream extends InputStream

构造方法:

  1. public PipedInputStream()

主要方法:

  1. public int read() throws IOException//输入方法

代码实例:

  1. import java.io.*;
  2.  
  3. public class PipedDemo {
  4.  
  5. public static void main(String[] args) throws IOException {
  6. // TODO Auto-generated method stub
  7. SendThread send = new SendThread();
  8. RecieveThread recieve = new RecieveThread();
  9. send.getOutput().connect(recieve.getInput());
  10. new Thread(send,"消息发送线程:").start();
  11. new Thread(recieve,"消息接收线程:").start();
  12. }
  13.  
  14. }
  15. class SendThread implements Runnable{
  16. private PipedOutputStream output;//管道输出流
  17.  
  18. public SendThread() {
  19. this.output = new PipedOutputStream();//实例化管道输出流
  20. }
  21. @Override
  22. public void run() {
  23. // TODO Auto-generated method stub
  24. for(int i=1;i<=10;i++) {
  25. try {
  26. this.output.write(("【第"+i+"次信息发送-" + Thread.currentThread().getName()+"i love you】\n").getBytes());
  27. } catch (IOException e) {
  28. // TODO Auto-generated catch block
  29. e.printStackTrace();
  30. }
  31. }
  32. try {
  33. this.output.close();
  34. } catch (IOException e) {
  35. // TODO Auto-generated catch block
  36. e.printStackTrace();
  37. }
  38. }
  39. public PipedOutputStream getOutput() {
  40. return output;
  41. }
  42. }
  43. class RecieveThread implements Runnable{
  44. private PipedInputStream input;//管道输入流
  45.  
  46. public RecieveThread() {
  47. this.input = new PipedInputStream();//实例化管道输入流
  48. }
  49. @Override
  50. public void run() {
  51. // TODO Auto-generated method stub
  52. byte[] data = new byte[1024];
  53. int len = 0;
  54. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  55. try {
  56. while((len=this.input.read(data))!= -1) {
  57. bos.write(data,0,len);
  58. }
  59. System.out.println("{"+Thread.currentThread().getName()+"接收消息}"+new String(bos.toByteArray()));
  60. bos.close();
  61. } catch (IOException e) {
  62. // TODO Auto-generated catch block
  63. e.printStackTrace();
  64. }
  65. try {
  66. this.input.close();
  67. } catch (IOException e) {
  68. // TODO Auto-generated catch block
  69. e.printStackTrace();
  70. }
  71. }
  72. public PipedInputStream getInput() {
  73. return input;
  74. }
  75. }

3.随机流(RandomAcceFile类)

对于文件内容的处理操作主要是通过InputStream(Reader)OutputStream(Write)来实现,但是利用这些类
实现的内容读取只能够将数据部分部分读取进来,如果文件非常庞大(20G),此时存再按照传统的IO操作进行读取
和分析根本不可能完成,所以这种情况下RandomAccessFile类可以实现跳跃式的读取,可以只读取中间的部分内容
(前提:需要有一个完善的保存形式)

RandomAccessFile类的定义:

  1. public class RandomAccessFile extends Object
  2. implements DataOutput, DataInput, Closeable

构造方法:

  1. public RandomAccessFile​(File file,String mode) throws FileNotFoundException

RandomAccessFile最大的特点实在数据的读取处理上,因为所有的数据时按照固定的长度进行的保存,所以读取的
时候就可以进行跳字节读取

主要方法:

  1. public int skipBytes​(int n) throws IOException//向下跳
  1. public void seek​(long pos) throws IOException//向回跳

代码实例:

  1. /*
  2. * 实现文件的保存
  3. */
  4. package IODemo;
  5.  
  6. import java.io.*;
  7.  
  8. public class RandomAccessFileDemo {
  9.  
  10. public static void main(String[] args) throws Exception {
  11. // TODO Auto-generated method stub
  12. File file = new File("D:"+File.separator+"mldn.txt");
  13. RandomAccessFile raf = new RandomAccessFile(file,"rw");
  14. String[] names = new String[] {"zhangsan","wangwu ","lisi "};//名字占8位
  15. int age[] = new int[] {30,20,16};//年龄占4位
  16. for(int x = 0 ; x < names.length ; x++) {
  17. raf.write(names[x].getBytes());
  18. raf.writeInt(age[x]);
  19. }
  20. raf.close();
  21. }
  22.  
  23. }
  1. /*
  2. * 读取数据
  3. */
  4. package IODemo;
  5.  
  6. import java.io.*;
  7.  
  8. public class RandomAccessFileDemo2 {
  9.  
  10. public static void main(String[] args) throws Exception {
  11. // TODO Auto-generated method stub
  12. File file = new File("D:"+File.separator+"mldn.txt");
  13. RandomAccessFile raf = new RandomAccessFile(file,"rw");
  14. {//读取lisi数据,跳过24位
  15. raf.skipBytes(24);
  16. byte[] data = new byte[8];
  17. int len = raf.read(data);
  18. System.out.println("姓名:"+new String(data,0,len).trim()+"年龄:"+raf.readInt());
  19. }
  20. {//读取wangwu数据,回跳12位
  21. raf.seek(12);
  22. byte[] data = new byte[8];
  23. int len = raf.read(data);
  24. System.out.println("姓名:"+new String(data,0,len).trim()+"年龄:"+raf.readInt());
  25. }
  26. {//读取zhangsan数据,回跳到顶点
  27. raf.seek(0);
  28. byte[] data = new byte[8];
  29. int len = raf.read(data);
  30. System.out.println("姓名:"+new String(data,0,len).trim()+"年龄:"+raf.readInt());
  31. }
  32. }
  33.  
  34. }

整体的使用之中由用户自行定义要读取的位置,而后按照指定的结构进行数据的读取

java学习笔记之IO编程—内存流、管道流、随机流的更多相关文章

  1. java学习笔记之IO编程—字节流和字符流

    1. 流的基本概念 在java.io包里面File类是唯一一个与文件本身有关的程序处理类,但是File只能够操作文件本身而不能操作文件的内容,或者说在实际的开发之中IO操作的核心意义在于:输入与输出操 ...

  2. java学习笔记之IO编程—打印流和BufferedReader

    1.打印流(PrintWriter) 想要通过程序实现内容输出,其核心一定是要依靠OutputStream类,但是OutputStream类有一个最大缺点,就是这个类中的输出操作功能有限,所有的数据一 ...

  3. java学习笔记之IO编程—对象序列化

    对象序列化就是将内存中保存的对象以二进制数据流的形式进行处理,可以实现对象的保存或网络传输. 并不是所有的对象都可以被序列化,如果要序列化的对象,那么对象所在的类一定要实现java.io.Serial ...

  4. java学习笔记之IO编程—目录和文件的拷贝

    进行文件或目录的拷贝时,要先判断处理对象是文件还是目录,如果是文件则直接拷贝,如果是目录还需要拷贝它的子目录及其文件,这就需要递归处理了 import java.io.*; class FileUti ...

  5. java学习笔记之IO编程—File文件操作类

    1. File类说明 在Java语言里面提供有对于文件操作系统操作的支持,而这个支持就在java.io.File类中进行了定义,也就是说在整个java.io包里面,File类是唯一一个与文件本身操作( ...

  6. java学习笔记15--多线程编程基础2

    本文地址:http://www.cnblogs.com/archimedes/p/java-study-note15.html,转载请注明源地址. 线程的生命周期 1.线程的生命周期 线程从产生到消亡 ...

  7. java学习笔记14--多线程编程基础1

    本文地址:http://www.cnblogs.com/archimedes/p/java-study-note14.html,转载请注明源地址. 多线程编程基础 多进程 一个独立程序的每一次运行称为 ...

  8. 【原】Java学习笔记033 - IO

    package cn.temptation; public class Sample01 { public static void main(String[] args) { // 需求:继承关系中爷 ...

  9. Java学习笔记-10.io流

    1.输入流,只能从中读取数据,而不能向其写出数据.输出流,只能想起写入字节数据,而不能从中读取. 2.InputStream的类型有: ByteArrayInputStream 包含一个内存缓冲区,字 ...

随机推荐

  1. Nginx + Linux 性能调优

    Nginx以高性能负载均衡.缓存和web服务器出名,支撑着世界上繁忙网站中的40%.大多数使用场景下,Nginx和Linux系统的默认配置表现较好,但是仍有必要做一些调优以期达到最佳性能. 这篇文章讨 ...

  2. Windows 远程桌面连接Ubuntu14.04

    在Ubuntu系统进行如下系统配置 1.安装xrdp sudo apt-get install xrdp 2.安装vnc4server sudo apt-get install vnc4server ...

  3. c++ bool

    bool 就两个值,真或者假,通常用来存储关系表达式或者逻辑表达式的结果. 以前是用 int 来表示真假,大 int 有多个值,所以才规定 0 为假,非零为真,导致对应关系比较麻烦,有了 bool 就 ...

  4. .NET CORE(C#) WPF简单菜单MVVM绑定

    微信公众号:Dotnet9,网站:Dotnet9,问题或建议:请网站留言, 如果对您有所帮助:欢迎赞赏. .NET CORE(C#) WPF简单菜单MVVM绑定 阅读导航 本文背景 代码实现 本文参考 ...

  5. 在虚拟机中使用NetToPLCSim和PLC相连.

    1,虚拟机...系统Win10...里面安装了VS. 2,本机...系统Win10...里面安装了博图15. 3,转换软件:NetToPLCSIM. 4,本机和虚拟机连接同一个路由器.注意: 5,设置 ...

  6. C#设计模式学习笔记:(19)策略模式

    本笔记摘抄自:https://www.cnblogs.com/PatrickLiu/p/8057654.html,记录一下学习过程以备后续查用. 一.引言 今天我们要讲行为型设计模式的第七个模式--策 ...

  7. float布局打破标准流,神助攻clear清浮动

    布局是什么?根据功能划分小块,再根据设计稿还原,书写静态页面,然后再在块里面填充内容,完善功能,js施加交互效果.div作为一个容器,独占一行,代码书写习惯从上至下属于标准流,而浮动float的css ...

  8. MySQL基础(4) | 视图

    MySQL基础(4) | 视图 基本语法 1.创建 CREATE VIEW <视图名> AS <SELECT语句> 语法说明如下. <视图名>:指定视图的名称.该名 ...

  9. 测试.NET core MiddleWare 运行逻辑

    话不多说,直接开整. 首先创建一个.NET CORE web 工程,创建完成之后,会自动创建相关文件如图(本示例基于.NET CORE 3.0): 打开Startup.cs可以看到在Configure ...

  10. C#设计模式学习笔记:(6)适配器模式

    本笔记摘抄自:https://www.cnblogs.com/PatrickLiu/p/7640873.html,记录一下学习过程以备后续查用. 一.引言 从今天开始我们开始讲结构型设计模式,结构型设 ...