java.nio.channels.FileChannel.open()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(8.0k)|赞(0)|评价(0)|浏览(184)

本文整理了Java中java.nio.channels.FileChannel.open()方法的一些代码示例,展示了FileChannel.open()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。FileChannel.open()方法的具体详情如下:
包路径:java.nio.channels.FileChannel
类名称:FileChannel
方法名:open

FileChannel.open介绍

暂无

代码示例

代码示例来源:origin: spring-projects/spring-framework

/**
 * This implementation opens a FileChannel for the underlying file.
 * @see java.nio.channels.FileChannel
 */
@Override
public WritableByteChannel writableChannel() throws IOException {
  return FileChannel.open(this.filePath, StandardOpenOption.WRITE);
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * @param filePath     the path of the file to write to.
 * @param charset      the charset to use when writing characters.
 * @param forceOnFlush set to true if you want {@link FileChannel#force(boolean)} to be called on {@link #flush()}.
 * @param options      the options for opening the file.
 * @throws IOException if something went wrong.
 */
FileChannelWriter(Path filePath, Charset charset, boolean forceOnFlush, boolean forceOnClose, OpenOption... options) throws IOException {
  this.charset = charset;
  this.forceOnFlush = forceOnFlush;
  this.forceOnClose = forceOnClose;
  channel = FileChannel.open(filePath, options);
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * This implementation opens a FileChannel for the underlying file.
 * @see java.nio.channels.FileChannel
 */
@Override
public ReadableByteChannel readableChannel() throws IOException {
  try {
    return FileChannel.open(this.filePath, StandardOpenOption.READ);
  }
  catch (NoSuchFileException ex) {
    throw new FileNotFoundException(ex.getMessage());
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public WritableByteChannel writableChannel() throws IOException {
  return FileChannel.open(getFile().toPath(), StandardOpenOption.WRITE);
}

代码示例来源:origin: apache/kafka

/**
 * Attempt to read a file as a string
 * @throws IOException
 */
public static String readFileAsString(String path, Charset charset) throws IOException {
  if (charset == null) charset = Charset.defaultCharset();
  try (FileChannel fc = FileChannel.open(Paths.get(path))) {
    MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
    return charset.decode(bb).toString();
  }
}

代码示例来源:origin: neo4j/neo4j

@Override
public void truncate( File path, long size ) throws IOException
{
  try ( FileChannel channel = FileChannel.open( path( path ) ) )
  {
    channel.truncate( size );
  }
}

代码示例来源:origin: neo4j/neo4j

public static FileChannel open( Path path, OpenMode openMode ) throws IOException
{
  return FileChannel.open( path, convertOpenMode( openMode ) );
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * This implementation returns a FileChannel for the given URI-identified
 * resource, provided that it refers to a file in the file system.
 * @since 5.0
 * @see #getFile()
 */
@Override
public ReadableByteChannel readableChannel() throws IOException {
  try {
    // Try file system channel
    return FileChannel.open(getFile().toPath(), StandardOpenOption.READ);
  }
  catch (FileNotFoundException | NoSuchFileException ex) {
    // Fall back to InputStream adaptation in superclass
    return super.readableChannel();
  }
}

代码示例来源:origin: pxb1988/dex2jar

@Override
protected void doCommandLine() throws Exception {
  if (remainingArgs.length < 1) {
    throw new HelpException("<core.xxxx> is required.");
  }
  Path core = new File(remainingArgs[0]).toPath();
  try (SeekableByteChannel channel = FileChannel.open(core, StandardOpenOption.READ);) {
    List<Long> possibleOdexs = findPossibleOdexLocation(channel);
    extractDex(channel, possibleOdexs, core.getFileName().toString());
  }
}

代码示例来源:origin: apache/incubator-druid

@Override
public WriteOutBytes makeWriteOutBytes() throws IOException
{
 File file = File.createTempFile("filePeon", null, dir);
 FileChannel ch = FileChannel.open(
   file.toPath(),
   StandardOpenOption.READ,
   StandardOpenOption.WRITE
 );
 closer.register(file::delete);
 closer.register(ch);
 return new FileWriteOutBytes(file, ch);
}

代码示例来源:origin: apache/flink

LocalRecoverableFsDataOutputStream(LocalRecoverable resumable) throws IOException {
  this.targetFile = checkNotNull(resumable.targetFile());
  this.tempFile = checkNotNull(resumable.tempFile());
  if (!tempFile.exists()) {
    throw new FileNotFoundException("File Not Found: " + tempFile.getAbsolutePath());
  }
  this.fileChannel = FileChannel.open(tempFile.toPath(), StandardOpenOption.WRITE, StandardOpenOption.APPEND);
  if (this.fileChannel.position() < resumable.offset()) {
    throw new IOException("Missing data in tmp file: " + tempFile.getAbsolutePath());
  }
  this.fileChannel.truncate(resumable.offset());
  this.fos = Channels.newOutputStream(fileChannel);
}

代码示例来源:origin: apache/flink

LocalRecoverableFsDataOutputStream(File targetFile, File tempFile) throws IOException {
  this.targetFile = checkNotNull(targetFile);
  this.tempFile = checkNotNull(tempFile);
  this.fileChannel = FileChannel.open(tempFile.toPath(), StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW);
  this.fos = Channels.newOutputStream(fileChannel);
}

代码示例来源:origin: neo4j/neo4j

public static void main( String[] args ) throws IOException
  {
    Path path = Paths.get( args[0] );
    try ( FileChannel channel = FileChannel.open( path, StandardOpenOption.READ, StandardOpenOption.WRITE );
       java.nio.channels.FileLock lock = channel.lock() )
    {
      System.out.println( LOCKED_OUTPUT );
      System.out.flush();
      System.in.read();
    }
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public Mono<Void> writeWith(Path file, long position, long count) {
  return doCommit(() ->
      Mono.defer(() -> {
        try (FileChannel source = FileChannel.open(file, StandardOpenOption.READ)) {
          StreamSinkChannel destination = this.exchange.getResponseChannel();
          Channels.transferBlocking(destination, source, position, count);
          return Mono.empty();
        }
        catch (IOException ex) {
          return Mono.error(ex);
        }
      }));
}

代码示例来源:origin: apache/kafka

/**
 * Checks that unmap doesn't throw exceptions.
 */
@Test
public void testUnmap() throws Exception {
  File file = TestUtils.tempFile();
  try (FileChannel channel = FileChannel.open(file.toPath())) {
    MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_ONLY, 0, 0);
    MappedByteBuffers.unmap(file.getAbsolutePath(), map);
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void readByteChannel() throws Exception {
  URI uri = this.resource.getURI();
  Flux<DataBuffer> result =
      DataBufferUtils.readByteChannel(() -> FileChannel.open(Paths.get(uri), StandardOpenOption.READ),
          this.bufferFactory, 3);
  verifyReadData(result);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void readAndWriteByteChannel() throws Exception {
  Path source = Paths.get(
      DataBufferUtilsTests.class.getResource("DataBufferUtilsTests.txt").toURI());
  Flux<DataBuffer> sourceFlux =
      DataBufferUtils
          .readByteChannel(() -> FileChannel.open(source, StandardOpenOption.READ),
              this.bufferFactory, 3);
  Path destination = Files.createTempFile("DataBufferUtilsTests", null);
  WritableByteChannel channel = Files.newByteChannel(destination, StandardOpenOption.WRITE);
  DataBufferUtils.write(sourceFlux, channel)
      .subscribe(DataBufferUtils.releaseConsumer(),
          throwable -> fail(throwable.getMessage()),
          () -> {
            try {
              String expected = String.join("", Files.readAllLines(source));
              String result = String.join("", Files.readAllLines(destination));
              assertEquals(expected, result);
            }
            catch (IOException e) {
              fail(e.getMessage());
            }
            finally {
              DataBufferUtils.closeChannel(channel);
            }
          });
}

代码示例来源:origin: square/okio

@Test public void writableChannelNioFile() throws Exception {
 File file = temporaryFolder.newFile();
 FileChannel fileChannel = FileChannel.open(file.toPath(), StandardOpenOption.WRITE);
 testWritableByteChannel(fileChannel);
 BufferedSource emitted = Okio.buffer(Okio.source(file));
 assertEquals("defghijklmnopqrstuvw", emitted.readUtf8());
 emitted.close();
}

代码示例来源:origin: square/okio

@Test public void readableChannelNioFile() throws Exception {
 File file = temporaryFolder.newFile();
 BufferedSink initialData = Okio.buffer(Okio.sink(file));
 initialData.writeUtf8("abcdefghijklmnopqrstuvwxyz");
 initialData.close();
 FileChannel fileChannel = FileChannel.open(file.toPath(), StandardOpenOption.READ);
 testReadableByteChannel(fileChannel);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void readByteChannelCancel() throws Exception {
  URI uri = this.resource.getURI();
  Flux<DataBuffer> result =
      DataBufferUtils.readByteChannel(() -> FileChannel.open(Paths.get(uri), StandardOpenOption.READ),
          this.bufferFactory, 3);
  StepVerifier.create(result)
      .consumeNextWith(stringConsumer("foo"))
      .thenCancel()
      .verify();
}

相关文章