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

方法一:可以在第一个读操作中读取字符串的长度,然后再将字符串和二进制素数值读入到文本中。这种方式的唯一不足是:这不是一种有效读取文件的方式,因为有很多的读操作,其中的每个都读取非常少量的数据。

 import java.nio.file.*;
import java.nio.channels.FileChannel;
import java.io.IOException;
import java.nio.ByteBuffer; public class ReadPrimesMixedData {
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 (FileChannel inCh = (FileChannel)Files.newByteChannel(file)){
ByteBuffer lengthBuf = ByteBuffer.allocate(8);
int strLength = 0; // Stores the string length ByteBuffer[] buffers = {
null, // Byte buffer to hold string
ByteBuffer.allocate(8) // Byte buffer to hold prime
}; while(true) {
if(inCh.read(lengthBuf) == -1) // Read the string length,
break; // if its EOF exit the loop lengthBuf.flip(); // Extract the length and convert to int
strLength = (int)lengthBuf.getDouble(); // Now create the buffer for the string
buffers[0] = ByteBuffer.allocate(2*strLength); if(inCh.read(buffers) == -1) { // Read the string & binary prime value
// Should not get here!
System.err.println("EOF found reading the prime string.");
break; // Exit loop on EOF
} System.out.printf(
"String length: %3s String: %-12s Binary Value: %3d%n",
strLength,
((ByteBuffer)(buffers[0].flip())).asCharBuffer().toString(),
((ByteBuffer)buffers[1].flip()).getLong()); // Clear the buffers for the next read
lengthBuf.clear();
buffers[1].clear();
}
System.out.println("\nEOF reached.");
} catch(IOException e) {
e.printStackTrace();
}
}
}

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

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

    控制台程序,本例读取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. 使用spring等框架的web程序在Tomcat下的启动顺序及思路理清

    大牛请绕过,此文仅针对自己小白水平,对web程序的启动流程做个清晰的回顾. 一.使用spring等框架的web程序在Tomcat下的启动流程 1)Tomcat是根据web.xml来启动的.首先到web ...

  2. thinkPHP实现瀑布流的方法

    thinkPHP实现瀑布流的方法 文章TAG:thinkphp 瀑布流 时间:2014-11-29来源:www.aspku.com 作者:源码库 文章热度: 131 ℃ 过期已备案域名,注册就能用!终 ...

  3. 大话数据结构(五)(java程序)——顺序存储结构的插入与删除

    获得元素操作 对于线性表的顺序存储结构来说,我们要实现getElement操作,即将线性表的第i个位置元素返回即可 插入操作 插入算法思路: 1.如果插入位置不合理,抛出异常 2.如果插入表的长度大于 ...

  4. laravel 外键schema RBAC

    $table->bigIncrements('id')  ;   Incrementing ID (primary key) using a " UNSIGNED BIG INTEGE ...

  5. vector 初始化所有方法

    简介:vector可用于代替C中的数组,或者MFC中的CArray,从许多说明文档或者网上评论,一般一致认为应该多用vector,因为它的效率更高,而且具备很好的异常安全性.而且vector是STL推 ...

  6. FW Docker为容器分配指定物理网段的静态IP

    官方有关于网桥和IP配置的文档地址:https://docs.docker.com/articles/networking/ 1.宿主机(系统采用ubuntu-14.04.1-server-amd64 ...

  7. CDH介绍

    本文引用自:Cloudera 系列2:CDH介绍http://www.aboutyun.com/thread-18379-1-1.html(出处: about云开发) CDH提供: 灵活性-存储任何类 ...

  8. 通过全局设置过滤器,就能让所有窗口都可移动,而不是都要继承指定QDialog

    #include "appinit.h" #include <QMouseEvent> #include <QApplication> #include & ...

  9. Qt 之 自定义提示信息框—迅雷风格(模拟QDialog类的exec()方法) good

    http://blog.csdn.net/goforwardtostep/article/details/53614830

  10. 20145211 《Java程序设计》第2周学习总结——桃花依旧笑春风

    教材学习内容总结 基本类型 整数 short 2字节,int 4字节,long 8字节 字节 byte 1字节 浮点数 float 4字节,double 8字节 字符 char 2字节(包括字母.汉字 ...