List of usage examples for java.nio.file StandardOpenOption READ
StandardOpenOption READ
To view the source code for java.nio.file StandardOpenOption READ.
Click Source Link
From source file:Main.java
public static void main(String[] args) throws Exception { Path src = Paths.get("test2.txt"); SeekableByteChannel sbc = Files.newByteChannel(src, StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE); }
From source file:Test.java
public static void main(String[] args) throws Exception { AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(Paths.get("/asynchronous.txt"), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE); Future<Integer> writeFuture1 = fileChannel.write(ByteBuffer.wrap("Sample".getBytes()), 0); Future<Integer> writeFuture2 = fileChannel.write(ByteBuffer.wrap("Box".getBytes()), 0); int result;// w w w. jav a 2 s . c o m result = writeFuture1.get(); System.out.println("Sample write completed with " + result + " bytes written"); result = writeFuture2.get(); System.out.println("Box write completed with " + result + " bytes written"); }
From source file:Test.java
public static void main(String[] args) throws Exception { AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(Paths.get("/asynchronous.txt"), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE); CompletionHandler<Integer, Object> handler = new CompletionHandler<Integer, Object>() { @Override/*from w w w . j a v a 2 s. co m*/ public void completed(Integer result, Object attachment) { System.out.println("Attachment: " + attachment + " " + result + " bytes written"); System.out.println("CompletionHandler Thread ID: " + Thread.currentThread().getId()); } @Override public void failed(Throwable e, Object attachment) { System.err.println("Attachment: " + attachment + " failed with:"); e.printStackTrace(); } }; System.out.println("Main Thread ID: " + Thread.currentThread().getId()); fileChannel.write(ByteBuffer.wrap("Sample".getBytes()), 0, "First Write", handler); fileChannel.write(ByteBuffer.wrap("Box".getBytes()), 0, "Second Write", handler); }
From source file:Main.java
public static void main(String[] args) throws Exception { Path path = Paths.get("test.txt"); try (AsynchronousFileChannel afc = AsynchronousFileChannel.open(path, StandardOpenOption.READ)) { int fileSize = (int) afc.size(); ByteBuffer dataBuffer = ByteBuffer.allocate(fileSize); Future<Integer> result = afc.read(dataBuffer, 0); int readBytes = result.get(); System.out.format("%s bytes read from %s%n", readBytes, path); System.out.format("Read data is:%n"); byte[] byteData = dataBuffer.array(); Charset cs = Charset.forName("UTF-8"); String data = new String(byteData, cs); System.out.println(data); } catch (IOException ex) { ex.printStackTrace();//from w w w . j av a 2 s.c o m } }
From source file:Test.java
public static void main(String args[]) throws Exception { ExecutorService pool = new ScheduledThreadPoolExecutor(3); AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(Paths.get("data.txt"), EnumSet.of(StandardOpenOption.READ), pool); CompletionHandler<Integer, ByteBuffer> handler = new CompletionHandler<Integer, ByteBuffer>() { public synchronized void completed(Integer result, ByteBuffer attachment) { for (int i = 0; i < attachment.limit(); i++) { System.out.print((char) attachment.get(i)); }// w w w . j a va 2 s . com } public void failed(Throwable e, ByteBuffer attachment) { } }; final int bufferCount = 5; ByteBuffer buffers[] = new ByteBuffer[bufferCount]; for (int i = 0; i < bufferCount; i++) { buffers[i] = ByteBuffer.allocate(10); fileChannel.read(buffers[i], i * 10, buffers[i], handler); } pool.awaitTermination(1, TimeUnit.SECONDS); for (ByteBuffer byteBuffer : buffers) { for (int i = 0; i < byteBuffer.limit(); i++) { System.out.println((char) byteBuffer.get(i)); } } }
From source file:Test.java
public static void main(String args[]) throws Exception { ExecutorService pool = new ScheduledThreadPoolExecutor(3); AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(Paths.get("data.txt"), EnumSet.of(StandardOpenOption.READ), pool); CompletionHandler<Integer, ByteBuffer> handler = new CompletionHandler<Integer, ByteBuffer>() { @Override//from w ww . ja va 2 s. c o m public synchronized void completed(Integer result, ByteBuffer attachment) { for (int i = 0; i < attachment.limit(); i++) { System.out.println((char) attachment.get(i)); } } @Override public void failed(Throwable e, ByteBuffer attachment) { } }; final int bufferCount = 5; ByteBuffer buffers[] = new ByteBuffer[bufferCount]; for (int i = 0; i < bufferCount; i++) { buffers[i] = ByteBuffer.allocate(10); fileChannel.read(buffers[i], i * 10, buffers[i], handler); } pool.awaitTermination(1, TimeUnit.SECONDS); for (ByteBuffer byteBuffer : buffers) { for (int i = 0; i < byteBuffer.limit(); i++) { System.out.print((char) byteBuffer.get(i)); } } }
From source file:org.roda.core.plugins.plugins.base.InventoryReportPluginUtils.java
public static void mergeFiles(List<Path> files, Path mergedFile) throws IOException { try (FileChannel out = FileChannel.open(mergedFile, StandardOpenOption.APPEND, StandardOpenOption.WRITE)) { for (Path path : files) { try (FileChannel in = FileChannel.open(path, StandardOpenOption.READ)) { for (long p = 0, l = in.size(); p < l;) p += in.transferTo(p, l - p, out); }/* ww w. ja va 2 s . co m*/ } } catch (IOException e) { throw e; } }
From source file:de.elomagic.carafile.client.CaraFileUtils.java
/** * Creates a meta file from the given file. * * @param path Path of the file//w w w. j ava2 s.com * @param filename Real name of the file because it can differ from path parameter * @return * @throws IOException * @throws GeneralSecurityException */ public static final MetaData createMetaData(final Path path, final String filename) throws IOException, GeneralSecurityException { if (Files.notExists(path)) { throw new FileNotFoundException("File " + path.toString() + " not found!"); } if (Files.isDirectory(path)) { throw new IllegalArgumentException("Not a file: " + path.toString()); } MetaData md = new MetaData(); md.setSize(Files.size(path)); md.setFilename(filename); md.setCreationDate(new Date()); md.setChunkSize(DEFAULT_PIECE_SIZE); try (InputStream in = Files.newInputStream(path, StandardOpenOption.READ); BufferedInputStream bin = new BufferedInputStream(in)) { MessageDigest mdComplete = MessageDigest.getInstance(MessageDigestAlgorithms.SHA_1); byte[] buffer = new byte[DEFAULT_PIECE_SIZE]; int bytesRead; while ((bytesRead = bin.read(buffer)) > 0) { mdComplete.update(buffer, 0, bytesRead); ChunkData chunk = new ChunkData( DigestUtils.sha1Hex(new ByteArrayInputStream(buffer, 0, bytesRead))); md.addChunk(chunk); } String sha1 = Hex.encodeHexString(mdComplete.digest()); md.setId(sha1); } return md; }
From source file:org.cryptomator.ui.settings.Settings.java
public static synchronized Settings load() { if (INSTANCE == null) { try {// w ww. ja va 2 s.co m Files.createDirectories(SETTINGS_DIR); final Path settingsFile = SETTINGS_DIR.resolve(SETTINGS_FILE); final InputStream in = Files.newInputStream(settingsFile, StandardOpenOption.READ); INSTANCE = JSON_OM.readValue(in, Settings.class); return INSTANCE; } catch (IOException e) { LOG.warn("Failed to load settings, creating new one."); INSTANCE = Settings.defaultSettings(); } } return INSTANCE; }
From source file:io.druid.segment.writeout.TmpFileSegmentWriteOutMedium.java
@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);//from w ww . j a va 2s . c o m closer.register(ch); return new FileWriteOutBytes(file, ch); }