public class ChangeWriter
extends java.io.Writer
Writer
will initially write a temporary file. When close()
is called, it compares the contents of the original file with the
temporary file. If the contents have changed, it will overwrite the
original file with the contents of the temporary file; otherwise, the
original file is left unchanged. Thus, the last time modified of the
original file is only changed when the contents change. The temporary
file is deleted when close()
is called.Constructor and Description |
---|
ChangeWriter(java.io.File destination,
java.io.File directory)
Create a writer to write a new file or overwrite an old file.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Complete writing to the original file if needed.
|
static java.io.Writer |
createWriter(java.lang.String filename,
java.lang.String directory)
Create a
Writer for a particular filename and directory
combination. |
protected void |
finalize()
Make sure to close the writer if the programmer should forget to do so.
|
void |
flush()
This will flush all the buffered output into the temporary file.
|
void |
write(char[] cbuf)
Write a character array.
|
void |
write(char[] cbuf,
int off,
int len)
Write part of a character array.
|
void |
write(int c)
Write a character.
|
void |
write(java.lang.String str)
Write a string.
|
void |
write(java.lang.String str,
int off,
int len)
Write a substring.
|
public ChangeWriter(java.io.File destination, java.io.File directory) throws java.io.IOException
createWriter(java.lang.String, java.lang.String)
instead of this
constructor; it uses the simpler FileWriter
if
destination does not already exist.destination
- this is the location of the new filedirectory
- this is the directory where the temporary
file will be created. If null
,
a system dependent temporary will be
used (e.g., /tmp on Unix).java.io.IOException
- something went wrong creating the Writer
.public static java.io.Writer createWriter(java.lang.String filename, java.lang.String directory) throws java.io.IOException
Writer
for a particular filename and directory
combination.filename
- the filename without any leading directory information.directory
- the directory name. This string will be prepended
to filename to get the whole path to the file.
If it's not an empty string, it should end with
the directory separator character,
File.separatorChar
.ChangeWriter
or
a FileWriter
. The final type
shouldn't really matter to the caller.java.io.IOException
- something went wrong creating the Writer
.public void write(java.lang.String str) throws java.io.IOException
write
in class java.io.Writer
str
- the string to be output.java.io.IOException
- There was a problem writing to the output device.
Writing to a closed ChangeWriter also produces an
IOException
.public void write(char[] cbuf) throws java.io.IOException
write
in class java.io.Writer
cbuf
- the character array to be output.java.io.IOException
- There was a problem writing to the output device.
Writing to a closed ChangeWriter also produces an
IOException
.public void write(java.lang.String str, int off, int len) throws java.io.IOException
write
in class java.io.Writer
str
- the whole stringoff
- the offset of the first character to be written.len
- the number of characters to write.java.io.IOException
- There was a problem writing to the output device.
Writing to a closed ChangeWriter also produces an
IOException
.public void write(int c) throws java.io.IOException
write
in class java.io.Writer
c
- the character to be output.java.io.IOException
- There was a problem writing to the output device.
Writing to a closed ChangeWriter also produces an
IOException
.public void write(char[] cbuf, int off, int len) throws java.io.IOException
write
in class java.io.Writer
cbuf
- the character array to be output.off
- the index of the first character to be written.len
- the number of characters to write.java.io.IOException
- There was a problem writing to the output device.
Writing to a closed ChangeWriter also produces an
IOException
.public void flush() throws java.io.IOException
close()
is
called.flush
in interface java.io.Flushable
flush
in class java.io.Writer
java.io.IOException
- There was a problem flushing the output device.
Flushing a closed ChangeWriter also produces an
IOException
.public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.Writer
java.io.IOException
- This method does a lot compared to many close methods. It must
compare the temporary file, and then try to write the original
file. There are many possible reasons for an exception.protected void finalize() throws java.lang.Throwable
finalize
in class java.lang.Object
java.lang.Throwable