本文整理汇总了Java中java.nio.file.OpenOption类的典型用法代码示例。如果您正苦于以下问题:Java OpenOption类的具体用法?Java OpenOption怎么用?Java OpenOption使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
OpenOption类属于java.nio.file包,在下文中一共展示了OpenOption类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: newFileChannel
import java.nio.file.OpenOption; //导入依赖的package包/类
/**
* Open/creates file, returning FileChannel to access the file
*
* @param pathForWindows
* The path of the file to open/create
* @param pathToCheck
* The path used for permission checks (if security manager)
*/
static FileChannel newFileChannel(String pathForWindows,
String pathToCheck,
Set<? extends OpenOption> options,
long pSecurityDescriptor)
throws WindowsException
{
Flags flags = Flags.toFlags(options);
// default is reading; append => writing
if (!flags.read && !flags.write) {
if (flags.append) {
flags.write = true;
} else {
flags.read = true;
}
}
// validation
if (flags.read && flags.append)
throw new IllegalArgumentException("READ + APPEND not allowed");
if (flags.append && flags.truncateExisting)
throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed");
FileDescriptor fdObj = open(pathForWindows, pathToCheck, flags, pSecurityDescriptor);
return FileChannelImpl.open(fdObj, pathForWindows, flags.read, flags.write, flags.append, null);
}
示例2: writeToZip
import java.nio.file.OpenOption; //导入依赖的package包/类
/**
* Write the dex program resources to @code{archive} and the proguard resource as its sibling.
*/
public void writeToZip(Path archive, OutputMode outputMode) throws IOException {
OpenOption[] options =
new OpenOption[] {StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING};
try (Closer closer = Closer.create()) {
try (ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(archive, options))) {
List<Resource> dexProgramSources = getDexProgramResources();
for (int i = 0; i < dexProgramSources.size(); i++) {
ZipEntry zipEntry = new ZipEntry(outputMode.getOutputPath(dexProgramSources.get(i), i));
byte[] bytes =
ByteStreams.toByteArray(closer.register(dexProgramSources.get(i).getStream()));
zipEntry.setSize(bytes.length);
out.putNextEntry(zipEntry);
out.write(bytes);
out.closeEntry();
}
}
}
}
示例3: fuseOpenFlagsToNioOpenOptions
import java.nio.file.OpenOption; //导入依赖的package包/类
public Set<OpenOption> fuseOpenFlagsToNioOpenOptions(Set<OpenFlags> flags) {
Set<OpenOption> result = new HashSet<>();
if (flags.contains(OpenFlags.O_RDONLY) || flags.contains(OpenFlags.O_RDWR)) {
result.add(StandardOpenOption.READ);
}
if (flags.contains(OpenFlags.O_WRONLY) || flags.contains(OpenFlags.O_RDWR)) {
result.add(StandardOpenOption.WRITE);
}
if (flags.contains(OpenFlags.O_APPEND)) {
result.add(StandardOpenOption.APPEND);
}
if (flags.contains(OpenFlags.O_TRUNC)) {
result.add(StandardOpenOption.TRUNCATE_EXISTING);
}
return result;
}
示例4: asFileDownload
import java.nio.file.OpenOption; //导入依赖的package包/类
/**
* Returns a {@code BodyHandler<Path>} that returns a
* {@link BodyProcessor BodyProcessor}<{@link Path}>
* where the download directory is specified, but the filename is
* obtained from the {@code Content-Disposition} response header. The
* {@code Content-Disposition} header must specify the <i>attachment</i> type
* and must also contain a
* <i>filename</i> parameter. If the filename specifies multiple path
* components only the final component is used as the filename (with the
* given directory name). When the {@code HttpResponse} object is
* returned, the body has been completely written to the file and {@link
* #body()} returns a {@code Path} object for the file. The returned {@code Path} is the
* combination of the supplied directory name and the file name supplied
* by the server. If the destination directory does not exist or cannot
* be written to, then the response will fail with an {@link IOException}.
*
* @param directory the directory to store the file in
* @param openOptions open options
* @return a response body handler
*/
public static BodyHandler<Path> asFileDownload(Path directory, OpenOption... openOptions) {
return (status, headers) -> {
String dispoHeader = headers.firstValue("Content-Disposition")
.orElseThrow(() -> unchecked(new IOException("No Content-Disposition")));
if (!dispoHeader.startsWith("attachment;")) {
throw unchecked(new IOException("Unknown Content-Disposition type"));
}
int n = dispoHeader.indexOf("filename=");
if (n == -1) {
throw unchecked(new IOException("Bad Content-Disposition type"));
}
int lastsemi = dispoHeader.lastIndexOf(';');
String disposition;
if (lastsemi < n) {
disposition = dispoHeader.substring(n + 9);
} else {
disposition = dispoHeader.substring(n + 9, lastsemi);
}
Path file = Paths.get(directory.toString(), disposition);
return BodyProcessor.asFile(file, openOptions);
};
}
示例5: fchCopy
import java.nio.file.OpenOption; //导入依赖的package包/类
private static void fchCopy(Path src, Path dst) throws IOException
{
Set<OpenOption> read = new HashSet<>();
read.add(READ);
Set<OpenOption> openwrite = new HashSet<>();
openwrite.add(CREATE_NEW);
openwrite.add(WRITE);
try (FileChannel srcFc = src.getFileSystem()
.provider()
.newFileChannel(src, read);
FileChannel dstFc = dst.getFileSystem()
.provider()
.newFileChannel(dst, openwrite))
{
ByteBuffer bb = ByteBuffer.allocate(8192);
while (srcFc.read(bb) >= 0) {
bb.flip();
dstFc.write(bb);
bb.clear();
}
}
}
示例6: newByteChannel
import java.nio.file.OpenOption; //导入依赖的package包/类
/**
* File access is checked using {@link #checkAccess(BlobStoreContext, CloudPath, Set)}
* always with {@link AclEntryPermission#WRITE_DATA} and {@link AclEntryPermission#ADD_FILE},
* and optionally with {@link AclEntryPermission#APPEND_DATA} if <em>options</em> contains
* {@link StandardOpenOption#APPEND}.
* @see CloudFileChannel
*/
@Override
public CloudFileChannel newByteChannel(BlobStoreContext context, CloudPath path,
Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException {
EnumSet<AclEntryPermission> channelPerms = EnumSet.noneOf(AclEntryPermission.class);
options.forEach(o -> {
AclEntryPermission aclPerm = openOptionToAclEntryPermission(o);
if (aclPerm != null) {
channelPerms.add(aclPerm);
}
});
// Check the parent path for file add
if (channelPerms.remove(AclEntryPermission.ADD_FILE)) {
checkAccess(context, path.getParent(), CREATE_NEW_FILE_PERMS);
}
// Check file access if the file exists
if (path.exists()) {
checkAccess(context, path, channelPerms);
}
// Create the channel
return new CloudFileChannel(context, path, getCloudFileChannelTransport(), options, attrs);
}
开发者ID:brdara,项目名称:java-cloud-filesystem-provider,代码行数:32,代码来源:DefaultCloudFileSystemImplementation.java
示例7: openOptionToAclEntryPermission
import java.nio.file.OpenOption; //导入依赖的package包/类
/**
* Transforms a {@link StandardOpenOption} into an {@link AclEntryPermission}. Other
* {@link OpenOption} types are ignored.
* @param o
* @return The option as an ACL permission or null if this is not applicable.
*/
protected AclEntryPermission openOptionToAclEntryPermission(OpenOption o) {
if (o instanceof StandardOpenOption) {
switch ((StandardOpenOption)o) {
case APPEND: return AclEntryPermission.APPEND_DATA;
case CREATE: return AclEntryPermission.ADD_FILE;
case CREATE_NEW: return AclEntryPermission.ADD_FILE;
case DELETE_ON_CLOSE: return AclEntryPermission.DELETE;
case READ: return AclEntryPermission.READ_DATA;
case TRUNCATE_EXISTING: return AclEntryPermission.APPEND_DATA;
case WRITE: return AclEntryPermission.WRITE_DATA;
default: return null;
}
}
return null;
}
开发者ID:brdara,项目名称:java-cloud-filesystem-provider,代码行数:23,代码来源:DefaultCloudFileSystemImplementation.java
示例8: newByteChannel
import java.nio.file.OpenOption; //导入依赖的package包/类
@Override
public SeekableByteChannel newByteChannel( Set<? extends OpenOption> options, FileAttribute<?>... attrs )
throws IOException
{
MemoryFileStore mfs = fs.getFileStore();
MemoryFile file;
if( options.contains( StandardOpenOption.CREATE_NEW ) ) {
// FIXME need to fail if it already exists
file = mfs.getOrCreateFile( this );
}
else if( options.contains( StandardOpenOption.CREATE ) ) {
file = mfs.getOrCreateFile( this );
}
else {
file = mfs.findFile( this );
}
return file.newByteChannel( this, options, attrs );
}
示例9: newByteChannel
import java.nio.file.OpenOption; //导入依赖的package包/类
public SeekableByteChannel newByteChannel( Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs )
throws IOException
{
boolean append = true;
if( options.contains( StandardOpenOption.APPEND ) ) {
append = true;
}
else if( options.contains( StandardOpenOption.TRUNCATE_EXISTING ) ) {
append = false;
}
if( options.contains( StandardOpenOption.READ ) ) {
return new MemChannel.Read( buffer );
}
if( options.contains( StandardOpenOption.WRITE ) ) {
if( !append ) {
truncate();
}
return new MemChannel.Appender( buffer );
}
throw new IOException( "Must read or write" );
}
示例10: getFileChannel
import java.nio.file.OpenOption; //导入依赖的package包/类
/**
* Opens a file, returning a seekable byte channel to access the file.
* See
* @throws IllegalArgumentException
* if the set contains an invalid combination of options
* @throws UnsupportedOperationException
* if an unsupported open option is specified
* @throws IOException
* if an I/O error occurs
*
* @see java.nio.channels.FileChannel#open(Path,Set,FileAttribute[])
*/
public FileChannel getFileChannel(Set<? extends OpenOption> options) throws IOException {
for (OpenOption option : options) {
checkOpenOption(option);
}
File localFile;
try {
localFile = getLocalFile();
} catch (IOException e) {
throw new NoSuchFileException(toPath().toString(), null, e.getMessage());
}
FileChannel fileChannel = FileChannel.open(localFile.toPath(), options);
boolean write = options.contains(StandardOpenOption.WRITE) || options.contains(StandardOpenOption.APPEND);
boolean read = options.contains(StandardOpenOption.READ) || !write;
return new MCRFileChannel(this, fileChannel, write);
}
示例11: WriteAsyncFileConfig
import java.nio.file.OpenOption; //导入依赖的package包/类
WriteAsyncFileConfig(C config, File file, Charset charset, ConfigWriter<? super C> writer,
WritingMode writingMode, ConfigParser<?, ? super C> parser,
ParsingMode parsingMode, FileNotFoundAction nefAction) {
super(config);
this.file = file;
this.charset = charset;
this.writer = writer;
this.parser = parser;
this.parsingMode = parsingMode;
this.nefAction = nefAction;
if (writingMode == WritingMode.APPEND) {
this.openOptions = new OpenOption[]{WRITE, CREATE};
} else {
this.openOptions = new OpenOption[]{WRITE, CREATE, TRUNCATE_EXISTING};
}
this.writeCompletedHandler = new WriteCompletedHandler();
}
示例12: writeCsv
import java.nio.file.OpenOption; //导入依赖的package包/类
/**
* 報告書をCSVファイルに書き込む
*
* @param path ファイル
* @param header ヘッダー
* @param body 内容
* @param applend 追記フラグ
* @throws IOException
*/
public static void writeCsv(Path path, String[] header, List<String[]> body, boolean applend)
throws IOException {
OpenOption[] options;
if (applend) {
options = new OpenOption[] { StandardOpenOption.CREATE, StandardOpenOption.APPEND };
} else {
options = new OpenOption[] { StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING };
}
try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(path, options))) {
if (!Files.exists(path) || (Files.size(path) <= 0)) {
out.write(StringUtils.join(header, ',').getBytes(AppConstants.CHARSET));
out.write("\r\n".getBytes(AppConstants.CHARSET));
}
for (String[] colums : body) {
out.write(StringUtils.join(colums, ',').getBytes(AppConstants.CHARSET));
out.write("\r\n".getBytes(AppConstants.CHARSET));
}
}
}
示例13: writeContent
import java.nio.file.OpenOption; //导入依赖的package包/类
private long writeContent(byte[] content, HTTPEnvRequest env, OpenOption... openOptions)
{
content = env.getSettings()
.getFileManager()
.getProperty(
IContentMutation.class,
"local::content::mutation",
NoContentMutation.getInstance())
.transform(content, this, env);
try
{
Files.write(getPhysicalFile(env).toPath(), content, openOptions);
}
catch (IOException ex)
{
throw new UnexpectedException(ex);
}
this.lastModified = Instant.now();
return content.length;
}
示例14: getSaveFileChannel
import java.nio.file.OpenOption; //导入依赖的package包/类
public FileChannel getSaveFileChannel(String fileName) {
FileDialog fd = new FileDialog(shlSimpleChatExample, SWT.SAVE);
fd.setText("Choose a file for the incoming file transfer");
fd.setFileName(fileName);
String selected = fd.open();
Path path = FileSystems.getDefault().getPath(selected);
OpenOption[] read = { StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW };
try {
System.out.println("File will be saved to: "+path);
FileChannel fileChannel = FileChannel.open(path, read);
return fileChannel;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
示例15: startup
import java.nio.file.OpenOption; //导入依赖的package包/类
@Override
public void startup() {
this.fileSystem = FileSystems.getDefault();
this.provider = fileSystem.provider();
this.readOptions = new HashSet<OpenOption>();
this.readOptions.add(StandardOpenOption.READ);
this.readOptions.add(StandardOpenOption.SYNC);
try {
int i = inputPathString.length;
fileChannel = new FileChannel[i];
while (--i>=0) {
fileChannel[i] = provider.newFileChannel(fileSystem.getPath(inputPathString[i]), readOptions);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}