下面列出了怎么用java.nio.file.attribute.FileAttribute的API类实例代码及写法,或者点击链接到github查看源代码。
public static void enablePrestoJavaDebugger(DockerContainer container, int debugPort)
{
try {
FileAttribute<Set<PosixFilePermission>> rwx = PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwxrwxrwx"));
Path script = Files.createTempFile("enable-java-debugger", ".sh", rwx);
Files.writeString(
script,
format(
"#!/bin/bash\n" +
"printf '%%s\\n' '%s' >> '%s'\n",
"-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=0.0.0.0:" + debugPort,
CONTAINER_PRESTO_JVM_CONFIG),
UTF_8);
container.withCopyFileToContainer(MountableFile.forHostPath(script), "/docker/presto-init.d/enable-java-debugger.sh");
// expose debug port unconditionally when debug is enabled
exposePort(container, debugPort);
}
catch (IOException e) {
throw new UncheckedIOException(e);
}
}
private static void writeKeyToFile(final String path, final String fileName, final String key)
throws IOException {
Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxr-----");
FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms);
Path p = Paths.get(path).resolve(fileName);
Path keyFile;
if (!java.nio.file.Files.exists(p)) {
keyFile = java.nio.file.Files.createFile(p, attr);
} else {
keyFile = p;
}
FileOutputStream fos = new FileOutputStream(keyFile.toString());
fos.write(key.getBytes());
fos.close();
}
private File flushPropertiesToTempFile(Map<String, Object> configProps) throws IOException {
FileAttribute<Set<PosixFilePermission>> attributes
= PosixFilePermissions.asFileAttribute(new HashSet<>(
Arrays.asList(PosixFilePermission.OWNER_WRITE,
PosixFilePermission.OWNER_READ)));
File configFile = Files.createTempFile("ksqlclient", "properties", attributes).toFile();
configFile.deleteOnExit();
try (FileOutputStream outputStream = new FileOutputStream(configFile)) {
Properties clientProps = new Properties();
for (Map.Entry<String, Object> property
: configProps.entrySet()) {
clientProps.put(property.getKey(), property.getValue());
}
clientProps.store(outputStream, "Configuration properties of KSQL AdminClient");
}
return configFile;
}
@Override
public void createSymbolicLink(Path link, Path target, FileAttribute<?>... attrs)
throws IOException
{
triggerEx(target, "createSymbolicLink");
Files.createSymbolicLink(unwrap(link), unwrap(target), attrs);
}
/**
* Create a file, if it does not already exist.
*
* @param path The path to the file to create.
* @param attrs An optional list of file attributes to set atomically when creating the file.
* @return {@code true} if the file was created.
* @throws IOException If an I/O error occurs or the parent directory does not exist.
*/
public static boolean createFileIfMissing(Path path, FileAttribute<?>... attrs) throws IOException {
requireNonNull(path);
try {
java.nio.file.Files.createFile(path, attrs);
} catch (FileAlreadyExistsException e) {
return false;
}
return true;
}
private void init(RegistryInfo registryInfo, Map<String, String> config) {
this.registryName = registryInfo.getName();
this.path = config.get("path");
if (path == null) {
throw new RuntimeException("path variable not set");
}
if (!new File(path).isDirectory()) {
try {
Files.createDirectories(new File(path).toPath(), new FileAttribute[] {});
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
/**
* Creates a temporary file in the given directory, or in in the
* temporary directory if dir is {@code null}.
*/
static Path createTempFile(Path dir,
String prefix,
String suffix,
FileAttribute<?>[] attrs)
throws IOException
{
return create(dir, prefix, suffix, false, attrs);
}
public TmpDirectoryCreator(final Supplier<Path> dir, final String prefix, final FileAttribute<?>... attrs)
{
super();
LOG.debug("Creating {} with dir={} prefix={} attrs={}", this.getClass().getSimpleName(), dir, prefix, attrs);
this.dir = dir;
this.prefix = prefix;
this.attrs = attrs;
}
/**
* Creates a temporary directory in the given directory, or in in the
* temporary directory if dir is {@code null}.
*/
static Path createTempDirectory(Path dir,
String prefix,
FileAttribute<?>[] attrs)
throws IOException
{
return create(dir, prefix, null, true, attrs);
}
/**
* Used by createDirectories to attempt to create a directory. A no-op
* if the directory already exists.
*/
private static void createAndCheckIsDirectory(Path dir,
FileAttribute<?>... attrs)
throws IOException
{
try {
createDirectory(dir, attrs);
} catch (FileAlreadyExistsException x) {
if (!isDirectory(dir, LinkOption.NOFOLLOW_LINKS))
throw x;
}
}
/**
* Creates a temporary file in the given directory, or in in the
* temporary directory if dir is {@code null}.
*/
static Path createTempFile(Path dir,
String prefix,
String suffix,
FileAttribute<?>[] attrs)
throws IOException
{
return create(dir, prefix, suffix, false, attrs);
}
/**
* Creates a temporary directory in the given directory, or in in the
* temporary directory if dir is {@code null}.
*/
static Path createTempDirectory(Path dir,
String prefix,
FileAttribute<?>[] attrs)
throws IOException
{
return create(dir, prefix, null, true, attrs);
}
/**
* Used by createDirectories to attempt to create a directory. A no-op
* if the directory already exists.
*/
private static void createAndCheckIsDirectory(Path dir,
FileAttribute<?>... attrs)
throws IOException
{
try {
createDirectory(dir, attrs);
} catch (FileAlreadyExistsException x) {
if (!isDirectory(dir, LinkOption.NOFOLLOW_LINKS))
throw x;
}
}
@Override
public void createSymbolicLink(Path link, Path target, FileAttribute<?>... attrs)
throws IOException
{
triggerEx(target, "createSymbolicLink");
Files.createSymbolicLink(unwrap(link), unwrap(target), attrs);
}
@Override
public void createDirectory(Path dir, FileAttribute<?>... attrs)
throws IOException
{
triggerEx(dir, "createDirectory");
Files.createDirectory(unwrap(dir), attrs);
}
@Override
public SeekableByteChannel newByteChannel(Path file,
Set<? extends OpenOption> options,
FileAttribute<?>... attrs)
throws IOException
{
triggerEx(file, "newByteChannel");
return Files.newByteChannel(unwrap(file), options, attrs);
}
@Override
public SeekableByteChannel newByteChannel(Path path,
Set<? extends OpenOption> options,
FileAttribute<?>... attrs)
throws IOException
{
if (options.contains(CustomOption.IGNORE)) {
ignoreCount++;
options.remove(CustomOption.IGNORE);
}
return super.newByteChannel(path, options, attrs);
}
/**
* Creates a temporary file in the given directory, or in in the
* temporary directory if dir is {@code null}.
*/
static Path createTempFile(Path dir,
String prefix,
String suffix,
FileAttribute<?>[] attrs)
throws IOException
{
return create(dir, prefix, suffix, false, attrs);
}
@Override
public void createSymbolicLink(Path link, Path target, FileAttribute<?>... attrs)
throws IOException
{
triggerEx(target, "createSymbolicLink");
Files.createSymbolicLink(unwrap(link), unwrap(target), attrs);
}
@Override
public void createDirectory(Path dir, FileAttribute<?>... attrs)
throws IOException
{
triggerEx(dir, "createDirectory");
Files.createDirectory(unwrap(dir), attrs);
}
@Override
public SeekableByteChannel newByteChannel(Path file,
Set<? extends OpenOption> options,
FileAttribute<?>... attrs)
throws IOException
{
triggerEx(file, "newByteChannel");
return Files.newByteChannel(unwrap(file), options, attrs);
}
@Override
public SeekableByteChannel newByteChannel(Path path,
Set<? extends OpenOption> options,
FileAttribute<?>... attrs)
throws IOException
{
if (options.contains(CustomOption.IGNORE)) {
ignoreCount++;
options.remove(CustomOption.IGNORE);
}
return super.newByteChannel(path, options, attrs);
}
/**
* Creates a temporary file in the given directory, or in in the
* temporary directory if dir is {@code null}.
*/
static Path createTempFile(Path dir,
String prefix,
String suffix,
FileAttribute<?>[] attrs)
throws IOException
{
return create(dir, prefix, suffix, false, attrs);
}
/**
* Creates a temporary directory in the given directory, or in in the
* temporary directory if dir is {@code null}.
*/
static Path createTempDirectory(Path dir,
String prefix,
FileAttribute<?>[] attrs)
throws IOException
{
return create(dir, prefix, null, true, attrs);
}
/**
* Used by createDirectories to attempt to create a directory. A no-op
* if the directory already exists.
*/
private static void createAndCheckIsDirectory(Path dir,
FileAttribute<?>... attrs)
throws IOException
{
try {
createDirectory(dir, attrs);
} catch (FileAlreadyExistsException x) {
if (!isDirectory(dir, LinkOption.NOFOLLOW_LINKS))
throw x;
}
}
@Override
public SeekableByteChannel newByteChannel(Path path,
Set<? extends OpenOption> options,
FileAttribute<?>... attrs)
throws IOException
{
if (options.contains(CustomOption.IGNORE)) {
ignoreCount++;
options.remove(CustomOption.IGNORE);
}
return super.newByteChannel(path, options, attrs);
}
@Override
public SeekableByteChannel newByteChannel(Path file,
Set<? extends OpenOption> options,
FileAttribute<?>... attrs)
throws IOException
{
triggerEx(file, "newByteChannel");
return Files.newByteChannel(unwrap(file), options, attrs);
}
/**
* Creates a new and empty file, if the file does not exist.
*
* @param file the path to the file to create
* @param attributes an optional list of file attributes to set atomically when creating the file
* @return the file
* @throws IOException if an I/O error occurs or the parent directory does not exist
* @see Files#createFile(Path, FileAttribute[])
*/
public static Path createIfNotExists(Path file, FileAttribute<?>... attributes) throws IOException {
Objects.requireNonNull(file, "'file' must not be null");
Objects.requireNonNull(attributes, "'attributes' must not be null");
try {
return Files.createFile(file, attributes);
}
catch (FileAlreadyExistsException ex) {
return file;
}
}
/**
* Create a file, if it does not already exist.
*
* @param path The path to the file to create.
* @param attrs An optional list of file attributes to set atomically when creating the file.
* @return {@code true} if the file was created.
* @throws IOException If an I/O error occurs or the parent directory does not exist.
*/
public static boolean createFileIfMissing(Path path, FileAttribute<?>... attrs) throws IOException {
requireNonNull(path);
try {
java.nio.file.Files.createFile(path, attrs);
} catch (FileAlreadyExistsException e) {
return false;
}
return true;
}
/**
* Creates a temporary file in the given directory, or in in the
* temporary directory if dir is {@code null}.
*/
static Path createTempFile(Path dir,
String prefix,
String suffix,
FileAttribute<?>[] attrs)
throws IOException
{
return create(dir, prefix, suffix, false, attrs);
}