控制台程序,本例读取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. MySQL存储引擎总结

    MySQL存储引擎总结 作者:果冻想 字体:[增加 减小] 类型:转载   这篇文章主要介绍了MySQL存储引擎总结,本文讲解了什么是存储引擎.MyISAM.InnoDB.MEMORY.MERGE等内 ...

  2. HTML: 字符實體

    在HTML編寫中,有些字符不適合直接在代碼中寫出,比如>,<, (space空格),",',&等等,這時我們按照一定的格式將它們寫出,比如(大於)> 符號,我們用 ...

  3. MySQL线程独享[转]

    一.前言在 MySQL 中,线程独享内存主要用于各客户端连接线程存储各种操作的独享数据,如线程栈信息,分组排序操作,数据读写缓冲,结果集暂存等等,而且大多数可以通过相关参数来控制内存的使用量。 二.线 ...

  4. Centos下使用Heartbeat实现集群[转]

    Linux 包括 CentOS 下高可用性(HA:High Availability)集群方案很多,而 Heartbeat 是比较常见和性价比比较高的一种。一、硬件及网络连接 群集一般需要2台以上服务 ...

  5. mysql select 练习题

    10. 查询Score表中的最高分的学生学号和课程号.(子查询或者排序) select sno,cno from score where degree  in(select max(degree) f ...

  6. rpc rmi http

    1.RPC与RMI (1)RPC 跨语言,而 RMI只支持Java. (2)RMI 调用远程对象方法,允许方法返回 Java 对象以及基本数据类型,而RPC 不支持对象的概念,传送到 RPC 服务的消 ...

  7. php--yii2.0框架的curl

    yii2.0框架的增删改查 //插入操作  save() $customer=new Customer(); $customer->name=‘小熊‘; $customer->save() ...

  8. HTML5新标签和属性

    1.<time>标签(支持IE9以上和其他浏览器) 今年是<time datetime="2015-12-12">2015年</time> &l ...

  9. new char[]和new char()的区别

    new char[1];分配一块连续的内存,也就是一个数组,里面有1个元素new   char(1);分配一块内存,有一个元素,该元素被初始化为1

  10. SQL PROMPT 取消dbo前缀

    SQL Prompt 无疑大大提高了开发者的效率,高效而简单,特别适合大型的数据库脚本编写,但遗憾得是至今没有可供使用的中文版本.SQL Prompt 默认对象名前面会有 dbo 前缀,在一些场合这样 ...