`

26、IO包中的管道流和随机访问文件

阅读更多

一、PipedOutputStream

 

  1. 可以将管道输出流连接到管道输入流来创建通信管道。
  2. 管道输出流是管道的发送端。
  3. 通常,数据由某个线程写入 PipedOutputStream 对象,
  4. 并由其他线程从连接的 PipedInputStream 读取。
  5. 不建议对这两个对象尝试使用单个线程,因为这样可能会造成该线程死锁。
  6. 如果某个线程正从连接的管道输入流中读取数据字节,但该线程不再处于活动状态,则该管道被视为处于 毁坏 状态。
public class PipedOutputStream extends OutputStream
{
	//创建尚未连接到管道输入流的管道输出流。必须在使用之前将管道输出流连接到管道输入流(既可由接收者连接,也可由发送者连接)。
	public PipedOutputStream(){}

	//创建连接到指定管道输入流的管道输出流。写入此流的数据字节稍后将用作 snk 的输入
	public PipedOutputStream(PipedInputStream snk)
                  throws IOException{}

	//将此管道输出流连接到接收者。如果此对象已经连接到其他某个管道输入流,则抛出 IOException
	public void connect(PipedInputStream snk)
             throws IOException{}
}

 

二、PipedInputStream

 

public class PipedInputStream extends InputStream
{
	//创建尚未连接的 PipedInputStream。在使用前必须将其连接到 PipedOutputStream
	public PipedInputStream(){}

	//创建一个尚未连接的 PipedInputStream,并对管道缓冲区使用指定的管道大小
	public PipedInputStream(int pipeSize){}

	//创建 PipedInputStream,使其连接到管道输出流 src。写入 src 的数据字节可用作此流的输入
	public PipedInputStream(PipedOutputStream src)
                 throws IOException{}

	//创建一个 PipedInputStream,使其连接到管道输出流 src,并对管道缓冲区使用指定的管道大小
	public PipedInputStream(PipedOutputStream src,
                        int pipeSize)
                 throws IOException{}

	//使此管道输入流连接到管道输出流 src。如果此对象已经连接到其他某个管道输出流,则抛出 IOException
	public void connect(PipedOutputStream src)
             throws IOException{}
}

 

三、示例

import java.io.*;

public class PipedStreamDemo {

	public static void main(String[] args) throws IOException {
		PipedInputStream pis = new PipedInputStream();
		PipedOutputStream pos = new PipedOutputStream(pis);
		new Thread(new Read(pis)).start();
		new Thread(new Write(pos)).start();
	}
}
//读取线程
class Read implements Runnable
{
	PipedInputStream pis;
	Read(PipedInputStream pis)
	{
		this.pis = pis;
	}
	public void run()
	{
		byte[] buf = new byte[1024];
		try {
			int len = pis.read(buf);
			String content = new String(buf,0,len);
			System.out.println(content);
			pis.close();
		} catch (IOException e) {
			System.out.println("读取失败");
		}
	}
}
//写入线程
class Write implements Runnable
{
	PipedOutputStream pos;
	Write(PipedOutputStream pos)
	{
		this.pos = pos;
	}
	public void run()
	{
		try {
			Thread.sleep(5000);
			pos.write("你好".getBytes());
			pos.close();
		} catch (IOException e) {
			System.out.println("写入失败");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

 

四、PiepedWriter,PiepedReader和PipedOutputStream,PipedInputStream一样的

 

五、RandomAccessFile

 

  1. 该类不是IO体系中的子类,但是它是IO包的成员,因为它具备读和写功能
  2. 此类的实例支持对随机访问文件的读取和写入。
  3. 其实完成读写的原理就是内部封装了直接输入流和输出流
  4. 内部封装了一个大型 byte 数组。
  5. 存在指向该隐含数组的光标或索引,称为文件指针;
  6. 输入操作从文件指针开始读取字节,并随着对字节的读取而前移此文件指针。
  7. 如果随机访问文件以读取/写入模式创建,则输出操作也可用;
  8. 输出操作从文件指针开始写入字节,并随着对字节的写入而前移此文件指针。
  9. 写入隐含数组的当前末尾之后的输出操作导致该数组扩展。
  10. 该文件指针可以通过 getFilePointer 方法读取,并通过 seek 方法设置。 
  11. 通常,如果此类中的所有读取例程在读取所需数量的字节之前已到达文件末尾,则抛出 EOFException(是一种 IOException)。
  12. 如果由于某些原因无法读取任何字节,而不是在读取所需数量的字节之前已到达文件末尾,则抛出 IOException,而不是 EOFException。
  13. 需要特别指出的是,如果流已被关闭,则可能抛出 IOException。 
public class RandomAccessFile implements DataOutput, DataInput, Closeable
{
	//创建从中读取和向其中写入(可选)的随机访问文件流,该文件由 File 参数指定。
	public RandomAccessFile(File file,
                        String mode)
                 throws FileNotFoundException{}

	//创建从中读取和向其中写入(可选)的随机访问文件流,该文件具有指定名称
	public RandomAccessFile(String name,
                        String mode)
                 throws FileNotFoundException{}

	/**
	 * 通过构造函数可以看出,该类只能操作文件,而且操作文件还有模式
	 * mode 参数指定用以打开文件的访问模式。
	 * "r" 以只读方式打开。调用结果对象的任何 write 方法都将导致抛出 IOException。
	 * "rw" 打开以便读取和写入。如果该文件尚不存在,则尝试创建该文件,如果存在则不会覆盖
	 * "rws" "rwd"
	 */
	
	//关闭此随机访问文件流并释放与该流关联的所有系统资源。关闭的随机访问文件不能执行输入或输出操作,而且不能重新打开。
	public void close()
           throws IOException{}

	//返回此文件中的当前偏移量,到此文件开头的偏移量(以字节为单位),在该位置发生下一个读取或写入操作
	public long getFilePointer()
                    throws IOException{}

	//返回按字节测量的此文件的长度
	public long length()
            throws IOException{}

	//从此文件中读取一个数据字节。以整数形式返回此字节,范围在 0 到 255 (0x00-0x0ff)。
	public int read()
         throws IOException{}

	public int read(byte[] b)
         throws IOException{}

	public int read(byte[] b,
                int off,
                int len)
         throws IOException{}

	//从此文件读取一个 boolean。此方法从该文件的当前文件指针开始读取单个字节。值 0 表示 false。其他任何值表示 true
	public final boolean readBoolean()
                          throws IOException{}

	//从此文件读取一个有符号的八位值。此方法从该文件的当前文件指针开始读取一个字节。如果读取的字节为 b,其中 0 <= b <= 255\
	public final byte readByte()
                    throws IOException{}

	//从此文件读取一个字符。此方法从该文件的当前文件指针开始读取两个字节。如果按顺序读取的字节为 b1 和 b2,其中 0 <= b1, b2 <= 255
	public final char readChar()
                    throws IOException{}

	//从此文件读取一个 double,8个字节
	public final double readDouble()
                        throws IOException{}

	readInt readFloat 读4个字节 readLong readShort 类似的操作

	//按单字节值将 boolean 写入该文件。值 true 写出为值 (byte)1;值 false 写出为值 (byte)0。写入从文件指针的当前位置开始
	public final void writeBoolean(boolean v)
                        throws IOException{}

	//按两个字节将 short 写入该文件,先写高字节。写入从文件指针的当前位置开始
	public final void writeShort(int v)
                      throws IOException{}

	write方法和read方法一一对应

	//设置到此文件开头测量到的文件指针偏移量,在该位置发生下一个读取或写入操作。
	public void seek(long pos)
          throws IOException{}

	//设置此文件的长度。如果 length 方法返回的文件的现有长度大于 newLength 参数,则该文件将被截短
	public void setLength(long newLength)
               throws IOException{}

	//尝试跳过输入的 n 个字节以丢弃跳过的字节。返回跳过的实际字节数。如果 n 为负数,则不跳过任何字节
	public int skipBytes(int n)
              throws IOException{}
}

 

六、示例

 

    利用RandomAccessFile可以实现对文件的分段读写

 

import java.io.*;
public class RandomAccessFileDemo {

	public static void main(String[] args) throws IOException {
		RandomAccessFile raf = new RandomAccessFile("e:/mm.txt","rw");
		//writeFile(raf);
		//readFile(raf);
		//writeFile2(raf);
		readFile2(raf);
	}
	public static void writeFile(RandomAccessFile raf) throws IOException
	{
		raf.write("张三".getBytes());
		raf.writeInt(97);
		raf.write("李四".getBytes());
		raf.writeInt(98);
		raf.close();
	}
	public static void readFile(RandomAccessFile raf) throws IOException
	{
		byte[] buf = new byte[4];
		raf.read(buf);
		String name = new String(buf);
		int age = raf.readInt();
		System.out.println(name+":"+age);
	}
	public static void writeFile2(RandomAccessFile raf) throws IOException
	{
		raf.seek(8);
		raf.write("王武".getBytes());
		raf.writeInt(99);
		raf.close();
	}
	public static void readFile2(RandomAccessFile raf) throws IOException
	{
		raf.skipBytes(8);
		byte[] buf = new byte[4];
		raf.read(buf);
		String name = new String(buf);
		int age = raf.readInt();
		System.out.println(name+":"+age);
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics