RandomAccessFile 相对其它流多了一个seek() 方法指定指针的偏移量。

1、指定起始位置读取剩余内容

public static void test01() throws IOException {
RandomAccessFile raf = new RandomAccessFile("src/com/xzlf/io/CopyFile.java", "r");
// 随机读取
raf.seek(2);
byte[] flush = new byte[1024];
int len = -1;
while((len=raf.read(flush)) != -1) {
System.out.println(new String(flush,0,len));
}
raf.close();
}

2、指定起始位置和实际大小

public static void test02() throws IOException {
RandomAccessFile raf = new RandomAccessFile("src/com/xzlf/io/CopyFile.java", "r");
// 起始位置
int beginPos = 2+1026;
// 实际大小
int actualSize = 1026;
// 随机操作
raf.seek(beginPos);
// 缓冲容器
byte[] flush = new byte[1024];
int len = -1;
while ((len = raf.read(flush)) != -1) {
if(actualSize > len) {
System.out.println(new String(flush, 0, len));
actualSize -= len;
}else {
System.out.println(new String(flush, 0, actualSize));
break;
}
}
raf.close();
}

3、封装

package com.xzlf.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector; public class SplitFile {
// 源头
private File src;
// 目的地
private String destDir;
// 所有分割后文件存储路劲
List<String> destPath;
// 每块大小
int blockSize;
// 块数
int size;
public SplitFile(String srcPaht, String destDir) {
this(srcPaht, destDir, 1024);
} public SplitFile(String srcPath, String destDir, int blockSize) {
this.src = new File(srcPath);
this.destDir = destDir;
this.blockSize = blockSize;
this.destPath = new ArrayList<String>(); init();
} // 初始化
private void init() {
// 总长度
long len = this.src.length();
// 块数
this.size = (int) Math.ceil(len * 1.0 / blockSize);
for(int i = 0; i < size; i++) {
this.destPath.add(this.destDir + "/" + i + "-" + this.src.getName());
}
} /**
* 分割
* 1、计算每一块的起始位置及大小
* 2、分割
* @throws IOException
*/
public void split() throws IOException {
File src = this.src;
// 总长度
long len = src.length();
// 起始位置和实际大小
int beginPos = 0;
int actualSize = 0;
for(int i = 0; i < size; i++) {
beginPos = i * blockSize;
if(i == size -1) { // 最后一块
actualSize = (int) len;
}else {
actualSize = blockSize;
len -= actualSize; // 剩余量
}
System.out.println(i + "-->" + beginPos);
splitDetail(i, beginPos, actualSize);
}
} /**
* 指定第 i 块、起始位置、实际长度
* @param i
* @param beginPos
* @param actualSize
* @throws IOException
*/
private void splitDetail(int i, int beginPos, int actualSize) throws IOException {
RandomAccessFile raf = new RandomAccessFile(src, "r");
RandomAccessFile raf2 = new RandomAccessFile(destPath.get(i), "rw");
raf.seek(beginPos);
byte[] flush = new byte[1024];
int len = -1;
while((len = raf.read(flush)) != -1) {
if(actualSize > len) {// 获取本次读取的所有内容
raf2.write(flush, 0, len);
actualSize -= len;
}else {
raf2.write(flush, 0, actualSize);
break;
}
}
raf.close();
raf2.close();
} /**
* 文件的合并
* @param destPaht
* @throws IOException
*/
public void merge(String destPath) throws IOException {
// 输出流
OutputStream os = new BufferedOutputStream(new FileOutputStream(destPath, true));
Vector<InputStream> vi = new Vector<InputStream>(); // 输入流
for (int i = 0; i < this.destPath.size(); i++) {
vi.add(new BufferedInputStream(new FileInputStream(this.destPath.get(i))));
}
SequenceInputStream sis = new SequenceInputStream(vi.elements());
// 文件拷贝
byte[] flush = new byte[1024];
int len = -1;
while((len = sis.read(flush)) != -1) {
os.write(flush, 0, len);
} // 释放资源
os.flush();
os.close();
sis.close();
}
}

4、添加测试代码测试:

	public static void main(String[] args) throws IOException {
// SplitFile sf = new SplitFile("src/com/xzlf/io/CopyFile.java", "dest");
SplitFile sf = new SplitFile("水电费.png", "dest");
sf.split();
// sf.merge("CopyFile.java");
sf.merge("水电费-merge.png");
}

Java IO 流 -- 随机读取和写入流 RandomAccessFile (文件分割和合并)的更多相关文章

  1. Java中的字符串流的读取和写入(创建文件并判断重复账户)

    各位我又来了!!哎!好心酸!我还没注册到三天!!没法登上博客的首页!!心累!! import java.io.BufferedOutputStream; import java.io.Buffered ...

  2. JAVA IO分析二:字节数组流、基本数据&对象类型的数据流、打印流

    上一节,我们分析了常见的节点流(FileInputStream/FileOutputStream  FileReader/FileWrite)和常见的处理流(BufferedInputStream/B ...

  3. Java IO(十二) 字符流 Writer 和 Reader

    Java IO(十二) 字符流 Reader和 Writer 一.介绍 涉及到文件(如果是纯文本文件形式)操作时,Java除了提供 FIle(文件和目录路径名的抽象表示形式) 和 FileDescri ...

  4. nodeks —— fs模块 —— 从流中 读取和写入数据

    Fs流读取和写入数据 使用文件流来读取大文件不会卡顿 1, 从流中读取数据 var fs = require("fs"); var data = ''; var count = 0 ...

  5. Java配置文件Properties的读取、写入与更新操作

    /** * 实现对Java配置文件Properties的读取.写入与更新操作 */ package test; import java.io.BufferedInputStream; import j ...

  6. 对Java配置文件Properties的读取、写入与更新操作

    http://breezylee.iteye.com/blog/1340868 对Java配置文件Properties的读取.写入与更新操作 博客分类: javase properties  对Jav ...

  7. 实现对Java配置文件Properties的读取、写入与更新操作

    /** * 实现对Java配置文件Properties的读取.写入与更新操作 */ package test; import java.io.BufferedInputStream; import j ...

  8. JAVA IO分析三:IO总结&文件分割与合并实例

    时间飞逝,马上就要到2018年了,今天我们将要学习的是IO流学习的最后一节,即总结回顾前面所学,并学习一个案例用于前面所学的实际操作,下面我们就开始本节的学习: 一.原理与概念 一.概念流:流动 .流 ...

  9. Java IO学习笔记三:MMAP与RandomAccessFile

    作者:Grey 原文地址:Java IO学习笔记三:MMAP与RandomAccessFile 关于RandomAccessFile 相较于前面提到的BufferedReader/Writer和Fil ...

随机推荐

  1. 调试 node.js 程序

    调试 node.js 程序 在程序开发中,如何快速的查找定位问题是一项非常重要的基本功.在实际开发过程中,或多或少都会遇到程序出现问题导致无法正常运行的情况,因此,调试代码就变成了一项无法避免的工作. ...

  2. coding++: java把一个整数拆分为单个值

    方式一: int num = 100; int[] ary = new int[(num+"").length()]; for(int i = ary.length-1;i> ...

  3. Redis在linux环境下的安装

    下载Redis安装包 wget http://download.redis.io/releases/redis-3.2.9.tar.gz 解压Redis安装包 tar -zxvf redis-3.2. ...

  4. Ubuntu系统查看命令命令使用方式

    如:[gzip]命令,可执行: (tf) duanyongchun@hc1217:~/pycharm_projects /3DUNet-Pytorch /data$ gzip --help 输出: 由 ...

  5. Ubuntu系统在Anaconda中安装Python3.6的虚拟环境

    原因:Anaconda的python版本是3.7的,TensorFlow尚不支持此版本,于是我们创建一个Python的虚拟环境以支持TensorFlow 创建tf环境 conda create --n ...

  6. SpringBoot系列(二)入门知识

    SpringBoot系列(二)入门知识 往期推荐 SpringBoot系列(一)idea新建springboot项目 引言 本来新建springboot项目应该放在入门知识这一章的,但是由于新建spr ...

  7. Jedis连接外部Redis

    Jedis连接外部Redis 1.在服务器开放端口redis默认6379,如果有宝塔面板则还需要在宝塔放行6379端口 2.修改redis.conf 注释掉 绑定IP 127.0.0.1 # bind ...

  8. Java数据结构与排序

    一.引子:想要给ArrayList排序却发现没有排序方法?你有两种选择:        1.换用TreeSet:     2.使用Collection.sort(List<T> list) ...

  9. 使用错误代码对象进行C++错误处理

    原文发表于codeproject,由本人翻译整理分享于此. 前言 我已经使用了本文描述的代码和机制近20年了,到目前为止,我还没有找到更好的方法来处理大型C++项目中的错误.最初的想法是从一篇文章(D ...

  10. 微信小程序template富文本插件image宽度被js强制设置

    这段时间一直做微信小程序,过程中遇到了一个问题,这个问题一直没有得到完美的解决. 问题描述: 在Web编程中经常会引入template插件,这个插件是封装好,我们通常的做法是直接引入,配置简单,好用, ...