Java基础之读文件——使用输入流读取二进制文件(StreamInputFromFile)
控制台程序,读取Java基础之读文件部分(StreamOutputToFile)写入的50个fibonacci数字。
import java.nio.file.*;
import java.nio.*;
import java.io.*; public class StreamInputFromFile {
public static void main(String[] args) { Path file = Paths.get(System.getProperty("user.home")).resolve("Beginning Java Struff").resolve("fibonnaci.bin"); if(!Files.exists(file)) {
System.out.println(file + " does not exist. Terminating program.");
System.exit(1);
} final int count = 6; // Number of values to be read each time // Buffer to hold count values
ByteBuffer buf = ByteBuffer.allocate(8*count); LongBuffer values = buf.asLongBuffer();
byte[] bytes = buf.array(); // Backing array for buf
int totalRead = 0; // Total value read
try(BufferedInputStream fileIn = new BufferedInputStream(Files.newInputStream(file))){
int numberRead = 0;
while(true) {
numberRead = fileIn.read(bytes, 0, bytes.length);
if(numberRead == -1) // EOF reached
break;
totalRead += numberRead/8; // Increment total for(int i = 0 ; i < numberRead/8 ; ++i) // Access as many as there are
System.out.format("%12d", values.get()); System.out.println(); // New line
values.flip(); // Reset for next input
}
System.out.format("%d values read.%n", totalRead);
} catch(IOException e) {
System.err.println("Error writing file: " + file);
e.printStackTrace();
}
}
}
Java基础之读文件——使用输入流读取二进制文件(StreamInputFromFile)的更多相关文章
- Java基础之读文件——使用通道读取混合数据2(ReadPrimesMixedData2)
控制台程序,本例读取Java基础之写文件部分(PrimesToFile2)写入的Primes.txt. 方法二:设置一个任意容量的.大小合适的字节缓冲区并且使用来自文件的字节进行填充.然后整理出缓冲区 ...
- Java基础之读文件——使用通道读取混合数据1(ReadPrimesMixedData)
控制台程序,本例读取Java基础之写文件部分(PrimesToFile2)写入的Primes.txt. 方法一:可以在第一个读操作中读取字符串的长度,然后再将字符串和二进制素数值读入到文本中.这种方式 ...
- Java基础之读文件——使用缓冲读取器读取文件(ReaderInputFromFile)
控制台程序,本例读取Java基础之写文件部分(WriterOutputToFile)写入的Saying.txt. import java.io.*; import java.nio.file.*; i ...
- Java基础之读文件——从文件中读取文本(ReadAString)
控制台程序,使用通道从缓冲区获取数据,读取Java基础之写文件(BufferStateTrace)写入的charData.txt import java.nio.file.*; import java ...
- Java基础之读文件——使用通道读二进制数据(ReadPrimes)
控制台程序,本例读取Java基础之写文件部分(PrimesToFile)写入的primes.bin. import java.nio.file.*; import java.nio.*; import ...
- 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中的读文件,文件的创建,写文件
前言 大家好,给大家带来Java中的读文件,文件的创建,写文件的概述,希望你们喜欢 读文件 public static void read(String path,String filename){ ...
随机推荐
- php安装json模块
centOS上因为看php源码中没有json模块,于是采用pecl自动编译安装:# yum install php-devel# yum install php-pear# yum install g ...
- PHP 中获取当前时间[Datetime Now]
在 PHP 中可以通过date()获取当前时间,在>5.2的版本中最好还是用 datetime 类型 date() <?php echo date('Y-m-d H:i:s'); ?> ...
- apache磁盘缓存配置
确保mod_cache和mod_disk_cache是开启的 配置如下: CacheDefaultExpire 86400 #失效时间,单位秒CacheEnable disk / #缓存路径 ...
- 去除字符串中空格的方法(2016.1.12P141-2)
// forif来处理空格 // 方法一 String str = " ww sse rr"; String str1;// 定义一个中间变量 String str2 = &quo ...
- OpenMP for Fortran
OpenMP for Fortran OpenMP Directive Syntax of OpenMP compiler directive for Fortran: !$OMP Directive ...
- 获取真实ip的报告
今天登录九秒社团 http://www.9miao.com/的时候忘记了用户名和密码,尝试了5次都没登录成功,网站弹出提示15分钟后才能再次登录.我纳闷它是怎么判断用户的登录次数,这时候用户还没有登录 ...
- struts1 plugin
struts plugin 在struts.xml中注册之后,在系统启动之后调用 init 方法,通常在init方法中进行转化器的注册,在destory中移除转化器 1. struts文件 <p ...
- 【运维工具】logrotate 日志管理神器
服务器经常会产生各种各样的日志文件,我们需要定期清理 日志的分类 系统日志 应用日志 系统日志 例如系统的history 历史信息 crontab的运行日志 一般系统日志系统都帮我们运维好了,不 ...
- iOS 键盘隐藏
IOS7 点击空白处隐藏键盘的几种方法 IOS开发中经常要用到输入框,默认情况下点击输入框就会弹出键盘,但是必须要实现输入框return的委托方法才能取消键盘的显示,对于用户体验来说很不友好,我 ...
- textview自适应高度的计算方法
http://blog.csdn.net/smking/article/details/22221441