写单元测试时,使用TemporaryFolder管理临时文件夹,它位于系统的临时文件目录里,Unit test执行完毕,Junit自动将其清除。
使用注解@Rule标注,告知Junit框架来管理。
@Rule public TemporaryFolder root = new TemporaryFolder(); @Test public void testFolder() throws IOException { // New folder with random name. File randomFolder = root.newFolder(); // New folder with given name. File imageFolder = root.newFolder("image"); // New folder recursively. Create a path under temporary folder: session/cache/icon. File iconFolder = root.newFolder("session", "cache", "icon"); // New file with random name. File randomFile = root.newFile(); // New file with given name. File noteFile = root.newFile("note"); }