【发布时间】:2011-10-29 06:09:39
【问题描述】:
这是一个代码 sn-p,我正在打开一个文件以同时使用两个 FileOutputStreams 进行写入。
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”。
我需要确保我没有打开当前打开以供另一个实例写入的文件进行读/写/删除。如何确保不修改当前打开以供写入的文件?
【问题讨论】:
标签: java android io android-file