CHAPTER 8 ■ WRITING AND READING FILES
String tempDirectoryName = "C:" + File.separator + "test";
File tempDirectory = new File(tempDirectoryName);
for (int i = 0; i < 10; i++) {
try {
File thisFile =
File.createTempFile("tmp", null, tempDirectory);
} catch (Exception e) {
System.out.println("Couldn't create temp file " + i);
}
}
System.out.println("Done creating temp files");
}
}
The createTempFile method guarantees that each temp file created during the life of the program
will be different. It doesn't guarantee that the file names will mean anything to a human. Listing 8-6
shows the names of the files that program created in our test directory.
Listing 8-6. Temp file names
tmp1672349819571008723.tmp
tmp3234113842230809615.tmp
tmp5343720775549909618.tmp
tmp6194798942830449846.tmp
tmp7016714774703888253.tmp
tmp720922773409895465.tmp
tmp7634665877605496722.tmp
tmp7730975525106591320.tmp
tmp799007629249445444.tmp
tmp8048303951856646489.tmp
Figure 8-5 shows the temp files after they've been created in my test directory.
Figure 8-5. Temp files in the test directory