Java Reference
In-Depth Information
correct, you have to ensure that the temp file gets created on the same disk partition (drive
letter or mount point) as the user's file:
src/main/java/com/darwinsys/io/FileSaver.java
// package com.darwinsys.io;
public
public class
class
FileSaver
FileSaver
{
private
private enum
enum
State
{
/** The state before and after use */
AVAILABLE
,
/** The state while in use */
INUSE
}
private
private
State state
;
private
private final
final
File inputFile
;
private
private final
final
File tmpFile
;
private
private final
final
File backupFile
;
public
public
FileSaver
(
File input
)
throws
throws
IOException
{
// Step 1: Create temp file in right place
this
this
.
inputFile
=
input
;
tmpFile
=
new
new
File
(
inputFile
.
getAbsolutePath
() +
".tmp"
);
tmpFile
.
createNewFile
();
tmpFile
.
deleteOnExit
();
backupFile
=
new
new
File
(
inputFile
.
getAbsolutePath
() +
".bak"
);
state
=
State
.
AVAILABLE
;
}
/**
* Return a reference to the contained File object, to
* promote reuse (File objects are immutable so this
* is at least moderately safe). Typical use would be:
* <pre>
* if (fileSaver == null ||
* !(fileSaver.getFile().equals(file))) {
* fileSaver = new FileSaver(file);
* }
* </pre>
*/
public
public
File
getFile
() {
return
return
inputFile
;
}