Java基础之读文件——使用通道读取混合数据2(ReadPrimesMixedData2)
控制台程序,本例读取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)的更多相关文章
- Java基础之读文件——使用通道读取混合数据1(ReadPrimesMixedData)
控制台程序,本例读取Java基础之写文件部分(PrimesToFile2)写入的Primes.txt. 方法一:可以在第一个读操作中读取字符串的长度,然后再将字符串和二进制素数值读入到文本中.这种方式 ...
- Java基础之读文件——使用输入流读取二进制文件(StreamInputFromFile)
控制台程序,读取Java基础之读文件部分(StreamOutputToFile)写入的50个fibonacci数字. import java.nio.file.*; import java.nio.* ...
- Java基础之读文件——使用通道读二进制数据(ReadPrimes)
控制台程序,本例读取Java基础之写文件部分(PrimesToFile)写入的primes.bin. import java.nio.file.*; import java.nio.*; import ...
- Java基础之读文件——使用缓冲读取器读取文件(ReaderInputFromFile)
控制台程序,本例读取Java基础之写文件部分(WriterOutputToFile)写入的Saying.txt. import java.io.*; import java.nio.file.*; i ...
- Java基础之读文件——使用通道随机读取文件(RandomFileRead)
import java.nio.file.*; import java.nio.channels.FileChannel; import java.io.IOException; import jav ...
- Java基础之读文件——使用通道随机读写文件(RandomReadWrite)
控制台程序,使用通道随机读写primes_backup.bin文件. import static java.nio.file.StandardOpenOption.*; import java.nio ...
- Java基础之读文件——使用通道复制文件(FileBackup)
控制台程序,除了使用Files类中使用copy()方法将文件复制外,还可以使用FileChannel对象复制文件,连接到输入文件的FileChannel对象能直接将数据传输到连接到输出文件的FileC ...
- Java基础之读文件——从文件中读取文本(ReadAString)
控制台程序,使用通道从缓冲区获取数据,读取Java基础之写文件(BufferStateTrace)写入的charData.txt import java.nio.file.*; import java ...
- Java基础之写文件——创建通道并且写文件(TryChannel)
控制台程序,创建一个文件并且使用通道将一些文本写入到这个文件中. import static java.nio.file.StandardOpenOption.*; import java.nio.c ...
随机推荐
- centos7 安装及配置
第一步 下载centoshttps://www.centos.org/download/CentOS-7.0-1406-x86_64-DVD.iso:这个镜像(DVD image)包括了那些可以用安装 ...
- selenium 使用笔记
下面一段代码是使用selenium访问网页一个小实例 #!/usr/bin/python# -*- coding: utf-8 -*- '''Created on Dec 6, 2013 @autho ...
- ECSHOP二次开发指南
ECSHOP二次开发指南 发布时间:2013-05-28 12:47:00 来源: 评论:0 点击: 次 [字号:大 中 小] QQ空间新浪微博腾讯微博人人网豆瓣网百度空间百度搜藏开心网复制更 ...
- shell 中的数学计算
1.1.第一种——expr格式:expr 操作数 1 操作符 操作数 2举例: 1 expr 1 + 2 TMP=$(expr 1 + 2) 2 expr 1 + 4 / 3 TM ...
- MVC 扩展方法特点
.NET MVC 3中扩展方法特点: (1)扩展类的名称以Extensions结尾: (2)扩展类的类型是static: (3)扩展方法至少有一个参数,第一个参数必须指定该方法作用于哪个类型,并且该参 ...
- miniproject black jack--Fail
第一部分 下载这个小项目的程序模板并回顾card类的定义.这个类已经执行了所以你的任务是自己熟悉下代码.开始,通过粘贴card类定义到程序模板中并验证我们的代码如预期那样工作. 实现“__init__ ...
- P1092 虫食算 NOIP2002
为了测试stl 30分的暴力写法... #include <bits/stdc++.h> using namespace std; const int maxn = 11; int n; ...
- quanpailie quanbianli
#include<stdio.h>char data[5]={'a','b','c','d','e'};int vist[5]={0};char step[5]={0};char bu[5 ...
- 2016/4/21 关于jquery复习
jQuert AJAX [1]jQuery load()方法 :是AJAX方法, 从服务器加载数据,并把数据放入被选元素中 语法: $(selector).load(URL,data,callback ...
- 【转】UnityVS(Visual Studio Tools For Unity)的安装与使用
Unity 的开发者们,尤其是微软系的Unity开发者们,用Mono是不是烦死了?你是不是跟我一样,用vs来写代码,用Mono来跟踪调试?好麻烦啊好麻烦. 也许你会说,傻逼你不会用UnityVS插件么 ...