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. TensorFlow报错module 'tensorflow' has no attribute 'xxx'解决办法

    原因:TensorFlow2.0版本修改了许多函数名字 tf.sub()更改为tf.subtract() tf.mul()更改为tf.multiply() tf.types.float32更改为tf. ...

  2. python数据分析工具 | matplotlib

    不论是数据挖掘还是数学建模,都免不了数据可视化的问题.对于 Python 来说,matplotlib 是最著名的绘图库,它主要用于二维绘图,当然也可以进行简单的三维绘图.它不但提供了一整套和 Matl ...

  3. G 树的难题

    时间限制 : 10000 MS   空间限制 : 165536 KB 评测说明 : 1s,128m 问题描述 给出一个无根树.树有N个点,边有权值.每个点都有颜色,是黑色.白色.灰色这三种颜色之一,称 ...

  4. vulnhub~DC-9

    首先对于整体的思路,网上有很多的教程,如果国内的不能满足建议‘fanqiang’,Google有很多大佬,各个部分都有详细的说明.但又由于每个人的环境都有所不同,所以会在同样的命令行执行后出现不同的错 ...

  5. Codeforces Round #565 (Div. 3)--D. Recover it!--思维+欧拉筛

    D. Recover it! Authors guessed an array aa consisting of nn integers; each integer is not less than ...

  6. 2019级第一次月赛暨ACM工作室第一次招新赛、补题赛

    A:最简单签到,没有之一 Description 此题简单如题意,就是求最大值 Input 多组输入 每组输入输入一串字符串(包括字母和数字),长度小于500 Output 每行输出字符ASCII值与 ...

  7. Linux学习第10天-命令执行顺序控制与管道

    学习重点: cut,grep,wc,sort命令的使用 管道的理解 一.顺序执行多条命令 当我们需要使用apt-get安装一个软件,然后安装完成后立即运行安装的软件(或命令工具),又恰巧你的主机才更换 ...

  8. JVM系列十(虚拟机性能监控神器 - BTrace).

    BTrace 是什么? BTrace 是一个动态安全的 Java 追踪工具,它通过向运行中的 Java 程序植入字节码文件,来对运行中的 Java 程序热更新,方便的获取程序运行时的数据信息,并且,保 ...

  9. 用curl调用https接口

    今天在windows下用curl类获取微信token一直返回false,查阅资料后,发现是https证书的锅,在curl类中加上这两条,问题瞬间解决. curl_setopt($ch, CURLOPT ...

  10. 多级分销概念 MongoDB||MySQL

    1.背景 购物软件中提供推荐注册返利机制,A->B,A->C,B->D,B->E.被邀请人只有一对一的上级,上级对下级是一对多,用户可以一直邀请用户. 2.实现方法 2.1.M ...