FileOutputStream
これは、2 つの 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」という 2 つのメッセージが表示されます。
別のインスタンスによって現在書き込み用に開かれているファイルを読み取り/書き込み/削除用に開かないようにする必要があります。現在書き込み用に開いているファイルを変更しないようにするにはどうすればよいですか?