java.io.FileNotFoundException:该进程无法访问该文件,因为它正在被另一个进程使用) [英] java.io.FileNotFoundException: The process cannot access the file because it is being used by another process)

查看:1418
本文介绍了java.io.FileNotFoundException:该进程无法访问该文件,因为它正在被另一个进程使用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个侦听器,该侦听器连续侦听队列,当消息到达时,它将把该消息写到文件中.

I written a listener which listen to queue continuously, as message arrive it should write that message to file.

@Override
public void handleDelivery(String consumerTag,
                           Envelope envelope,
                           AMQP.BasicProperties properties,
                           byte[] body) throws IOException {
    String response = new String(body);

    String routingKey = envelope.getRoutingKey();
    String contentType = properties.getContentType();
    String correlationId = properties.getCorrelationId();
    System.out.println("response "+ counter+ " :: "+ response);
        try {
            ResponseWriter.responseWrite(response, correlationId);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    Runnable task = new VariableLengthTask(this, envelope.getDeliveryTag(), channel, 1000);
    executorService.submit(task);

}

上述方法中的行

ResponseWriter.responseWrite(response, correlationId);

将调用以下方法:

File file =new File(defaultExcelPath);
        FileOutputStream fop = null;
        PrintWriter writer = null;
        synchronized(file) {
            try {
                List<String> datalist = new ArrayList<String>();
                // add details to datalist....
                // write to the file ..
                .....
                .....
                .....

                fop = new FileOutputStream(defaultExcelPath, true);
                writer = new PrintWriter(fop);
                for(String data : datalist) {
                    writer.println(data);
                }
                writer.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                //e.printStackTrace();
                throw e;
            } finally {
                if(fop!= null) fop.close();
                if(writer!= null) writer.close();
            }
      }

但是由于处理程序方法在几秒钟内列出了100条以上的消息,因此文件写入方法抛出的异常低于

but due to handler method listing more than 100 messages in some seconds, file writing method throws below exception

java.io.FileNotFoundException:C:\ Files \ performanceEvaluator.csv(该进程无法访问该文件,因为该文件正在被另一个进程使用)

java.io.FileNotFoundException: C:\Files\performanceEvaluator.csv (The process cannot access the file because it is being used by another process)

如何使同时写入文件(或任何其他方式)的代码

how I can make the code which write to the file concurrently (or any other way..)

推荐答案

这是关键字synchronized起作用的地方.以public synchronized void function()方式声明函数有两件事:

This is where the keyword synchronized comes into play. Declaring a function in this: public synchronized void function() manner does two things:

  1. 首先,不可能对同一对象的两次同步方法调用进行交织.当一个线程正在为对象执行同步方法时,所有其他为同一对象块调用同步方法的线程(挂起执行),直到第一个线程对该对象完成为止.
  2. 第二,当同步方法退出时,它会自动与之前对同一对象的同步方法的任何调用建立先发生关系.这样可以保证对对象状态的更改对所有线程都是可见的.

来自 Java教程.

这篇关于java.io.FileNotFoundException:该进程无法访问该文件,因为它正在被另一个进程使用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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