I/O 和 内存映射文件

内存映射文件最快,其次是缓冲器和通道(nio),最后是一般的io操作



旧的I/O:java.io.*

输入流(字节流,字符流)(部分)                                                 将字节流转换为字符流用InputStreanRreader

InputStream                                             Reader                                 

FileInputStream                                      FileReader

FilterInputStream                                   FilterReader

            |                                                             |

DataInputStream                                       BufferedReader                                                           DataInputStream 主要操作不同的基本类型数据以及String对象,如果要用到

                                                                      readLine()方法选用bufferedReader否则首选DataInputStream


BufferedInputStream                                BufferedReader 主要起缓冲的作用


输出流(字节流,字符流)

将上面的InputStream 换成 OutputStream                                Reader   换成 Writer



独立的类 RandomAccessFile    继承Object 类实现了 Datainput 和DataOutput  接口      即可读又可写   支持搜寻方法(只有它支持)并且只适用文件


package Number_18;

import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * 读写随机访问文件
 * 
 * @author he
 * 
 */
public class P544 {

	public static final String file = "F:/IOtest/b/a.txt";

	// 读取文件
	public static void display() throws IOException {
		// 以只读的方式打开文件
		RandomAccessFile rFile = new RandomAccessFile(file, "r");
		for (int i = 0; i < 7; i++) {
			System.out.println("i=" + i + " Value=" + rFile.readDouble());

		}
		System.out.println(rFile.readUTF());
		rFile.close();

	}

	public static void main(String[] args) throws IOException {
		// 写文件
		RandomAccessFile wFile = new RandomAccessFile(file, "rw");
		for (int i = 0; i < 7; i++) {
			wFile.writeDouble(i * 1.414);
		}
		wFile.writeUTF("UTF-8");
		wFile.close();
		display();
		wFile = new RandomAccessFile(file, "rw");
		// 因为double字节为8所以 用5*8的形式查找第五个双精度值 并修改
		wFile.seek(5 * 8);
		wFile.writeDouble(2.333);
		wFile.close();
		display();
	}

}




新的I/O: java.nio.*

目的是为了提高速度:    通道和缓冲器

ByteBuffer  --  唯一与通道交互的缓冲器(储存为加工的字节)     并且有as 方法转换为视图缓冲器       (对视图的任何修改都会映射成为对ByteBuffer中数据的修改)

在旧的I/O类库中只有fileinputstream   fileoutputstream 和 Randomaccessfile  有产生FileChannel (通道) 的方法     



package Number_1802;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 新I/O 通道,缓冲器 唯一直接与通道交互的缓冲器 ByteBuffer
 * FileInputStream,FileOutpuoStream,RandomAccessFile 有getChannel方法返回
 * FileChannel类型的对象
 * 
 * @author he
 * 
 */
public class P552 {
	@SuppressWarnings("resource")
	public static void main(String[] args) throws IOException {
		// 创建通道fc
		FileChannel fc = new FileOutputStream("F:/IOtest/b/data.txt")
				.getChannel();
		// 将some txt 变为Byte数组 包装到缓冲区 , 再写入通道fc
		fc.write(ByteBuffer.wrap("some txt ".getBytes()));
		fc.close();

		fc = new RandomAccessFile("F:/IOtest/b/data.txt", "rw").getChannel();
		// 设置位置移动到最后 方便再写入数据
		fc.position(fc.size());
		fc.write(ByteBuffer.wrap("some more ".getBytes()));
		fc.close();

		fc = new FileInputStream("F:/IOtest/b/data.txt").getChannel();
		// 分配新的字节缓冲区
		ByteBuffer bb = ByteBuffer.allocate(1024);
		// 将通道中的数据写入缓冲区
		fc.read(bb);
		// 缓冲区反转
		bb.flip();

		while (bb.hasRemaining()) {
			System.out.print((char) bb.get());
		}

	}

}


内存映射文件

 允许我们创建和修改那些因为太大而不能放入内存的文件     映射文件中的所有输出 必须使用RandomAccessFile

运行后产生一个100多m的TXT文件   慎重!!!


package Number_1802;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 内存映射文件
 * 
 * @author he
 * 
 */
public class P563 {
	private static final int length = 0x8FFFFFF; // 128M
//	static int length=0x7d;

	@SuppressWarnings("resource")
	public static void main(String[] args) throws IOException, IOException {

		MappedByteBuffer out = new RandomAccessFile("F:/IOtest/b/data3.txt","rw")
				.getChannel().map(
				// 将此通道的文件区域映射到内存 , 设置为只读,开始0 大小为length
						FileChannel.MapMode.READ_WRITE, 0, length);
		for (int i = 0; i < length; i++) {
			out.put((byte)'x');
		}
		System.out.println("Finished writing");
		for (int i = length / 2; i < length / 2 + 6; i++) {
			System.out.println((char)out.get(i));
		}

	}

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值