情况如下在我的应用程序中我有 2 个并行运行的线程,其中一个线程的目的是捕获屏幕截图,第二个线程的目的是重命名第一个线程保存在特定文件夹中的屏幕截图线程-应用程序的代码如下-:
CapturingAndRenamingSimultaneously.java
/**
* Created with IntelliJ IDEA.
* User: AnkitSablok
* Date: 15/1/13
* Time: 1:03 PM
* To change this template use File | Settings | File Templates.
*/
package com.tian.screenshotcapture;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class CapturingAndRenamingSimultaneously {
public static void main(String[] args) {
// we use the linked blocking queue here to resolve the concurrency issues
final BlockingQueue<File> queue = new LinkedBlockingQueue<File>(1024);
new Thread(new Runnable() {
@Override
public synchronized void run() {
try {
System.out.println("In the capture thread now");
CaptureScreenshots.captureScreenshots(queue);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public synchronized void run() {
try {
while (true) {
System.out.println("In the rename thread now");
RenameScreenShots.renameScreenshots(queue);
Thread.sleep(5000);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
CaptureScreenshots.java
/**
* Created with IntelliJ IDEA.
* User: AnkitSablok
* Date: 15/1/13
* Time: 12:35 PM
* To change this template use File | Settings | File Templates.
*/
// this code is used to capture the screenshots
package com.tian.screenshotcapture;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class CaptureScreenshots {
// this code is used to capture the screen shots
public static void captureScreenshots(BlockingQueue<File> queue) throws Exception {
String fileName = "C:\\Users\\ankitsablok\\Desktop\\Screenshots";
int index = 0;
for (; ; ) {
++index;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRectangle);
ImageIO.write(image, "jpg", new File(fileName + "\\i" + index + ".jpg"));
queue.put(new File(fileName + "\\i" + index + ".jpg"));
Thread.sleep(1000);
}
}
}
重命名屏幕截图.java
/**
* Created with IntelliJ IDEA.
* User: AnkitSablok
* Date: 15/1/13
* Time: 12:49 PM
* To change this template use File | Settings | File Templates.
*/
package com.tian.screenshotcapture;
import java.io.*;
import java.util.concurrent.BlockingQueue;
public class RenameScreenShots {
public static void renameScreenshots(BlockingQueue<File> queue) throws IOException, InterruptedException {
for (int i = 0; i < queue.size(); ++i) {
File sourceFile = queue.take();
System.out.println("The filename is : " + sourceFile.getName());
if (sourceFile.getName().contains("sent")) {
} else {
System.out.println("The modified name of the source file is :" + sourceFile.getName().substring(0,
sourceFile.getName().indexOf('.'))
+ "sent" + ".jpg");
File newFile = new File(sourceFile.getParent() + "/" + sourceFile.getName().substring(0,
sourceFile.getName().indexOf('.'))
+ "sent" + ".jpg");
byte[] buffer = new byte[1024];
FileInputStream fis = new FileInputStream(sourceFile);
FileOutputStream fos = new FileOutputStream(newFile);
int length;
while ((length = fis.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
System.out.println("The file was deleted successfully : " + sourceFile.delete());
fis.close();
fos.close();
}
}
}
}
我希望同步对文件夹的访问,即当一个进程将图像写入文件夹之后,另一个进程应该能够重命名文件夹中的图像。但是我无法同步对写入和读取屏幕截图的文件夹的访问,在某些时候使用上面的代码会给出 FileNotFoundException 错误,其他进程正在使用该文件。我该如何解决这个问题。
提前致谢。