temporary folder in junit

Temporary folder rule allows you to create a temporary folder every time you run the test. Below example demonstrates how we can use temporary folder rule. Notice that in both tests, a new temporary folder is created.
 
package rules;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.IOException;

public class TempFolderRule {

    @Rule
    public TemporaryFolder tempFolder = new TemporaryFolder();

    @Test
    public void testUsingTempFolder() throws IOException {
        System.out.println("Creating a simple file in a temp folder");
        File testFile = tempFolder.newFile("testfile.txt");
        System.out.println(testFile.getAbsolutePath());
        System.out.println("Creating a simple folder in a temp folder");
        File testfolder = tempFolder.newFolder("testfolder");
        System.out.println("Deleting temp folder..No need to delete explicitly");
        tempFolder.delete();
    }

    @Test
    public void testAnotherTempFolder() throws IOException {
        System.out.println("Creating a other file in a temp folder");
        File testFile = tempFolder.newFile("otherTestfile.txt");
        System.out.println(testFile.getAbsolutePath());
    }
}

Here is the output of above example.
 
Creating a simple file in a temp folder
C:UsersSagarAppDataLocalTempjunit1357048258081112491	estfile.txt
Creating a simple folder in a temp folder
Deleting temp folder..No need to delete explicitly
Creating a other file in a temp folder
C:UsersSagarAppDataLocalTempjunit315837697325521595ot 

Web development and Automation testing

solutions delivered!!