java 写入缓存_Java通用工具类----缓存块写入

package com.download;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.RandomAccessFile;

import java.nio.ByteBuffer;

import java.nio.channels.FileChannel;

import java.util.Arrays;

/**

* function1: 生成测试数据 10个5M文件

* function2: 启动线程,读取文件,如果缓存块超过10M,则生成落地文件

*

*/

public class Naming {

static ByteBuffer cache = ByteBuffer.allocate(10 * 1024 * 1024);

private static final Object object = new Object();

public static void main(String[] args) {

// 生成测试数据

// String test = "E:\\tmp\\offset\\test";

//

// byte[] b = new byte[5 * 1024 * 1024];

// BufferedOutputStream bos = null;

// for (int i = 0; i < 10; i++) {

// try {

// String name = System.currentTimeMillis() + "-" + i;

// bos = new BufferedOutputStream(new FileOutputStream(new File(test + "/" + name + ".txt")));

// Arrays.fill(b, (byte)i);

// bos.write(b);

// bos.flush();

// bos.close();

// } catch (FileNotFoundException e) {

// e.printStackTrace();

// } catch (IOException e) {

// e.printStackTrace();

// }

// }

String input = "E:\\tmp\\offset\\input";

String output = "E:\\tmp\\offset";

Thread inputThread = new Thread(new parseInput(input), "inputThread");

Thread outputThread = new Thread(new output(output), "outputThread");

inputThread.start();

outputThread.start();

}

public static class parseInput implements Runnable {

private String input;

public parseInput(String input) {

this.input = input;

}

@Override

public void run() {

System.out.println("input Thread is running...");

while (true) {

if (new File(input).list().length != 0) {

for (File file : new File(input).listFiles()) {

if (file.getName().endsWith("txt")) {

System.out.println("开始解析文件" + file.getName());

try {

RandomAccessFile accessFile = new RandomAccessFile(file, "r");

FileChannel channel = accessFile.getChannel();

long currentPosition = 0L;

int readLength = channel.read(cache, currentPosition);

while (readLength != -1) {

currentPosition += readLength;

if (!cache.hasRemaining()) {

synchronized(object) {

object.wait();

}

}

readLength = channel.read(cache, currentPosition);

}

channel.close();

accessFile.close();

file.delete();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

} else {

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

}

public static class output implements Runnable {

private String output;

public output(String output) {

this.output = output;

}

@Override

public void run() {

System.out.println("output Thread is running...");

while (true) {

if (!cache.hasRemaining()) { //缓存没有剩余空间时,开始读取数据

System.out.println("缓存块已满,开始序列化到硬盘");

cache.flip();

long blockid = System.currentTimeMillis();

BufferedOutputStream bos = null;

try {

bos = new BufferedOutputStream(new FileOutputStream(new File(output + "/" + blockid)));

bos.write(cache.array());

cache.clear();

synchronized (object) {

object.notifyAll();

}

System.out.println("序列化结束, 文件名为" + blockid);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

} else {

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值