控制台程序,本例读取Java基础之写文件部分(PrimesToFile2)写入的Primes.txt。

方法二:设置一个任意容量的、大小合适的字节缓冲区并且使用来自文件的字节进行填充。然后整理出缓冲区中所有的内容。这种方式的问题是:缓冲区的内容可能会在读取文件的一个数据项时半途而断。这样一来,就必须做一些工作对此进行检测并且找出下一步要做的工作,但是这比第一种方式更加有效,因为这极大减少了用于读取整个文件所需的读操作数目。

本例的关键是缓冲区类提供的compact()方法,即压缩缓冲区。

 import java.nio.file.*;
import java.nio.channels.ReadableByteChannel;
import java.io.IOException;
import java.nio.ByteBuffer; public class ReadPrimesMixedData2 { public static void main(String[] args) {
Path file = Paths.get(System.getProperty("user.home")).resolve("Beginning Java Struff").resolve("primes.txt");
if(!Files.exists(file)) {
System.out.println(file + " does not exist. Terminating program.");
System.exit(1);
} try(ReadableByteChannel inCh = Files.newByteChannel(file)) {
ByteBuffer buf = ByteBuffer.allocateDirect(256);
buf.position(buf.limit()); // Set the position for the loop operation
int strLength = 0; // Stores the string length
byte[] strChars = null; // Array to hold the bytes for the string while(true) {
if(buf.remaining() < 8) { // Verify enough bytes for string length
if(inCh.read(buf.compact()) == -1)
break; // EOF reached
buf.flip();
}
strLength = (int)buf.getDouble(); // Get the string length // Verify enough bytes for complete string
if(buf.remaining() < 2*strLength) {
if(inCh.read(buf.compact()) == -1) {
System.err.println("EOF found reading the prime string.");
break; // EOF reached
}
buf.flip();
}
strChars = new byte[2*strLength]; // Array for string bytes
buf.get(strChars); // Get the bytes if(buf.remaining() < 8) { // Verify enough bytes for prime value
if(inCh.read(buf.compact()) == -1) {
System.err.println("EOF found reading the binary prime value.");
break; // EOF reached
}
buf.flip();
} System.out.printf("String length: %3s String: %-12s Binary Value: %3d%n",
strLength, ByteBuffer.wrap(strChars).asCharBuffer(),buf.getLong());
} System.out.println("\nEOF reached."); } catch(IOException e) {
e.printStackTrace();
}
}
}

Java基础之读文件——使用通道读取混合数据2(ReadPrimesMixedData2)的更多相关文章

  1. Java基础之读文件——使用通道读取混合数据1(ReadPrimesMixedData)

    控制台程序,本例读取Java基础之写文件部分(PrimesToFile2)写入的Primes.txt. 方法一:可以在第一个读操作中读取字符串的长度,然后再将字符串和二进制素数值读入到文本中.这种方式 ...

  2. Java基础之读文件——使用输入流读取二进制文件(StreamInputFromFile)

    控制台程序,读取Java基础之读文件部分(StreamOutputToFile)写入的50个fibonacci数字. import java.nio.file.*; import java.nio.* ...

  3. Java基础之读文件——使用通道读二进制数据(ReadPrimes)

    控制台程序,本例读取Java基础之写文件部分(PrimesToFile)写入的primes.bin. import java.nio.file.*; import java.nio.*; import ...

  4. Java基础之读文件——使用缓冲读取器读取文件(ReaderInputFromFile)

    控制台程序,本例读取Java基础之写文件部分(WriterOutputToFile)写入的Saying.txt. import java.io.*; import java.nio.file.*; i ...

  5. Java基础之读文件——使用通道随机读取文件(RandomFileRead)

    import java.nio.file.*; import java.nio.channels.FileChannel; import java.io.IOException; import jav ...

  6. Java基础之读文件——使用通道随机读写文件(RandomReadWrite)

    控制台程序,使用通道随机读写primes_backup.bin文件. import static java.nio.file.StandardOpenOption.*; import java.nio ...

  7. Java基础之读文件——使用通道复制文件(FileBackup)

    控制台程序,除了使用Files类中使用copy()方法将文件复制外,还可以使用FileChannel对象复制文件,连接到输入文件的FileChannel对象能直接将数据传输到连接到输出文件的FileC ...

  8. Java基础之读文件——从文件中读取文本(ReadAString)

    控制台程序,使用通道从缓冲区获取数据,读取Java基础之写文件(BufferStateTrace)写入的charData.txt import java.nio.file.*; import java ...

  9. Java基础之写文件——创建通道并且写文件(TryChannel)

    控制台程序,创建一个文件并且使用通道将一些文本写入到这个文件中. import static java.nio.file.StandardOpenOption.*; import java.nio.c ...

随机推荐

  1. PHP自动解压上传的rar文件

    PHP自动解压上传的rar文件   浏览:383 发布日期:2015/07/20 分类:功能实现 关键字: php函数 php扩展 大家都知道php有个zip类可直接操作zip压缩文件,可是用户有时候 ...

  2. 做了一个简易的git 代码自动部署脚本

    做了一个简易的git 代码自动部署脚本 http://my.oschina.net/caomenglong/blog/472665 发表于2个月前(2015-06-30 21:08)   阅读(200 ...

  3. A20板子上的触摸屏设备号变化后解决

  4. MySQL 5.6.3

    SHOW VARIABLES LIKE '%version%'; https://dev.mysql.com/doc/refman/5.6/en/explain.html As of MySQL 5. ...

  5. JVM学习笔记

    1. JVM中的直接引用和符号引用 JVM在装载class文件的时候,会有一步是将符号引用解析为直接引用的过程. 那么这里的直接引用到底是什么呢? 对于指向“类型”[Class对象].类变量.类方法的 ...

  6. PIVOT&UNPIVOT

    如果是家电销售员,那么可能需要统计每月日销售的彩电.冰箱.空调...最大值.最小值.平均值等 如果你是耳鼻喉科医生,那么可能需要统计月度年度日接客咽炎.喉炎.鼻炎...最大值.最小值.平均值等 如果你 ...

  7. BAT(批处理)获得参数

    原文转自:http://blog.csdn.net/luhouxiang/article/details/31733049 获取参数有2种, 一种为从命令行输入参数,使用两个%中间包含数字表示,数字从 ...

  8. Spring整合CXF之发布WebService服务

    今天我们来讲下如何用Spring来整合CXF,来发布WebService服务: 给下官方文档地址:http://cxf.apache.org/docs/writing-a-service-with-s ...

  9. JS删除数组条目中重复的条目

    [腾讯2015春招web前端开发练习卷] 请给Array本地对象增加一个原型方法,它用于删除数组条目中重复的条目(可能有多个),返回值是一个包含被删除的重复条目的新数组. Array.prototyp ...

  10. [LeetCode]题解(python):045-Jump game II

    题目来源 https://leetcode.com/problems/jump-game-ii/ Given an array of non-negative integers, you are in ...