java 异步写文件,通过Java中的异步文件通道写入时丢失数据

I am trying to use asynchronousFileChannel to write the date into a text file. I made 3 jar file of the program with the AsynchronousFileChannel and compiled all 3 jars simultaneously through command prompt to read 3 different text files and output to one common temporary file

I have 2000 records in my test files(3) to be read,but the output in the common temporary file is missing some of the records,the output should have 6000 records but it shows only 5366 or 5666 or sometimes less than that.

I am not able to figure out why some data is lost as it is the functionality of a asynchronousFileChannel.

Here is the code for the java program using asynchronousfilechannel.

class Writer(){

public void writeOut(ReadableData fileData)

throws InterruptedException {

Path file = null;

AsynchronousFileChannel asynchFileChannel = null;

String filePath = tempFileName;

try {

file = Paths.get(filePath);

asynchFileChannel = AsynchronousFileChannel.open(file,

StandardOpenOption.WRITE, StandardOpenOption.CREATE);

CompletionHandler handler = new CompletionHandler() {

@Override

public void completed(Integer result, Object attachment) {

if (result == Integer.MAX_VALUE) {

log.debug("Attachment: " + attachment + " " + result

+ " bytes written");

log.debug("CompletionHandler Thread ID: "

+ Thread.currentThread().getId());

}

result++;

}

@Override

public void failed(Throwable e, Object attachment) {

try {

throw e;

} catch (Throwable e1) {

e1.printStackTrace();

}

log.debug("File Write Failed Exception:");

e.printStackTrace();

}

};

String printData = fileData.getId() + "|"

+ fileData.getName() + "|" + fileData.getEmpId()

+ "|" + fileData.getServieId() + "|" + "\n";

asynchFileChannel.write(ByteBuffer.wrap(printData.getBytes()),

asynchFileChannel.size(), "file write", handler);

log.debug(printData);

}

}

catch (IOException e) {

e.printStackTrace();

log.error(e.getMessage());

} finally {

}

}

}

}

and this is my class to read data from 3 files:

public class FileReader1 {

static Logger log = Logger.getLogger(FileHandlerNorthBoundMain.class

.getName());

Writer wrO=new Writer();

public static void main(String[] args) throws IOException,

IllegalFileFormatException, InterruptedException {

String filePath = "C:\\Users\\Public\\testdata1.csv"; //"C:\\Users\\Public\\testdata2.csv"; "C:\\Users\\Public\\testdata3.csv";

File file = new File(filePath);

log.info("Fetching data.... from: " + filePath);

ArrayList list = new ArrayList();

FileInputStream fs = null;

BufferedReader reader = null;

String Name;

int Id, EmpId, ServiceId;

ReadableData readableData = null;

int count = 0;

fs = new FileInputStream(file);

reader = new BufferedReader(new InputStreamReader(fs));

String line = reader.readLine();

while (line != null) {

StringTokenizer st = new StringTokenizer(line, "\\|");

while (st.hasMoreTokens()) {

try {

Id = Integer.parseInt(st.nextToken());

Name = st.nextToken();

EmpId = Integer.parseInt(st.nextToken());

ServiceId = Integer.parseInt(st.nextToken());

readableData = new ReadableData(Id,

, Name, EmpId,ServiceId);

wrO.writeOut(readableData);

list.add(count, readableData);

count = count++;

} catch (Exception ex) {

log.error("Illegal File Format");

throw new IllegalFileFormatException("Illegal File Format");

}

}

line = reader.readLine();

}

reader.close();

}

解决方案

Modify your Writer class with the following code part with asynchronousFileChannel lock()

byte[] test = printData.getBytes();

Future featureLock = asynchFileChannel.lock();

log.info("Waiting for the file to be locked ...");

FileLock lock = featureLock.get();

if (lock.isValid()) {

log.debug(printData);

Future featureWrite = asynchFileChannel.write(

ByteBuffer.wrap(test), asynchFileChannel.size());

log.info("Waiting for the bytes to be written ...");

int written = featureWrite.get();

log.info("I’ve written " + written + " bytes into "

+ file.getFileName() + " locked file!");

lock.release();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java异步上传文件可以使用Java NIO的AsynchronousFileChannel类。这个类提供了异步文件的方法,可以实现高效的文件上传操作。 以下是一个简单的示例代码: ```java import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.AsynchronousFileChannel; import java.nio.channels.CompletionHandler; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; public class AsyncFileUploader { public static void main(String[] args) throws IOException { Path file = Paths.get("path/to/file"); AsynchronousFileChannel channel = AsynchronousFileChannel.open( file, StandardOpenOption.WRITE); ByteBuffer buffer = ByteBuffer.allocate(1024); long position = 0; channel.write(buffer, position, buffer, new CompletionHandler<Integer, ByteBuffer>() { @Override public void completed(Integer result, ByteBuffer attachment) { if (attachment.hasRemaining()) { channel.write(buffer, position + result, buffer, this); } else { System.out.println("File upload completed!"); } } @Override public void failed(Throwable exc, ByteBuffer attachment) { System.out.println("File upload failed: " + exc.getMessage()); } }); } } ``` 这个示例代码使用AsynchronousFileChannel类打开要上传的文件,并使用ByteBuffer类分配一个缓冲区。然后,它使用write()方法将缓冲区的内容异步写入文件。如果缓冲区还有剩余的数据,它会继续调用write()方法。当所有数据写入文件,它会输出一条消息表示文件上传完成。如果写入文件发生错误,它会输出一条错误消息。 需要注意的是,异步文件上传的效率取决于硬件和操作系统的支持。在某些情况下,它可能比同步上传更慢。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值