第2部分:Web Start应用程序:并发问题 [英] Part-2: Web Start Application: Concurrency Issue

查看:91
本文介绍了第2部分:Web Start应用程序:并发问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此线程上给出建议,

我尝试使用FileLock,但是,当我在文件中写入内容时,excel文件以某种方式损坏,文件中没有任何内容(该文件为空,其中没有内容)

I tried using FileLock, however, when I write something in the file, somehow excel file gets corrupted and there is nothing in the file (it gets empty, no contents in there)

我有以下方法:

void writeIntoTheFile(XSSFWorkbook defectWorkBook, File fileToWrite) {

            FileLock lock = null;
            FileChannel channel = null;
            FileOutputStream out = null;
            try {
                //fileToWrite contains an excel .xlsx file
                channel = new RandomAccessFile(fileToWrite, "rw").getChannel();
                lock = channel.tryLock();
                if (lock != null) {
                    out = new FileOutputStream(fileToWrite.getPath());
                    defectWorkBook.write(out);

                } else {
                    JOptionPane.showMessageDialog(null, "Another instance is already writing, Try after a few seconds.", "Write Error...", JOptionPane.INFORMATION_MESSAGE);
                }
                    out.close();

            } catch (Exception e) {
                e.getMessage();
            } 


           finally{   
            if (lock != null && lock.isValid()) {
                    lock.release();
                }
            channel.close();

            } 

        }

问题似乎出在下面的代码上:

Seems the problem is coming from below code:

channel = new RandomAccessFile(fileToWrite, "rw").getChannel();
 lock = channel.tryLock();

有人可以在这个问题上帮助我吗?

Can anyone please help me on this issue?

拉胡尔

推荐答案

我怀疑您在try {}块中遇到了异常,但是您的捕获时钟没有清除它. e.getMessage()将获取消息,但不会打印出来.

I suspect you are getting an exception in your try{} block, but your catch clock doesn't prin it. e.getMessage() will get the message but not print it.

我建议使用类似 e.printStackTrace()的方式(对于生产系统,您可能希望做一些更有用的事情,但例外情况除外).

I suggest something along the lines of e.printStackTrace() (for a production system you would want to do something more useful with the exception).

这篇关于第2部分:Web Start应用程序:并发问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆