static FileWriter createOrGetFile(File folder, String name) throws IOException { File file = new File(folder, name + ".json"); if (!file.exists()) { Files.createFile(file.toPath()); } FileWriter fr = new FileWriter(file); return fr; }
/** * Creates an empty file and its intermediate directories if necessary. * * @param filePath pathname string of the file to create */ public static void createFile(String filePath) throws IOException { Path storagePath = Paths.get(filePath); Files.createDirectories(storagePath.getParent()); Files.createFile(storagePath); }
public Void perform() { try { Path target = vertx.resolveFile(p).toPath(); if (attrs != null) { Files.createFile(target, attrs); } else { Files.createFile(target); } } catch (IOException e) { throw new FileSystemException(e); } return null; } };
/** * Like the unix command of the same name, creates an empty file or updates the last modified * timestamp of the existing file at the given path to the current system time. */ @SuppressWarnings("GoodTime") // reading system time without TimeSource public static void touch(Path path) throws IOException { checkNotNull(path); try { Files.setLastModifiedTime(path, FileTime.fromMillis(System.currentTimeMillis())); } catch (NoSuchFileException e) { try { Files.createFile(path); } catch (FileAlreadyExistsException ignore) { // The file didn't exist when we called setLastModifiedTime, but it did when we called // createFile, so something else created the file in between. The end result is // what we wanted: a new file that probably has its last modified time set to approximately // now. Or it could have an arbitrary last modified time set by the creator, but that's no // different than if another process set its last modified time to something else after we // created it here. } } }
static void createGzippedFile(String filePath) throws IOException { Resource location = new ClassPathResource("test/", EncodedResourceResolverTests.class); Resource resource = new FileSystemResource(location.createRelative(filePath).getFile()); Path gzFilePath = Paths.get(resource.getFile().getAbsolutePath() + ".gz"); Files.deleteIfExists(gzFilePath); File gzFile = Files.createFile(gzFilePath).toFile(); GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzFile)); FileCopyUtils.copy(resource.getInputStream(), out); gzFile.deleteOnExit(); }
static void createGzippedFile(String filePath) throws IOException { Resource location = new ClassPathResource("test/", EncodedResourceResolverTests.class); Resource resource = new FileSystemResource(location.createRelative(filePath).getFile()); Path gzFilePath = Paths.get(resource.getFile().getAbsolutePath() + ".gz"); Files.deleteIfExists(gzFilePath); File gzFile = Files.createFile(gzFilePath).toFile(); GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzFile)); FileCopyUtils.copy(resource.getInputStream(), out); gzFile.deleteOnExit(); }
private static void putStoreInDirectory( Path databaseDirectory ) throws IOException { Files.createDirectories( databaseDirectory ); Path storeFile = DatabaseLayout.of( databaseDirectory.toFile() ).metadataStore().toPath(); Files.createFile( storeFile ); }
private static void putStoreInDirectory( Path databaseDirectory ) throws IOException { Files.createDirectories( databaseDirectory ); Path storeFile = DatabaseLayout.of( databaseDirectory.toFile() ).metadataStore().toPath(); Files.createFile( storeFile ); }
@Test void shouldNotCalculateTheArchiveNameIfPassedAnExistingFile() throws Exception { Files.createFile( archive ); execute( "foo.db" ); verify( dumper ).dump( any(), any(), eq( archive ), any() ); }
@BeforeEach void setUp() throws Exception { homeDir = testDirectory.directory( "home-dir" ).toPath(); configDir = testDirectory.directory( "config-dir" ).toPath(); // Touch config configFile = configDir.resolve( "neo4j.conf" ); Files.createFile( configFile ); // To make sure files are resolved from the working directory originalUserDir = System.setProperty( "user.dir", testDirectory.absolutePath().getAbsolutePath() ); }
@AndroidIncompatible // Path (for symlink creation) public void testScanDirectory_symlinkToRootCycle() throws IOException { ClassLoader loader = ClassPathTest.class.getClassLoader(); // directory with a cycle, // /root // /child // /[grandchild -> root] java.nio.file.Path root = createTempDirectory("ClassPathTest"); try { createFile(root.resolve("some.txt")); java.nio.file.Path child = createDirectory(root.resolve("child")); createSymbolicLink(child.resolve("grandchild"), root); ClassPath.DefaultScanner scanner = new ClassPath.DefaultScanner(); scanner.scan(root.toFile(), loader); assertEquals(ImmutableSet.of(new ResourceInfo("some.txt", loader)), scanner.getResources()); } finally { deleteRecursivelyOrLog(root); } }
@Test public void testUploadJar() throws Exception { final Path uploadedFile = Files.createFile(jarDir.resolve("Kafka010Example.jar")); final HandlerRequest<EmptyRequestBody, EmptyMessageParameters> request = createRequest(uploadedFile); final JarUploadResponseBody jarUploadResponseBody = jarUploadHandler.handleRequest(request, mockDispatcherGateway).get(); assertThat(jarUploadResponseBody.getStatus(), equalTo(JarUploadResponseBody.UploadStatus.success)); final String returnedFileNameWithUUID = jarUploadResponseBody.getFilename(); assertThat(returnedFileNameWithUUID, containsString("_")); final String returnedFileName = returnedFileNameWithUUID.substring(returnedFileNameWithUUID.lastIndexOf("_") + 1); assertThat(returnedFileName, equalTo(uploadedFile.getFileName().toString())); }
@AndroidIncompatible // Path (for symlink creation) public void testScanDirectory_symlinkCycle() throws IOException { ClassLoader loader = ClassPathTest.class.getClassLoader(); // directory with a cycle, // /root // /left // /[sibling -> right] // /right // /[sibling -> left] java.nio.file.Path root = createTempDirectory("ClassPathTest"); try { java.nio.file.Path left = createDirectory(root.resolve("left")); createFile(left.resolve("some.txt")); java.nio.file.Path right = createDirectory(root.resolve("right")); createFile(right.resolve("another.txt")); createSymbolicLink(left.resolve("sibling"), right); createSymbolicLink(right.resolve("sibling"), left); ClassPath.DefaultScanner scanner = new ClassPath.DefaultScanner(); scanner.scan(root.toFile(), loader); assertEquals( ImmutableSet.of( new ResourceInfo("left/some.txt", loader), new ResourceInfo("left/sibling/another.txt", loader), new ResourceInfo("right/another.txt", loader), new ResourceInfo("right/sibling/some.txt", loader)), scanner.getResources()); } finally { deleteRecursivelyOrLog(root); } }
@Test public void deleteDirectory_throws_IOE_if_file_is_symbolicLink() throws IOException { assumeTrue(SystemUtils.IS_OS_UNIX); Path folder = temporaryFolder.newFolder().toPath(); Path file1 = Files.createFile(folder.resolve("file1.txt")); Path symLink = Files.createSymbolicLink(folder.resolve("link1"), file1); assertThat(file1).isRegularFile(); assertThat(symLink).isSymbolicLink(); expectedException.expect(IOException.class); expectedException.expectMessage("Directory '" + symLink.toFile().getAbsolutePath() + "' is a symbolic link"); FileUtils2.deleteDirectory(symLink.toFile()); }
@Test public void deleteQuietly_deletes_symbolicLink() throws IOException { assumeTrue(SystemUtils.IS_OS_UNIX); Path folder = temporaryFolder.newFolder().toPath(); Path file1 = Files.createFile(folder.resolve("file1.txt")); Path symLink = Files.createSymbolicLink(folder.resolve("link1"), file1); assertThat(file1).isRegularFile(); assertThat(symLink).isSymbolicLink(); FileUtils.deleteQuietly(symLink.toFile()); assertThat(symLink).doesNotExist(); assertThat(file1).isRegularFile(); }
@Test public void deleteQuietly_deletes_symbolicLink() throws IOException { assumeTrue(SystemUtils.IS_OS_UNIX); Path folder = temporaryFolder.newFolder().toPath(); Path file1 = Files.createFile(folder.resolve("file1.txt")); Path symLink = Files.createSymbolicLink(folder.resolve("link1"), file1); assertThat(file1).isRegularFile(); assertThat(symLink).isSymbolicLink(); FileUtils2.deleteQuietly(symLink.toFile()); assertThat(symLink).doesNotExist(); assertThat(file1).isRegularFile(); }
@Test public void testRejectNonJarFiles() throws Exception { final Path uploadedFile = Files.createFile(jarDir.resolve("katrin.png")); final HandlerRequest<EmptyRequestBody, EmptyMessageParameters> request = createRequest(uploadedFile); try { jarUploadHandler.handleRequest(request, mockDispatcherGateway).get(); fail("Expected exception not thrown."); } catch (final ExecutionException e) { final Throwable throwable = ExceptionUtils.stripCompletionException(e.getCause()); assertThat(throwable, instanceOf(RestHandlerException.class)); final RestHandlerException restHandlerException = (RestHandlerException) throwable; assertThat(restHandlerException.getHttpResponseStatus(), equalTo(HttpResponseStatus.BAD_REQUEST)); } }
private void createPersistedDirectoryLevel(int levels, AlluxioURI ufsTop) throws Exception { if (levels <= 0) { return; } // Create files and nested directories in UFS. for (int i = 0; i < DIR_WIDTH; ++i) { Files.createFile(Paths.get(ufsTop.join(FILE_PREFIX + i).getPath())); Files.createDirectories(Paths.get(ufsTop.join(DIR_PREFIX + i).getPath())); createPersistedDirectoryLevel(levels - 1, ufsTop.join(DIR_PREFIX + i)); } }
@Before public void setUp() throws Exception { jarDir = temporaryFolder.newFolder().toPath(); restfulGateway = TestingRestfulGateway.newBuilder().build(); jarDeleteHandler = new JarDeleteHandler( () -> CompletableFuture.completedFuture(restfulGateway), Time.seconds(10), Collections.emptyMap(), new JarDeleteHeaders(), jarDir, Executors.directExecutor() ); Files.createFile(jarDir.resolve(TEST_JAR_NAME)); }
@Test public void testDeleteAfterFailedCreate() throws Exception{ //Check that a blob can be deleted when a temporary file exists in the blob directory LocalFsBlobStore store = initLocalFs(); String key = "test"; SettableBlobMeta metadata = new SettableBlobMeta(BlobStoreAclHandler .WORLD_EVERYTHING); try (AtomicOutputStream out = store.createBlob(key, metadata, null)) { out.write(1); File blobDir = store.getKeyDataDir(key); Files.createFile(blobDir.toPath().resolve("tempFile.tmp")); } store.deleteBlob("test",null); }