37、IO流——随机访问流、合并流

随机访问流

RandomAccessFile
直接继承自Object类
该类不属于流,此类的实例支持对随机访问文件的读取和写入

构造方法
public RandomAccessFile(String name,String mode)throws FileNotFoundException
创建从中读取和向其中写入(可选)的随机访问文件流,该文件具有指定名称
将创建一个新的 FileDescriptor 对象来表示到文件的连接
参数:mode 参数指定用以打开文件的访问模式

含意
“r”以只读方式打开。调用结果对象的任何 write 方法都将导致抛出IOException
“rw”打开以便读取和写入。如果该文件尚不存在,则尝试创建该文件

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

public final void writeUTF(String str) throws IOException
使用 modified UTF-8 编码以与机器无关的方式将一个字符串写入该文件
首先会加入2个字节(数据:加入的字符串的字节数,不是字符串的个数)

public void seek(long pos) throws IOException
设置到此文件开头测量到的文件指针偏移量,在该位置发生下一个读取或写入操作
偏移量的设置可能会超出文件末尾。偏移量的设置超出文件末尾不会改变文件的长度
只有在偏移量的设置超出文件末尾的情况下对文件进行写入才会更改其长度
参数:pos - 从文件开头以字节为单位测量的偏移量位置,在该位置设置文件指针
使用

public static void main(String[] args) throws IOException {
	RandomAccessFile raf1 = new RandomAccessFile("2.txt","rw");
	raf1.writeInt(100);
	raf1.writeChar('a');
	raf1.writeUTF("哈哈");

	read();
}
	
public static void read() throws IOException {
	RandomAccessFile raf2 = new RandomAccessFile("2.txt","rw");
	int i=raf2.readInt();
	System.out.println(i);
	System.out.println(raf2.getFilePointer());
		
	System.out.println("-------------------------");
		
	char c=raf2.readChar();
	System.out.println(c);
	System.out.println(raf2.getFilePointer());
		
	System.out.println("-------------------------");
		
	String string=raf2.readUTF();
	System.out.println(string);
	System.out.println(raf2.getFilePointer());
		
	System.out.println("-------------------------");
		
	raf2.seek(4);
	char c2=raf2.readChar();
	System.out.println(c2);
}

合并流

SequenceInputStream
表示其他输入流的逻辑串联

构造方法
public SequenceInputStream(InputStream s1,InputStream s2)
通过记住这两个参数来初始化新创建的 SequenceInputStream(将按顺序读取这两个参数,先读取 s1,然后读取 s2),以提供从此 SequenceInputStream 读取的字节

public SequenceInputStream(Enumeration<? extends InputStream> e)
通过记住参数来初始化新创建的 SequenceInputStream,该参数必须是生成运行时类型为 InputStream 对象的 Enumeration 型参数
将按顺序读取由该枚举生成的输入流,以提供从此 SequenceInputStream 读取的字节
在用尽枚举中的每个输入流之后,将通过调用该流的 close 方法将其关闭

Vector类的成员方法:
public Enumeration elements()返回此向量的组件的枚举
返回的 Enumeration 对象将生成此向量中的所有项。生成的第一项为索引 0 处的项,然后是索引 1 处的项,依此类推
返回:此向量的组件的枚举

使用
两个拼接:

InputStream is1 = new FileInputStream("1.txt");
InputStream is2 = new FileInputStream("2.txt");
SequenceInputStream sis = new SequenceInputStream(is1,is2);
BufferedOutputStream name = new BufferedOutputStream(new FileOutputStream("copy.txt"));
byte[] bs=new byte[1024];
int length=0;
while ((length=sis.read(bs))!=-1) {
name.write(bs,0,length);
}
sis.close();
name.close();

多个拼接:

InputStream is1 = new FileInputStream("1.txt");
InputStream is2 = new FileInputStream("2.txt");
InputStream is3 = new FileInputStream("copy.txt");
Vector<InputStream> v = new Vector<InputStream>();
v.add(is1);
v.add(is2);
v.add(is3);
Enumeration<InputStream> enu = v.elements();
SequenceInputStream sis = new SequenceInputStream(enu);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy2.txt"));
byte[] bs = new byte[1024];
int length = 0;
while ((length = sis.read(bs)) != -1) {
	bos.write(bs, 0, length);
}
sis.close();
bos.close();
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值