本文整理汇总了Java中java.nio.file.Files.createSymbolicLink方法的典型用法代码示例。如果您正苦于以下问题:Java Files.createSymbolicLink方法的具体用法?Java Files.createSymbolicLink怎么用?Java Files.createSymbolicLink使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.file.Files
的用法示例。
在下文中一共展示了Files.createSymbolicLink方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test
import java.nio.file.Files; //导入方法依赖的package包/类
void test(Path base, String name) throws IOException {
Path file = base.resolve(name);
Path javaFile = base.resolve("HelloWorld.java");
tb.writeFile(file,
"public class HelloWorld {\n"
+ " public static void main(String... args) {\n"
+ " System.err.println(\"Hello World!\");\n"
+ " }\n"
+ "}");
try {
Files.createSymbolicLink(javaFile, file.getFileName());
} catch (FileSystemException fse) {
System.err.println("warning: test passes vacuously, sym-link could not be created");
System.err.println(fse.getMessage());
return;
}
Path classes = Files.createDirectories(base.resolve("classes"));
new JavacTask(tb)
.outdir(classes)
.files(javaFile)
.run()
.writeAll();
}
示例2: testIndirectSymbolicLink
import java.nio.file.Files; //导入方法依赖的package包/类
/**
* Test directory tree with a indirect cyclic link.
* <pre>
* a
* b
* c ( -> a)
* d
* e ( -> c)
* </pre>
*
* @throws java.io.IOException
*/
public void testIndirectSymbolicLink() throws IOException {
File a = new File(getWorkDir(), "a");
File b = new File(a, "b");
File c = new File(b, "c");
File d = new File(a, "d");
File e = new File(d, "e");
assertTrue(b.mkdirs());
assertTrue(d.mkdirs());
try {
Files.createSymbolicLink(c.toPath(), a.toPath());
Files.createSymbolicLink(e.toPath(), c.toPath());
FileObject fo = FileUtil.toFileObject(getWorkDir());
assertEquals(3, countRecursiveChildren(fo));
} catch (IOException ex) {
printSkipped();
}
}
示例3: testEqual_links
import java.nio.file.Files; //导入方法依赖的package包/类
public void testEqual_links() throws IOException {
try (FileSystem fs = Jimfs.newFileSystem(Configuration.unix())) {
Path fooPath = fs.getPath("foo");
MoreFiles.asCharSink(fooPath, UTF_8).write("foo");
Path fooSymlink = fs.getPath("symlink");
Files.createSymbolicLink(fooSymlink, fooPath);
Path fooHardlink = fs.getPath("hardlink");
Files.createLink(fooHardlink, fooPath);
assertThat(MoreFiles.equal(fooPath, fooSymlink)).isTrue();
assertThat(MoreFiles.equal(fooPath, fooHardlink)).isTrue();
assertThat(MoreFiles.equal(fooSymlink, fooHardlink)).isTrue();
}
}
示例4: main
import java.nio.file.Files; //导入方法依赖的package包/类
public static void main(String... args) throws Exception {
ToolBox tb = new ToolBox();
tb.writeFile("tmp/B.java", BSrc);
// Try to set up a symbolic link for the test.
try {
Files.createSymbolicLink(Paths.get("a"), Paths.get("tmp"));
System.err.println("Created symbolic link");
} catch (UnsupportedOperationException | IOException e) {
System.err.println("Problem creating symbolic link: " + e);
System.err.println("Test cannot continue; test passed by default");
return;
}
// If symbolic link was successfully created,
// try a compilation that will use it.
new JavacTask(tb)
.sourcepath(".")
.outdir(".")
.sources(TSrc)
.run()
.writeAll();
}
示例5: mkSymlink
import java.nio.file.Files; //导入方法依赖的package包/类
public void mkSymlink(String symlinkName, String symlinkTarget) throws StorageAdapterException {
try {
File link = new File(symlinkName);
File target = new File(symlinkTarget);
mkParentDirs(link); // make sure directory containing the link exists
Files.createSymbolicLink(link.toPath(), target.toPath());
} catch (Exception e) {
String errMsg = e.getMessage();
if (e instanceof FileAlreadyExistsException) {
errMsg = "the file already exists";
}
errMsg = String.format("Error creating symlink %s to target %s: %s", symlinkName,
symlinkTarget, errMsg);
LOG.log(Level.WARNING, errMsg, e);
throw new StorageAdapterException(errMsg, e);
}
}
示例6: testByteSource_size_ofSymlinkToRegularFile_nofollowLinks
import java.nio.file.Files; //导入方法依赖的package包/类
public void testByteSource_size_ofSymlinkToRegularFile_nofollowLinks() throws IOException {
try (FileSystem fs = Jimfs.newFileSystem(Configuration.unix())) {
Path file = fs.getPath("file");
Files.write(file, new byte[10]);
Path link = fs.getPath("link");
Files.createSymbolicLink(link, file);
ByteSource source = MoreFiles.asByteSource(link, NOFOLLOW_LINKS);
assertThat(source.sizeIfKnown()).isAbsent();
try {
source.size();
fail();
} catch (IOException expected) {
}
}
}
示例7: testAddMissingSymlink
import java.nio.file.Files; //导入方法依赖的package包/类
public void testAddMissingSymlink () throws Exception {
if (isWindows()) {
return;
}
String path = "folder/file";
File f = new File(workDir, path);
// try with commandline client
File link = new File(workDir, "link");
Files.createSymbolicLink(Paths.get(link.getAbsolutePath()), Paths.get(path));
getClient(workDir).add(new File[] { link }, NULL_PROGRESS_MONITOR);
DirCacheEntry e = repository.readDirCache().getEntry(link.getName());
assertEquals(FileMode.SYMLINK, e.getFileMode());
assertEquals(0, e.getLength());
ObjectReader reader = repository.getObjectDatabase().newReader();
assertTrue(reader.has(e.getObjectId()));
byte[] bytes = reader.open(e.getObjectId()).getBytes();
assertEquals(path, RawParseUtils.decode(bytes));
reader.release();
}
示例8: testPredicates
import java.nio.file.Files; //导入方法依赖的package包/类
public void testPredicates() throws IOException {
Path file = createTempFile();
Path dir = tempDir.resolve("dir");
Files.createDirectory(dir);
assertTrue(MoreFiles.isDirectory().apply(dir));
assertFalse(MoreFiles.isRegularFile().apply(dir));
assertFalse(MoreFiles.isDirectory().apply(file));
assertTrue(MoreFiles.isRegularFile().apply(file));
Path symlinkToDir = tempDir.resolve("symlinkToDir");
Path symlinkToFile = tempDir.resolve("symlinkToFile");
Files.createSymbolicLink(symlinkToDir, dir);
Files.createSymbolicLink(symlinkToFile, file);
assertTrue(MoreFiles.isDirectory().apply(symlinkToDir));
assertFalse(MoreFiles.isRegularFile().apply(symlinkToDir));
assertFalse(MoreFiles.isDirectory().apply(symlinkToFile));
assertTrue(MoreFiles.isRegularFile().apply(symlinkToFile));
assertFalse(MoreFiles.isDirectory(NOFOLLOW_LINKS).apply(symlinkToDir));
assertFalse(MoreFiles.isRegularFile(NOFOLLOW_LINKS).apply(symlinkToFile));
}
示例9: testIsRecursiveSymbolicLinkIndirect
import java.nio.file.Files; //导入方法依赖的package包/类
/**
* Test isRecursiveSymbolicLink method. Use this folder tree:
* <pre>
* - workdir
* - a
* - b
* - c (symlink to d)
* - d (symlink to a)
* </pre>
*
* @throws java.io.IOException
*/
public void testIsRecursiveSymbolicLinkIndirect() throws IOException {
if (!checkSymlinksSupported()) {
return;
}
File dir = getWorkDir();
File a = new File(dir, "a");
File b = new File(a, "b");
File c = new File(b, "c");
File d = new File(dir, "d");
b.mkdirs();
Files.createSymbolicLink(d.toPath(), a.toPath());
Files.createSymbolicLink(c.toPath(), d.toPath());
FileObject dirFO = FileUtil.toFileObject(dir);
FileObject cFO = dirFO.getFileObject("a/b/c");
assertTrue(FileUtil.isRecursiveSymbolicLink(cFO));
}
示例10: testReadSymbolicLinkRelative
import java.nio.file.Files; //导入方法依赖的package包/类
public void testReadSymbolicLinkRelative() throws IOException {
if (!checkSymlinksSupported()) {
return;
}
File dir = getWorkDir();
File folder = new File(dir, "folder");
File link = new File(dir, "link");
folder.mkdir();
Path lp = Files.createSymbolicLink(link.toPath(), Paths.get("folder"));
assertNotNull(lp);
FileObject linkFO = FileUtil.toFileObject(link);
assertNotNull(linkFO);
FileObject dataFO = linkFO.readSymbolicLink();
assertNotSame(linkFO, dataFO);
assertNotNull(dataFO);
assertEquals("folder", dataFO.getNameExt());
}
示例11: testAllowedSymbolicLink
import java.nio.file.Files; //导入方法依赖的package包/类
/**
* Test allowed (non-cyclic) symlink.
* <pre>
* a
* b
* c ( -> d)
* d
* e
* f
* </pre>
*
* @throws java.io.IOException
*/
public void testAllowedSymbolicLink() throws IOException {
File a = new File(getWorkDir(), "a");
File b = new File(a, "b");
File c = new File(b, "c");
File d = new File(a, "d");
File e = new File(d, "e");
File f = new File(e, "f");
assertTrue(b.mkdirs());
assertTrue(f.mkdirs());
try {
Files.createSymbolicLink(c.toPath(), d.toPath());
assertEquals(3, countRecursiveChildren(FileUtil.toFileObject(b)));
} catch (IOException ex) {
printSkipped();
}
}
示例12: testByteSource_size_ofSymlinkToRegularFile
import java.nio.file.Files; //导入方法依赖的package包/类
public void testByteSource_size_ofSymlinkToRegularFile() throws IOException {
try (FileSystem fs = Jimfs.newFileSystem(Configuration.unix())) {
Path file = fs.getPath("file");
Files.write(file, new byte[10]);
Path link = fs.getPath("link");
Files.createSymbolicLink(link, file);
ByteSource source = MoreFiles.asByteSource(link);
assertEquals(10L, (long) source.sizeIfKnown().get());
assertEquals(10L, source.size());
}
}
示例13: symlink
import java.nio.file.Files; //导入方法依赖的package包/类
@Override
public void symlink(final Local file, final String target) throws AccessDeniedException {
try {
Files.createSymbolicLink(Paths.get(file.getAbsolute()), Paths.get(target));
}
catch(IOException | UnsupportedOperationException e) {
throw new AccessDeniedException(String.format("%s %s",
LocaleFactory.localizedString("Cannot create file", "Error"), file.getAbsolute()), e);
}
}
示例14: createSymbolicLink
import java.nio.file.Files; //导入方法依赖的package包/类
@Override
public void createSymbolicLink(Path link, Path target, FileAttribute<?>... attrs)
throws IOException
{
triggerEx(target, "createSymbolicLink");
Files.createSymbolicLink(unwrap(link), unwrap(target), attrs);
}
示例15: testPidFile
import java.nio.file.Files; //导入方法依赖的package包/类
public void testPidFile() throws IOException {
Path dir = createTempDir();
Path parent = dir.resolve("foo");
if (randomBoolean()) {
Files.createDirectories(parent);
if (randomBoolean()) {
try {
Path link = dir.resolve("link_to_real_path");
Files.createSymbolicLink(link, parent.getFileName());
parent = link;
} catch (UnsupportedOperationException | IOException | SecurityException ex) {
// fine - no links on this system
}
}
}
Path pidFile = parent.resolve("foo.pid");
long pid = randomLong();
if (randomBoolean() && Files.exists(parent)) {
try (BufferedWriter stream = Files.newBufferedWriter(pidFile, StandardCharsets.UTF_8, StandardOpenOption.CREATE_NEW)) {
stream.write("foo");
}
}
final PidFile inst = PidFile.create(pidFile, false, pid);
assertEquals(pidFile, inst.getPath());
assertEquals(pid, inst.getPid());
assertFalse(inst.isDeleteOnExit());
assertTrue(Files.exists(pidFile));
assertEquals(pid, Long.parseLong(new String(Files.readAllBytes(pidFile), StandardCharsets.UTF_8)));
}