Utils { public static void deleteFileOrDirectory(File file) throws IOException { if (file.exists()) { if (file.isDirectory()) { Path rootPath = Paths.get(file.getAbsolutePath()); Files.walk(rootPath, FileVisitOption.FOLLOW_LINKS) .sorted(Comparator.reverseOrder()) .map(Path::toFile) .forEach(File::delete); } else { file.delete(); } } else { throw new RuntimeException("File or directory does not exist"); } } private  Utils(); static String generateParameterHash(String path, String func, List<String> args); static String generateDirectoryHash(String rootDir, String chaincodeDir, String hash); static byte[] generateTarGz(File sourceDirectory, String pathPrefix); static byte[] readFile(File input); static String generateUUID(); static Timestamp generateTimestamp(); static void deleteFileOrDirectory(File file); static byte[] hash(byte[] input, Digest digest); static String combinePaths(String first, String... other); static byte[] readFileFromClasspath(String fileName); static Properties parseGrpcUrl(String url); static Exception checkGrpcUrl(String url); static boolean isNullOrEmpty(String url); static String logString(final String string); static byte[] generateNonce(); static String toHexString(ByteString byteString); static String toHexString(byte[] bytes);  }
@Test public void testDeleteFileOrDirectoryFile() throws Exception { File tempFile = tempFolder.newFile("temp.txt"); Assert.assertTrue(tempFile.exists()); Utils.deleteFileOrDirectory(tempFile); Assert.assertFalse(tempFile.exists()); }
@Test public void testDeleteFileOrDirectoryDirectory() throws Exception { File tempDir = createTempDirWithFiles(); Assert.assertTrue(tempDir.exists()); Utils.deleteFileOrDirectory(tempDir); Assert.assertFalse(tempDir.exists()); }