我製作了兩個應用程序,旨在同時運行(我不想將它們組合在一起),一個應用程序從某個文件讀取,另一個應用程序寫入該應用程序。當其中一個或另一個沒有運行錯誤時,但是如果它們都運行get獲取訪問被拒絕錯誤。第一 相關代碼:JAVA:我的兩個應用程序如何訪問同一個文件?
class MakeImage implements Runnable {
@Override
public void run() {
File file = new File("C:/Users/jeremy/Desktop/New folder (3)/test.png");
while (true) {
try{
//make image
if(image!=null)
{
file.createNewFile();
ImageIO.write(image, "png", file);
hello.repaint();}}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
第二的相關代碼:
BufferedImage image = null;
try {
// Read from a file
image = ImageIO.read(new File("C:/Users/jeremy/Desktop/New folder (3)/test.png"));
}
catch(Exception e){
e.printStackTrace();
}
if(image!=null)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
baos.flush();
byte[] imageInByte = baos.toByteArray();
baos.close();
returns=Base64.encodeBase64String(imageInByte);
}
我看了看這個:Java: how to handle two process trying to modify the same file,但那是當兩者都書面方式到這裏只有一個文件。我按照前者的答案中的建議嘗試了以後的重試方法,但沒有任何運氣。任何幫助將不勝感激。
數據庫引擎? – Randy 2012-07-17 18:06:06
複製寫入? – Joel 2012-07-17 18:14:04