外部归并排序(1)——通道映射方法的文件读写
2013-12-27 23:17
691 查看
1、核心类
映射方式读取文件涉及的核心类:java.nio.channels.FileChannel。该类通过 FileInputStream 实例的 getChannel 方法所获得的通道将允许进行读取操作;通过 FileOutputStream 实例的 getChannel 方法所获得的通道将允许进行写入操作。对于该类的其他详细解释,可参见JDK_API_1_6_zh_CN.CHM的FileChannel类。
2、输入流InputChannelImpl
该类构造函数有两个参数:输入文件,读取输入文件的byte模板的大小(即一次读取多长的字符)。
该类提供三个方法:打开一个文件来读取(open())、读取下一节数据(read_next())和判断文件是否读完(end_of_stream())。
源代码如下:
package com.emms.raw.channel.impl; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.ClosedChannelException; import java.nio.channels.FileChannel; import org.apache.log4j.Logger; /** * 通过映射方式读取文件。 * @author Administrator * */ public class InputChannelImpl { /** * 打印日志对象。 */ Logger logger = Logger.getLogger(InputChannelImpl.class); /** * 文件输入流。 */ private FileInputStream fileInputStream; /** * 输入流对于的文件通道。 */ private FileChannel fileChannel; /** * 作为映射模板的字节缓冲区。 */ private ByteBuffer byteBuffer; /** * */ private int byteBufferSize; /** * 用户输入的文件。 */ private File file; /** * 从文件中读取的映射模板大小的字节数组。 */ private byte[] bytelist; /** * 是否读取完毕。 */ private boolean isEnd = true; /** * 对应文件的输入流。 * @param file 输入文件。 * @param size 读取输入文件的byte模板的大小,即一次读取多长的字符。 */ public InputChannelImpl(File file,int size) { this.file = file; this.byteBufferSize = size; } /** * 打开一个文件来读取. */ public void open() { try { fileInputStream = new FileInputStream(this.file); } catch (FileNotFoundException e) { logger.error("Open File : " + file.getPath() + " failed",e); } fileChannel = fileInputStream.getChannel(); logger.info("Open File : " + file.getPath() + " success"); } /** * 读取下一个. */ public byte[] read_next() { if(fileChannel.isOpen()) { byteBuffer = ByteBuffer.allocate(byteBufferSize); try { fileChannel.read(byteBuffer); } catch (IOException e) { logger.error("Read data failed, because : IOException",e); } bytelist = byteBuffer.array(); } logger.info("Return bytelist length: " + bytelist.length + ". Read from " + file.getPath()); return bytelist; } /** * 当一个判断return true时,说明到达流的末尾. * @return */ public boolean end_of_stream() { try { if(fileChannel.position() < fileChannel.size()) { isEnd = false; } else { isEnd = true; close(); } } catch (ClosedChannelException e) { logger.info("The fileChannel is Closed"); } catch (IOException e) { logger.error("Get fileChannel position failed, because : IOException",e); } logger.info("Return isEnd : " + isEnd + ". Read from " + file.getPath()); return isEnd; } /** * 关闭输入流和读取数据的通道。 */ private void close() { if(fileChannel != null) { try { fileChannel.close(); } catch (IOException e) { logger.error("Close fileChannel failed, because : IOException",e); } } if(fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { logger.error("Close fileInputStream failed, because : IOException",e); } } } }
3、输出流OutputChannelImpl
该类构造函数有一个参数:需要创建的文件路径字符串,文件路径字符串的合法性需要调用者校验。
该类提供三个方法:创建一个新文件(create())、在流中写入一个元素(write())和关闭一个流(close())。
源代码如下:
package com.emms.raw.channel.impl; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import org.apache.log4j.Logger; /** * 通过映射方式输出文件。 * @author Administrator * */ public class OutputChannelImpl { /** * 打印日志对象。 */ Logger logger = Logger.getLogger(OutputChannelImpl.class); /** * 需要创建的文件路径。 */ private String filePath; /** * 通过文件路径,创建的文件。 */ private File file; /** * 输出流。 */ private FileOutputStream fileOutputStream; /** * 输出流对于的文件通道。 */ private FileChannel fileChannel; /** * 作为映射模板的字节缓冲区。 */ private ByteBuffer byteBuffer; /** * 构造方法,必须设置所需创建的文件路径。 * @param filePath */ public OutputChannelImpl(String filePath) { this.filePath = filePath; } /** * 创建一个新文件. */ public void create() { file = new File(this.filePath); try { fileOutputStream = new FileOutputStream(file); } catch (FileNotFoundException e) { logger.error("Create File : " + this.filePath + " failed",e); } fileChannel = fileOutputStream.getChannel(); logger.info("Create File : " + this.filePath + " success"); } /** * 在流中写入一个元素. * @param byteList */ public void write(byte[] byteList) { if(fileChannel.isOpen()) { byteBuffer = ByteBuffer.wrap(byteList); try { fileChannel.write(byteBuffer); } catch (IOException e) { logger.error("Write byteList length : " + byteList.length + ". to " + this.filePath + " failed, because : IOException",e); } logger.info("Write byteList length : " + byteList.length + ". to " + this.filePath + " success"); } else { logger.info("FileChannel is not opened"); } } /** * 关闭一个流. */ public void close() { if(fileChannel != null) { try { fileChannel.close(); } catch (IOException e) { logger.error("Close fileChannel failed, because : IOException",e); } } if(fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { logger.error("Close fileOutputStream failed, because : IOException",e); } } } }
[/code]
相关文章推荐
- JAVA NIO文件映射、通道、流读写文件示例
- JAVA NIO文件映射、通道、流读写文件示例
- 通过流实现文件读写的方法(中)
- Linq to sql:外部映射文件
- 使用Spring 来配置 hibernate 映射文件的 两种方法
- java大文件读写操作,java nio 之MappedByteBuffer,高效文件/内存映射
- java 读写文件[多种方法]
- php使用fputcsv()函数csv文件读写数据的方法(解决中文乱码问题)
- Python3读写INI配置文件简单方法
- mybatis逆向生成映射文件实体类 单元测试test 增删改查方法
- C#读写txt文件的两种方法介绍
- hibernate引入装有实体类映射文件的配置方法
- C、C++和MFC中文件的读写方法
- Java-NIO(五):通道(Channel)的数据传输与内存映射文件
- 用内存映射读写大文件
- 在android开发中正确读写sdcard文件的方法
- 一种简便实效的配置文件读写方法
- 启动数据库时提示ORA-03113: 通信通道的文件结尾解决方法
- linux c通过文件描述符以及write和read方法对文件进行读写
- C#读写txt文件的两种方法介绍[转]