控制台程序,生成一些二进制整型值并且将它们写入到文件中。

 import java.nio.file.*;
import java.nio.*;
import java.io.*; public class StreamOutputToFile { public static void main(String[] args) {
final int count = 50; // Number of values
long[] fiboNumbers = {0L,0L}; // Array of 2 elements
int index = 0; // Index to fibonumbers
ByteBuffer buf = ByteBuffer.allocate(count*8); // Buffer for output
LongBuffer longBuf = buf.asLongBuffer(); // View buffer for type long
Path file = Paths.get(System.getProperty("user.home")).resolve("Beginning Java Struff").resolve("fibonnaci.bin");
try {
// Create parent directory if it doesn't exist
Files.createDirectories(file.getParent());
} catch(IOException e) {
System.err.println("Error creating directory: " + file.getParent());
e.printStackTrace();
System.exit(1);
} try(BufferedOutputStream fileOut = new BufferedOutputStream(Files.newOutputStream(file))){
// Generate Fibonacci numbers in buffer
for(int i = 0 ; i < count ; ++i) {
if(i < 2)
fiboNumbers[index] = i;
else
fiboNumbers[index] = fiboNumbers[0] + fiboNumbers[1];
longBuf.put(fiboNumbers[index]);
index = ++index%2;
}
// Write the numbers to the file
fileOut.write(buf.array(), 0, buf.capacity());
System.out.println("File written.");
} catch(IOException e) {
System.err.println("Error writing file: " + file);
e.printStackTrace();
}
}
}

Java基础之写文件——通过缓冲流写文件(StreamOutputToFile)的更多相关文章

  1. JAVA基础复习与总结<八> 缓冲流_数据流_对象流_IO总结

    缓冲流.数据流以及对象流 一.缓冲流 缓冲流的概念:在读写的时候,对于单字节的读取会造成硬盘的频繁读写,增加访问次数,降低了读取文件的效率.而引入缓冲流之后,就可以将多个字节写入缓冲区,在缓冲区积累之 ...

  2. 使用文件流与使用缓冲流完成文件的复制操作性能对比,文件流 FileInputStream FileOutputStream 缓冲流: BufferedInputStream BufferedOutputStream

    package seday06; import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOExc ...

  3. Java IO流之【缓冲流和文件流复制文件对比】

    与文件流相比,缓冲流复制文件更快 代码: package Homework; import java.io.BufferedOutputStream; import java.io.File; imp ...

  4. File类的特点?如何创建File类对象?Java中如何操作文件内容,什么是Io流Io流如何读取和写入文件?字节缓冲流使用原则?

    重难点提示 学习目标 1.能够了解File类的特点(存在的意义,构造方法,常见方法) 2.能够了解什么是IO流以及分类(IO流的概述以及分类) 3.能够掌握字节输出流的使用(继承体系结构介绍以及常见的 ...

  5. Java分享笔记:使用缓冲流复制文件

    [1] 程序设计 /*------------------------------- 1.缓冲流是一种处理流,用来加快节点流对文件操作的速度 2.BufferedInputStream:输入缓冲流 3 ...

  6. Java基础学习 -- I/O系统、流

    Java类库里有四个表示流的抽象父类:InputStream.OutputStream.Reader.Writer. 其中 InputStream 和 OutputStream 是对字节进行操作的输入 ...

  7. Java 基础【10】 I/O流概念分析整理

    转载地址:http://blog.csdn.net/yuebinghaoyuan/article/details/7388059 java.io 中的流,可以从不同的角度进行分类. 按照数据流的方向不 ...

  8. Java I/O第二篇 之 (缓冲流 随机流 数组流 数据流)

    1:缓冲流 BufferedReader  BufferedWriter 具有较强的读写能力,能单独读写一行数据,能够减少对硬盘的访问次数. /** * 缓冲流 能够增强对问价数据的可读性,减少访问读 ...

  9. Java 基础【13】 I/O流概念分析整理

    转载地址:http://blog.csdn.net/yuebinghaoyuan/article/details/7388059 java.io 中的流,可以从不同的角度进行分类. 按照数据流的方向不 ...

随机推荐

  1. 判断返回数据是否为 null

    判断是否为null //尖括号                if ([regeocode.city isEqual:[NSNull class]]) {                    NSL ...

  2. 通过SocketLog快速分析OneThink程序

    通过SocketLog快速分析OneThink程序 http://www.thinkphp.cn/topic/10846.html   浏览:2332 发布日期:2014/02/08 分类:技术分享 ...

  3. lua5.2版本在VS2010下的环境搭建

    第一次使用脚本语言,第一次使用解释性语言 公司的现在维护的游戏开发的比较早,采用的lua还是比较早的版本,像NPC脚本系统就是使用lua+luabind结合的module模式,服务器端没涉及到,所以不 ...

  4. nginx日志配置[转]

     * * * sh /home/zyf/sh/cut_nginx_log.sh 这样就每天的0点1分把nginx日志重命名为日期格式,并重新生成今天的新日志文件。 日志对于统计排错来说非常有利的。本文 ...

  5. 7.PHP内核探索:Apache模块介绍

    Apache概述 Apache是目前世界上使用最为广泛的一种Web Server,它以跨平台.高效和稳定而闻名.按照去年官方统计的数据,Apache服务器的装机量占该市场60%以上的份额.尤其是在 X ...

  6. 模板-高精度BigInteger

    #include <bits/stdc++.h> using namespace std; struct BigInteger { static const int BASE = 1000 ...

  7. nginx配置相关

    一.autoindex on; 能解决无端端的403问题. 二.NGINX配置超时时间 1.啥时候用到 用来设置请求资源和服务器返回的时间,保证一个请求占用固定时间,超出后报504超时!这样可以保证一 ...

  8. [转]C++ string的trim, split方法

    很多其他语言的libary都会有去除string类的首尾空格的库函数,但是标准C++的库却不提供这个功能.但是C++string也提供很强大的功能,实现trim这种功能也不难.下面是几种方法: 1.使 ...

  9. PHP数据库基础

    PHP数据库基础: 1.varchar:字符串,用于姓名班级,地址等,地址一般长50,姓名长20 2.int:整数,用于成绩,序号等 3.float:小数 4.bit:布尔型,用于性别等 5.时间也用 ...

  10. 自动将每日的日志增量导入到hive中

    一:大纲介绍 1.导入方式 load data local inpath 'local_file_path' into table tbname partition (date='',hour='') ...