1.内存操作流

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

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

使用场景:

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

ByteArrayOutputStream类的定义:

public class ByteArrayOutputStream extends OutputStream

主要方法:

ByteArrayInputStream类的定义:

public class ByteArrayInputStream extends InputStream

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

import java.io.*;

public class ByteArrayStreamDemo {

    public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String str = "i believe i can fly";
ByteArrayOutputStream output = new ByteArrayOutputStream();
InputStream input = new ByteArrayInputStream(str.getBytes());
int data = 0;
while((data = input.read()) != -1) {
output.write(Character.toUpperCase(data));
}
byte res[] = output.toByteArray();
System.out.println(new String(res));
input.close();
output.close();
} }

2.管道流

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

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

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

public void connect​(PipedInputStream src) throws IOException

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

public void connect​(PipedWriter snk)  throws IOException

PipedOutputStream类的定义:

public class PipedOutputStream extends OutputStream

构造方法:

public PipedOutputStream()

主要方法:

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

PipedInputStream类的定义:

public class PipedInputStream extends InputStream

构造方法:

public PipedInputStream()

主要方法:

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

代码实例:

import java.io.*;

public class PipedDemo {

    public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
SendThread send = new SendThread();
RecieveThread recieve = new RecieveThread();
send.getOutput().connect(recieve.getInput());
new Thread(send,"消息发送线程:").start();
new Thread(recieve,"消息接收线程:").start();
} }
class SendThread implements Runnable{
private PipedOutputStream output;//管道输出流 public SendThread() {
this.output = new PipedOutputStream();//实例化管道输出流
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=1;i<=10;i++) {
try {
this.output.write(("【第"+i+"次信息发送-" + Thread.currentThread().getName()+"i love you】\n").getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
this.output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public PipedOutputStream getOutput() {
return output;
}
}
class RecieveThread implements Runnable{
private PipedInputStream input;//管道输入流 public RecieveThread() {
this.input = new PipedInputStream();//实例化管道输入流
}
@Override
public void run() {
// TODO Auto-generated method stub
byte[] data = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
while((len=this.input.read(data))!= -1) {
bos.write(data,0,len);
}
System.out.println("{"+Thread.currentThread().getName()+"接收消息}"+new String(bos.toByteArray()));
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
this.input.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public PipedInputStream getInput() {
return input;
}
}

3.随机流(RandomAcceFile类)

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

RandomAccessFile类的定义:

public class RandomAccessFile extends Object
implements DataOutput, DataInput, Closeable

构造方法:

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

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

主要方法:

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

代码实例:

/*
* 实现文件的保存
*/
package IODemo; import java.io.*; public class RandomAccessFileDemo { public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
File file = new File("D:"+File.separator+"mldn.txt");
RandomAccessFile raf = new RandomAccessFile(file,"rw");
String[] names = new String[] {"zhangsan","wangwu ","lisi "};//名字占8位
int age[] = new int[] {30,20,16};//年龄占4位
for(int x = 0 ; x < names.length ; x++) {
raf.write(names[x].getBytes());
raf.writeInt(age[x]);
}
raf.close();
} }
/*
* 读取数据
*/
package IODemo; import java.io.*; public class RandomAccessFileDemo2 { public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
File file = new File("D:"+File.separator+"mldn.txt");
RandomAccessFile raf = new RandomAccessFile(file,"rw");
{//读取lisi数据,跳过24位
raf.skipBytes(24);
byte[] data = new byte[8];
int len = raf.read(data);
System.out.println("姓名:"+new String(data,0,len).trim()+"年龄:"+raf.readInt());
}
{//读取wangwu数据,回跳12位
raf.seek(12);
byte[] data = new byte[8];
int len = raf.read(data);
System.out.println("姓名:"+new String(data,0,len).trim()+"年龄:"+raf.readInt());
}
{//读取zhangsan数据,回跳到顶点
raf.seek(0);
byte[] data = new byte[8];
int len = raf.read(data);
System.out.println("姓名:"+new String(data,0,len).trim()+"年龄:"+raf.readInt());
}
} }

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

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. linux系统的启动流程梳理

    1. 不同版本的linux系统的启动流程 1.1 centos6.x系统的启动流程 其详细启动步骤如下: 1)开机,BIOS自检,检查各个硬件是否正常 2)读取硬盘MBR信息,引导系统启动 3)加载g ...

  2. Java的开发—面向对象的7大原则之开闭原则(一)

    开闭原则(Open Close Principle) 一.定义: 软件中的(类.模块.函数等等)应该对于扩展是开放的,对于修改时关闭的.意味着一个实体允许在不改变它的源代码的前提变更它的行为 这里的软 ...

  3. hexo博客

    安装软件 node.js(建议稳定版本,本人安装v8.11.3) npm install -g hexo-cli hexo init myBlog //初始化,在myBlog的文件夹下建立网站 hex ...

  4. leetcode--js--Two Sum

    问题描述: 给定一个整数数列,找出其中和为特定值的那两个数. 你可以假设每个输入都只会有一种答案,同样的元素不能被重用. 示例: 给定 nums = [2, 7, 11, 15], target = ...

  5. localStorage 存储

    localStorage 的优势 localStorage 拓展了 cookie 的 4K 限制. localStorage 会可以将第一次请求的数据直接存储到本地,这个相当于一个 5M 大小的针对于 ...

  6. C#设计模式学习笔记:(16)观察者模式

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

  7. Linux中screen命令

    screen是一款由GNU计划开发的用于命令行终端切换的自由软件.用户可以通过该软件同时连接多个本地或远程的命令行会话,并在其间自由切换.GNU Screen可以看作是窗口管理器的命令行界面版本.它提 ...

  8. 常用类String的总结

    /* String:字符串,使用一对""引起来表示. 1.String声明为final的,不可被继承 2.String实现了Serializable接口:表示字符串是支持序列化的. ...

  9. 如何阻止a标签跳转

    <a href="www.baidu.com">百度</a> 上面为我们的a标签,要想阻止它进行跳转我们该怎么办呢? 当然我们有以下的几种办法_______ ...

  10. Umi 小白纪实(三)—— 震惊!路由竟然如此强大!

    在<Umi 小白纪实(一)>中有提到过简单的路由配置和使用,但这只是冰山一角 借用一句广告词,Umi 路由的能量,超乎你的想象 一.基本用法 Umi 的路由根结点是全局 layout  s ...