LocalFileSystem extends FileSystem { @Override public boolean delete(final Path f, final boolean recursive) throws IOException { final File file = pathToFile(f); if (file.isFile()) { return file.delete(); } else if ((!recursive) && file.isDirectory()) { File[] containedFiles = file.listFiles(); if (containedFiles == null) { throw new IOException("Directory " + file.toString() + " does not exist or an I/O error occurred"); } else if (containedFiles.length != 0) { throw new IOException("Directory " + file.toString() + " is not empty"); } } return delete(file); } LocalFileSystem(); @Override BlockLocation[] getFileBlockLocations(FileStatus file, long start, long len); @Override FileStatus getFileStatus(Path f); @Override URI getUri(); @Override Path getWorkingDirectory(); @Override Path getHomeDirectory(); @Override FSDataInputStream open(final Path f, final int bufferSize); @Override FSDataInputStream open(final Path f); @Override FileStatus[] listStatus(final Path f); @Override boolean delete(final Path f, final boolean recursive); @Override boolean mkdirs(final Path f); @Override FSDataOutputStream create(final Path filePath, final WriteMode overwrite); @Override boolean rename(final Path src, final Path dst); @Override boolean isDistributedFS(); @Override FileSystemKind getKind(); static URI getLocalFsURI(); static LocalFileSystem getSharedInstance();  }
@Test public void testDeletePathIfEmpty() throws IOException { File file = temporaryFolder.newFile(); File directory = temporaryFolder.newFolder(); File directoryFile = new File(directory, UUID.randomUUID().toString()); assertTrue(directoryFile.createNewFile()); Path filePath = new Path(file.toURI()); Path directoryPath = new Path(directory.toURI()); Path directoryFilePath = new Path(directoryFile.toURI()); FileSystem fs = FileSystem.getLocalFileSystem(); assertTrue(fs.exists(filePath)); assertTrue(fs.exists(directoryFilePath)); assertFalse(FileUtils.deletePathIfEmpty(fs, filePath)); assertTrue(fs.exists(filePath)); assertFalse(FileUtils.deletePathIfEmpty(fs, directoryPath)); assertTrue(fs.exists(directoryPath)); assertTrue(fs.delete(directoryFilePath, false)); assertTrue(FileUtils.deletePathIfEmpty(fs, directoryPath)); assertFalse(fs.exists(directoryPath)); }