Java基础之读文件——使用通道读取混合数据1(ReadPrimesMixedData)
控制台程序,本例读取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)的更多相关文章
- Java基础之读文件——使用通道读取混合数据2(ReadPrimesMixedData2)
控制台程序,本例读取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 ...
随机推荐
- 代理和block反向传值
代理传值: // SendViewController.h #import <UIKit/UIKit.h> @protocol SendInFor <NSObject> -(v ...
- ecshop session丢失问题
ecshop session丢失问题 电子商务PHP 用ecshop搭建了一个电子商务的系统,本地测试一切正常.放到服务器上出现问题: 症状: 点着点着经常无故退出,感觉session被清空 ...
- Unity3D之游戏架构脚本该如何来写(转)
这篇文章主要想大家说明一下我在Unity3D游戏开发中是如何写游戏脚本的,对于Unity3D这套游戏引擎来说入门极快,可是要想做好却非常的难.这篇文章的目的是让哪些已经上手Unity3D游戏引擎的朋友 ...
- Web前端开发:什么是页面重回(repaints)与回流(reflow)
前言:什么是重绘与回流? 重绘(repaints)是一个元素外观的改变所触发的浏览器行为,例如改变vidibility.outline.背景色等属性.浏览器会根据元素的新属性重新绘制,使元素呈现新的外 ...
- 页面静态化1 --- 概念(Apache内置压力测试工具使用方法)
三个概念 ①静态网址: http://127.0.0.1/regis.html ②动态网址:在请求页面时可以动态的传一些值进去. http://127.0.0.1/regis.php?id=45&am ...
- Heterogeneous System Architecture
https://en.wikipedia.org/wiki/Heterogeneous_System_Architecture Steps performed when offloading calc ...
- simplify the design of the hardware forming the interface between the processor and thememory system
Computer Systems A Programmer's Perspective Second Edition Many computer systems place restrictions ...
- Running Kafka At Scale
https://engineering.linkedin.com/kafka/running-kafka-scale If data is the lifeblood of high technolo ...
- go安装windows源码
直接安装就好,下面是安装地址http://pan.baidu.com/s/1gdDFi9t
- ImageX用来做Windows OEM部署
https://technet.microsoft.com/en-us/library/cc722145(v=ws.10).aspx http://download.csdn.net/user/phc ...