...
Code Block | ||
---|---|---|
| ||
class TempFile {
public static void main(String[] args) throws IOException{
File f = new File("tempnam.tmp");
FileOutputStream fop = new FileOutputStream(f);
String str = "Data";
if (f.exists()) {
fop.write(str.getBytes());
fop.close();
} else {
System.out.println("This file does not exist");
}
}
}
h2. |
Noncompliant
...
Code
...
Example
...
(
...
createTempFile()
...
,
...
deleteOnExit()
...
)
...
This
...
noncompliant
...
code
...
example
...
invokes
...
the
...
File.createTempFile()
...
method
...
which
...
generates
...
a
...
unique
...
temporary
...
filename
...
based
...
on
...
two
...
parameters,
...
a
...
prefix
...
and
...
an
...
extension.
...
This
...
is
...
the
...
only
...
method
...
currently
...
designed
...
and
...
provided
...
for
...
producing
...
unique
...
file
...
names;
...
although
...
the
...
names
...
produced
...
can
...
be
...
easily
...
predicted.
...
If
...
the
...
filename
...
must
...
be
...
unpredictable,
...
this
...
problem
...
can
...
be
...
solved
...
by
...
using
...
a
...
good
...
random
...
number
...
generator
...
to
...
produce
...
the
...
prefix.
...
Wiki Markup |
---|
This example also attempts to use the {{deleteOnExit()}} method to ensure that the temporary file is deleted when the JVM terminates. However, according to the Java API \[[API 2006|AA. Bibliography#API 06]\] Class {{File}}, method {{deleteOnExit()}} documentation: |
...
Deletion
...
will
...
be
...
attempted
...
only
...
for
...
normal
...
termination
...
of
...
the
...
virtual
...
machine,
...
as
...
defined
...
by
...
the
...
Java
...
Language
...
Specification.
...
Once
...
deletion
...
has
...
been
...
requested,
...
it
...
is
...
not
...
possible
...
to
...
cancel
...
the
...
request.
...
This
...
method
...
should
...
consequently
...
be
...
used
...
with
...
care.
...
Note:
...
this
...
method
...
should
...
not
...
be
...
used
...
for
...
file-locking,
...
as
...
the
...
resulting
...
protocol
...
cannot
...
be
...
made
...
to
...
work
...
reliably.
...
Wiki Markup |
---|
Consequently, the file is not deleted if the JVM terminates unexpectedly. A longstanding bug on Windows based systems reported as [Bug ID: 4171239|http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4171239] \[[SDN 2008|AA. Bibliography#SDN 08]\] causes JVMs to fail to delete a file when {{deleteOnExit()}} is invoked before the associated stream or {{RandomAccessFile}} is closed. |
Code Block | ||||
---|---|---|---|---|
| =
| |||
}
class TempFile {
public static void main(String[] args) throws IOException{
File f = File.createTempFile("tempnam",".tmp");
FileOutputStream fop = new FileOutputStream(f);
String str = "Data";
try {
fop.write(str.getBytes());
fop.flush();
} finally {
// Stream/file still open; file will
// not be deleted on Windows systems
f.deleteOnExit(); // Delete the file when the JVM terminates
}
}
}
|
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="297f9c4239576220-3421d382-4eb64414-bb5ab6db-53dafbc8439942f964210a3a"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] | Class File, methods | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="54b06b8c196af446-38d1ce05-4f7a48d9-949e925b-9d5cadff296766e67aedc752"><ac:plain-text-body><![CDATA[ | [[CVE 2008 | AA. Bibliography#CVE 08]] | [CVE-2008-5354 | http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-5354] | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="97ce5c1563f3a6c6-64d3e659-40f54486-a99eb154-3792edfefd7abc2d0a4f00ad"><ac:plain-text-body><![CDATA[ | [[Darwin 2004 | AA. Bibliography#Darwin 04]] | 11.5 Creating a Transient File | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="616514ac69594bb9-a95a5fa8-49ec4ed5-8729b341-211112e4844870f591cbf38b"><ac:plain-text-body><![CDATA[ | [[J2SE 2011 | AA. Bibliography#J2SE 11]] |
| ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="74afaf477233d774-c36a889f-4baf46c7-a2b7a433-534fcffe0175bb23f219be13"><ac:plain-text-body><![CDATA[ | [[SDN 2008 | AA. Bibliography#SDN 08]] | Bug IDs: 4171239, 4405521, 4635827, 4631820 | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="4c1a2c735ade317a-37993b52-40c64bbe-9137be85-c7cea19d6f9080d93578005f"><ac:plain-text-body><![CDATA[ | [[Secunia 2008 | AA. Bibliography#Secunia 08]] | [Secunia Advisory 20132 | http://secunia.com/advisories/20132/] | ]]></ac:plain-text-body></ac:structured-macro> |
...