这是一个代码片段,我正在打开一个文件以FileOutputStream
同时使用两个 s 进行写入。
FileOutputStream fis = null;
File openFile = new File("myfile");
try {
fis = new FileOutputStream(openFile);
Toast toast = Toast.makeText(getApplicationContext(), "Opened", Toast.LENGTH_SHORT);
toast.show();
}
catch (FileNotFoundException e1) {
Toast toast = Toast.makeText(getApplicationContext(), "FileNotFound", Toast.LENGTH_SHORT);
toast.show();
}
// now try to open it again
FileOutputStream fis2 = null;
try {
fis2 = new FileOutputStream(openFile);
Toast toast = Toast.makeText(getApplicationContext(), "Opened2", Toast.LENGTH_SHORT);
toast.show();
}
catch (Exception e1) {
Toast toast = Toast.makeText(getApplicationContext(), "Exception: " + e1.getMessage(), Toast.LENGTH_SHORT);
toast.show();
}
我最终收到两条Toast
消息,“Opened”和“Opened2”。
我需要确保我没有打开当前打开以供另一个实例写入的文件进行读/写/删除。如何确保不修改当前打开以供写入的文件?