本文整理了Java中java.io.File.setWritable()
方法的一些代码示例,展示了File.setWritable()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。File.setWritable()
方法的具体详情如下:
包路径:java.io.File
类名称:File
方法名:setWritable
[英]Equivalent to setWritable(writable, true).
[中]等同于setWritable(可写,true)。
代码示例来源:origin: Tencent/tinker
/**
* create directory
*
* @param directoryPath
*/
public static void createDirectory(final String directoryPath) {
File file = new File(directoryPath);
if (!file.exists()) {
file.setReadable(true, false);
file.setWritable(true, true);
file.mkdirs();
}
}
代码示例来源:origin: typ0520/fastdex
/**
* create directory
*
* @param directoryPath
*/
public static void createDirectory(final String directoryPath) {
File file = new File(directoryPath);
if (!file.exists()) {
file.setReadable(true, false);
file.setWritable(true, true);
file.mkdirs();
}
}
代码示例来源:origin: Tencent/tinker
/**
* create file,full filename,signle empty file.
*
* @param fullFilename
* @return boolean
*/
public static boolean createFile(final String fullFilename) {
boolean result = false;
File file = new File(fullFilename);
createDirectory(file.getParent());
try {
file.setReadable(true, false);
file.setWritable(true, true);
result = file.createNewFile();
} catch (Exception e) {
throw new FileUtilException(e);
}
return result;
}
代码示例来源:origin: ethereum/ethereumj
public static boolean recursiveDelete(String fileName) {
File file = new File(fileName);
if (file.exists()) {
//check if the file is a directory
if (file.isDirectory()) {
if ((file.list()).length > 0) {
for(String s:file.list()){
//call deletion of file individually
recursiveDelete(fileName + System.getProperty("file.separator") + s);
}
}
}
file.setWritable(true);
boolean result = file.delete();
return result;
} else {
return false;
}
}
代码示例来源:origin: typ0520/fastdex
/**
* create file,full filename,signle empty file.
*
* @param fullFilename
* @return boolean
*/
public static boolean createFile(final String fullFilename) {
boolean result = false;
File file = new File(fullFilename);
createDirectory(file.getParent());
try {
file.setReadable(true, false);
file.setWritable(true, true);
result = file.createNewFile();
} catch (Exception e) {
throw new FileUtilException(e);
}
return result;
}
代码示例来源:origin: redisson/redisson
File extractedLibFile = new File(targetFolder, extractedLibFileName);
extractedLibFile.setWritable(true, true) &&
extractedLibFile.setExecutable(true);
if (!success) {
return new File(targetFolder, extractedLibFileName);
代码示例来源:origin: apache/ignite
File file = new File(System.getProperty("java.io.tmpdir"), "ignite.lastport.tmp");
file.setWritable(true, false);
代码示例来源:origin: testcontainers/testcontainers-java
@BeforeClass
public static void setupContent() throws FileNotFoundException {
contentFolder.mkdir();
contentFolder.setReadable(true, false);
contentFolder.setWritable(true, false);
contentFolder.setExecutable(true, false);
File indexFile = new File(contentFolder, "index.html");
indexFile.setReadable(true, false);
indexFile.setWritable(true, false);
indexFile.setExecutable(true, false);
@Cleanup PrintStream printStream = new PrintStream(new FileOutputStream(indexFile));
printStream.println("<html><body>This worked</body></html>");
}
代码示例来源:origin: testcontainers/testcontainers-java
@SuppressWarnings({"Duplicates", "ResultOfMethodCallIgnored"})
@BeforeClass
public static void setupContent() throws FileNotFoundException {
contentFolder.mkdir();
contentFolder.setReadable(true, false);
contentFolder.setWritable(true, false);
contentFolder.setExecutable(true, false);
File indexFile = new File(contentFolder, "index.html");
indexFile.setReadable(true, false);
indexFile.setWritable(true, false);
indexFile.setExecutable(true, false);
@Cleanup PrintStream printStream = new PrintStream(new FileOutputStream(indexFile));
printStream.println("<html><body>This worked</body></html>");
}
代码示例来源:origin: apache/flink
@Test
public void testDeleteQuietly() throws Exception {
// should ignore the call
FileUtils.deleteDirectoryQuietly(null);
File doesNotExist = new File(tmp.getRoot(), "abc");
FileUtils.deleteDirectoryQuietly(doesNotExist);
File cannotDeleteParent = tmp.newFolder();
File cannotDeleteChild = new File(cannotDeleteParent, "child");
try {
assumeTrue(cannotDeleteChild.createNewFile());
assumeTrue(cannotDeleteParent.setWritable(false));
assumeTrue(cannotDeleteChild.setWritable(false));
FileUtils.deleteDirectoryQuietly(cannotDeleteParent);
}
finally {
//noinspection ResultOfMethodCallIgnored
cannotDeleteParent.setWritable(true);
//noinspection ResultOfMethodCallIgnored
cannotDeleteChild.setWritable(true);
}
}
代码示例来源:origin: apache/flink
@Test
public void testRenameFileWithNoAccess() throws IOException {
final FileSystem fs = FileSystem.getLocalFileSystem();
final File srcFile = temporaryFolder.newFile("someFile.txt");
final File destFile = new File(temporaryFolder.newFolder(), "target");
// we need to make the file non-modifiable so that the rename fails
Assume.assumeTrue(srcFile.getParentFile().setWritable(false, false));
Assume.assumeTrue(srcFile.setWritable(false, false));
try {
final Path srcFilePath = new Path(srcFile.toURI());
final Path destFilePath = new Path(destFile.toURI());
// this cannot succeed because the source folder has no permission to remove the file
assertFalse(fs.rename(srcFilePath, destFilePath));
}
finally {
// make sure we give permission back to ensure proper cleanup
//noinspection ResultOfMethodCallIgnored
srcFile.getParentFile().setWritable(true, false);
//noinspection ResultOfMethodCallIgnored
srcFile.setWritable(true, false);
}
}
代码示例来源:origin: apache/incubator-druid
@Test
public void testPushCannotCreateDirectory() throws IOException
{
exception.expect(IOException.class);
exception.expectMessage("Unable to create directory");
config.storageDirectory = new File(config.storageDirectory, "xxx");
Assert.assertTrue(config.storageDirectory.mkdir());
config.storageDirectory.setWritable(false);
localDataSegmentPusher.push(dataSegmentFiles, dataSegment, false);
}
代码示例来源:origin: apache/flink
@Test
public void testDeleteDirectory() throws Exception {
// deleting a non-existent file should not cause an error
File doesNotExist = new File(tmp.newFolder(), "abc");
FileUtils.deleteDirectory(doesNotExist);
// deleting a write protected file should throw an error
File cannotDeleteParent = tmp.newFolder();
File cannotDeleteChild = new File(cannotDeleteParent, "child");
try {
assumeTrue(cannotDeleteChild.createNewFile());
assumeTrue(cannotDeleteParent.setWritable(false));
assumeTrue(cannotDeleteChild.setWritable(false));
FileUtils.deleteDirectory(cannotDeleteParent);
fail("this should fail with an exception");
}
catch (AccessDeniedException ignored) {
// this is expected
}
finally {
//noinspection ResultOfMethodCallIgnored
cannotDeleteParent.setWritable(true);
//noinspection ResultOfMethodCallIgnored
cannotDeleteChild.setWritable(true);
}
}
代码示例来源:origin: voldemort/voldemort
@Test
public void testMkDir() {
File tempDir = new File(System.getProperty("java.io.tmpdir"), "temp"
+ System.currentTimeMillis());
// Working case
Utils.mkdirs(tempDir);
assertTrue(tempDir.exists());
// Exists & writable false
tempDir.setWritable(false);
try {
Utils.mkdirs(tempDir);
fail("Mkdir should have thrown an exception");
} catch(VoldemortException e) {}
}
代码示例来源:origin: apache/incubator-druid
@Test
public void testPushTaskLogDirCreationFails() throws Exception
{
final File tmpDir = temporaryFolder.newFolder();
final File logDir = new File(tmpDir, "druid/logs");
final File logFile = new File(tmpDir, "log");
Files.write("blah", logFile, StandardCharsets.UTF_8);
if (!tmpDir.setWritable(false)) {
throw new RuntimeException("failed to make tmp dir read-only");
}
final TaskLogs taskLogs = new FileTaskLogs(new FileTaskLogsConfig(logDir));
expectedException.expect(IOException.class);
expectedException.expectMessage("Unable to create task log dir");
taskLogs.pushTaskLog("foo", logFile);
}
代码示例来源:origin: ehcache/ehcache3
@Test
public void testFailsIfDirectoryDoesNotExistsAndIsNotCreated() throws IOException {
assumeTrue(testFolder.setWritable(false));
try {
File f = new File(testFolder, "notallowed");
try {
final DefaultLocalPersistenceService service = new DefaultLocalPersistenceService(new DefaultPersistenceConfiguration(f));
service.start(null);
fail("Expected IllegalArgumentException");
} catch(IllegalArgumentException e) {
assertThat(e.getMessage(), equalTo("Directory couldn't be created: " + f.getAbsolutePath()));
}
} finally {
testFolder.setWritable(true);
}
}
代码示例来源:origin: cSploit/android
inFile = new File(mCurrentTask.path);
total = inFile.length();
counter = new CountingInputStream(new FileInputStream(inFile));
old_percentage = -1;
f = new File(mCurrentTask.outputDir);
if (f.exists() && f.isDirectory() && (list = f.listFiles()) != null && list.length > 2)
wipe();
f = new File(mCurrentTask.outputDir, name);
if(!f.setWritable(w, true)) {
Logger.warning(String.format("cannot set writable permission of '%s'",name));
代码示例来源:origin: apache/nifi
@Test
public void testMoveOnCompleteWithParentOfTargetDirNotAccessible() throws IOException {
final File sourceFile = new File("target/1.txt");
final byte[] content = "Hello, World!".getBytes();
Files.write(sourceFile.toPath(), content, StandardOpenOption.CREATE);
final String moveTargetParent = "target/fetch-file";
final String moveTarget = moveTargetParent + "/move-target";
final TestRunner runner = TestRunners.newTestRunner(new FetchFile());
runner.setProperty(FetchFile.FILENAME, sourceFile.getAbsolutePath());
runner.setProperty(FetchFile.COMPLETION_STRATEGY, FetchFile.COMPLETION_MOVE.getValue());
runner.assertNotValid();
runner.setProperty(FetchFile.MOVE_DESTINATION_DIR, moveTarget);
runner.assertValid();
// Make the parent of move-target non-writable and non-readable
final File moveTargetParentDir = new File(moveTargetParent);
moveTargetParentDir.mkdirs();
moveTargetParentDir.setReadable(false);
moveTargetParentDir.setWritable(false);
try {
runner.enqueue(new byte[0]);
runner.run();
runner.assertAllFlowFilesTransferred(FetchFile.REL_FAILURE, 1);
runner.getFlowFilesForRelationship(FetchFile.REL_FAILURE).get(0).assertContentEquals("");
assertTrue(sourceFile.exists());
} finally {
moveTargetParentDir.setReadable(true);
moveTargetParentDir.setWritable(true);
}
}
代码示例来源:origin: apache/nifi
@Test
public void testMoveOnCompleteWithTargetExistsButNotWritable() throws IOException {
final File sourceFile = new File("target/1.txt");
final byte[] content = "Hello, World!".getBytes();
Files.write(sourceFile.toPath(), content, StandardOpenOption.CREATE);
final TestRunner runner = TestRunners.newTestRunner(new FetchFile());
runner.setProperty(FetchFile.FILENAME, sourceFile.getAbsolutePath());
runner.setProperty(FetchFile.COMPLETION_STRATEGY, FetchFile.COMPLETION_MOVE.getValue());
runner.assertNotValid();
runner.setProperty(FetchFile.MOVE_DESTINATION_DIR, "target/move-target");
runner.assertValid();
final File destDir = new File("target/move-target");
if (!destDir.exists()) {
destDir.mkdirs();
}
destDir.setWritable(false);
assertTrue(destDir.exists());
assertFalse(destDir.canWrite());
final File destFile = new File(destDir, sourceFile.getName());
runner.enqueue(new byte[0]);
runner.run();
runner.assertAllFlowFilesTransferred(FetchFile.REL_FAILURE, 1);
runner.getFlowFilesForRelationship(FetchFile.REL_FAILURE).get(0).assertContentEquals("");
assertTrue(sourceFile.exists());
assertFalse(destFile.exists());
}
代码示例来源:origin: apache/incubator-druid
final StorageLocationConfig locationConfig = new StorageLocationConfig();
final File localStorageFolder = tmpFolder.newFolder("local_storage_folder");
localStorageFolder.setWritable(true);
locationConfig.setPath(localStorageFolder);
locationConfig.setMaxSize(10L);
final StorageLocationConfig locationConfig2 = new StorageLocationConfig();
final File localStorageFolder2 = tmpFolder.newFolder("local_storage_folder2");
localStorageFolder2.setWritable(true);
locationConfig2.setPath(localStorageFolder2);
locationConfig2.setMaxSize(10L);
final File localSegmentFile = new File(
segmentSrcFolder,
"test_segment_loader/2014-10-20T00:00:00.000Z_2014-10-21T00:00:00.000Z/2015-05-27T03:38:35.683Z/0"
);
localSegmentFile.mkdirs();
final File indexZip = new File(localSegmentFile, "index.zip");
indexZip.createNewFile();
final File localSegmentFile2 = new File(
segmentSrcFolder,
"test_segment_loader/2014-11-20T00:00:00.000Z_2014-11-21T00:00:00.000Z/2015-05-27T03:38:35.683Z/0"
内容来源于网络,如有侵权,请联系作者删除!