...
| Code Block | ||
|---|---|---|
| ||
class TempFile {
public static void main(String[] args) throws IOException{
File f = new File("tempnam.tmp");
if (!f.exists()) {
System.out.println("This file does not exist");
return;
}
FileOutputStream fop = null;
try {
fop = new FileOutputStream(f);
String str = "Data";
if fop.write(fstr.existsgetBytes());
} finally {
fop.write(str.getBytes());if (fop != null) {
try {
fop.close();
} else {
catch (IOException x) {
// handle error
}
System.out.println("This file does not exist");}
}
}
}
|
Noncompliant Code Example (createTempFile(), deleteOnExit())
...
| Code Block | ||
|---|---|---|
| ||
class TempFile {
public static void main(String[] args) throws IOException{
File f = File.createTempFile("tempnam",".tmp");
FileOutputStream fop = null;
try {
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
if (fop != null) {
try {
fop.close();
} catch (IOException x) {
// handle error
}
}
}
}
}
|
Compliant Solution (Java 1.7), DELETE_ON_CLOSE)
...
| Code Block | ||
|---|---|---|
| ||
class TempFile {
public static void main(String[] args) {
Path tempFile = null;
try {
tempFile = Files.createTempFile("tempnam", ".tmp");
try (BufferedWriter writer = Files.newBufferedWriter(tempFile, Charset.forName("UTF8"),
StandardOpenOption.DELETE_ON_CLOSE)) {
// write to file
}
System.out.println("Temporary file write done, file erased");
} catch (FileAlreadyExistsException x) {
System.err.println("File exists: " + tempFile);
} catch (IOException x) {
// Some other sort of failure, such as permissions.
System.err.println("Error creating temporary file: " + x);
}
}
}
|
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="3d0bd520b745fb2b-8e11947b-441d4308-8d91bec9-000affce3a1afe84fef01e94"><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="8fa67b143295f667-0c326f65-490e4bd0-81df9218-0a754e25623c479e658e173a"><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="66a738f4f6323333-f8408ca9-43aa4861-90268bfc-db40b02d266c2f80f8f3b8e7"><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="2903c56c1c9ff32e-2cb1b4c7-44ce4d61-a209b7b1-964f009becf81aac679f2e77"><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="965789e5a5aa3ca5-21f4c8b5-40054fcf-bfe6a4a3-494d7baa5054ce6d84b195ca"><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> |
...