org.junit.rules

## Class TemporaryFolder

- java.lang.Object
- - org.junit.rules.ExternalResource
  - - org.junit.rules.TemporaryFolder

- All Implemented Interfaces:
    TestRule

      public class TemporaryFolder
      extends ExternalResource

  The TemporaryFolder Rule allows creation of files and folders that are guaranteed to be deleted when the test method finishes (whether it passes or fails):

       public static class HasTempFolder {
        @Rule
        public TemporaryFolder folder= new TemporaryFolder();

        @Test
        public void testUsingTempFolder() throws IOException {
            File createdFile= folder.newFile("myfile.txt");
            File createdFolder= folder.newFolder("subfolder");
            // ...
           }
       }

  Since:
    4.7

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `TemporaryFolder()`
    `TemporaryFolder(File parentFolder)`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected void` | `after()` Override to tear down your specific external resource.
    `protected void` | `before()` Override to set up your specific external resource.
    `void` | `create()` for testing purposes only.
    `void` | `delete()` Delete all files and folders under the temporary folder.
    `File` | `getRoot()`
    `File` | `newFile()` Returns a new fresh file with a random name under the temporary folder.
    `File` | `newFile(String fileName)` Returns a new fresh file with the given name under the temporary folder.
    `File` | `newFolder()` Returns a new fresh folder with a random name under the temporary folder.
    `File` | `newFolder(String... folderNames)` Returns a new fresh folder with the given name(s) under the temporary folder.
    `File` | `newFolder(String folder)` Returns a new fresh folder with the given name under the temporary folder.

    - ### Methods inherited from class org.junit.rules.ExternalResource

      `apply`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### TemporaryFolder

          public TemporaryFolder()

    - #### TemporaryFolder

          public TemporaryFolder(File parentFolder)

  - ### Method Detail

    - #### before

          protected void before()
                         throws Throwable

      Description copied from class: `ExternalResource`
      Override to set up your specific external resource.
      Overrides:
        `before` in class `ExternalResource`
      Throws:
        `Throwable`

    - #### after

          protected void after()

      Description copied from class: `ExternalResource`
      Override to tear down your specific external resource.
      Overrides:
        `after` in class `ExternalResource`

    - #### create

          public void create()
                      throws IOException

      for testing purposes only. Do not use.
      Throws:
        `IOException`

    - #### newFile

          public File newFile(String fileName)
                       throws IOException

      Returns a new fresh file with the given name under the temporary folder.
      Throws:
        `IOException`

    - #### newFile

          public File newFile()
                       throws IOException

      Returns a new fresh file with a random name under the temporary folder.
      Throws:
        `IOException`

    - #### newFolder

          public File newFolder(String folder)
                         throws IOException

      Returns a new fresh folder with the given name under the temporary folder.
      Throws:
        `IOException`

    - #### newFolder

          public File newFolder(String... folderNames)
                         throws IOException

      Returns a new fresh folder with the given name(s) under the temporary folder.
      Throws:
        `IOException`

    - #### newFolder

          public File newFolder()
                         throws IOException

      Returns a new fresh folder with a random name under the temporary folder.
      Throws:
        `IOException`

    - #### getRoot

          public File getRoot()

      Returns:
        the location of this temporary folder.

    - #### delete

          public void delete()

      Delete all files and folders under the temporary folder. Usually not called directly, since it is automatically applied by the `Rule`