Java Code Examples for java.nio.channels.AsynchronousFileChannel#write()
The following examples show how to use
java.nio.channels.AsynchronousFileChannel#write() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProgMainNio.java From javase with MIT License | 6 votes |
public void writeFile(String filePath, String input) throws IOException { Path path = Paths.get(filePath); AsynchronousFileChannel afc = AsynchronousFileChannel.open(path, WRITE, CREATE); WriteHandler handler = new WriteHandler(); ByteBuffer dataBuffer = ByteBuffer.wrap(input.getBytes());//getDataBuffer(); Attachment attach = new Attachment(); attach.asyncChannel = afc; attach.buffer = dataBuffer; attach.path = path; afc.write(dataBuffer, 0, attach, handler); System.out.println("Sleeping for 3 seconds..."); try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
Example 2
Source File: ProgMainNio.java From javase with MIT License | 6 votes |
private void writeFile(String filePath, String input) throws IOException { Path path = Paths.get(filePath); AsynchronousFileChannel afc = AsynchronousFileChannel.open(path, WRITE, CREATE); WriteHandler handler = new WriteHandler(); ByteBuffer dataBuffer = ByteBuffer.wrap(input.getBytes());//getDataBuffer(); Attachment attach = new Attachment(); attach.asyncChannel = afc; attach.buffer = dataBuffer; attach.path = path; afc.write(dataBuffer, 0, attach, handler); System.out.println("Sleeping for 3 seconds..."); try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
Example 3
Source File: AsyncFileChannelIntegrationTest.java From tutorials with MIT License | 6 votes |
@Test public void givenPathAndContent_whenWritesToFileWithFuture_thenCorrect() throws IOException, ExecutionException, InterruptedException { final String fileName = "temp"; final Path path = Paths.get(fileName); final AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE); final ByteBuffer buffer = ByteBuffer.allocate(1024); final long position = 0; buffer.put("hello world".getBytes()); buffer.flip(); final Future<Integer> operation = fileChannel.write(buffer, position); buffer.clear(); operation.get(); final String content = readContent(path); assertEquals("hello world", content); }
Example 4
Source File: FileWrite.java From coroutines with Apache License 2.0 | 5 votes |
/*************************************** * {@inheritDoc} */ @Override protected boolean performAsyncOperation( int nBytesWritten, AsynchronousFileChannel rChannel, ByteBuffer rData, ChannelCallback<Integer, AsynchronousFileChannel> rCallback) { long nPosition = get(FILE_POSITION); if (rData.hasRemaining()) { if (nBytesWritten > 0) { nPosition += nBytesWritten; } rChannel.write(rData, nPosition, rData, rCallback); return false; } else // finished { // remove position in the case of a later restart deleteRelation(FILE_POSITION); rData.clear(); return true; } }
Example 5
Source File: JimfsAsynchronousFileChannelTest.java From jimfs with Apache License 2.0 | 5 votes |
private static void checkAsyncWrite(AsynchronousFileChannel asyncChannel) throws Throwable { ByteBuffer buf = buffer("1234567890"); assertEquals(10, (int) asyncChannel.write(buf, 0).get()); buf.flip(); SettableFuture<Integer> future = SettableFuture.create(); asyncChannel.write(buf, 0, null, setFuture(future)); assertThat(future.get(10, SECONDS)).isEqualTo(10); }
Example 6
Source File: ProgMainNio.java From javase with MIT License | 2 votes |
public static void main(String[] args) { try { //Path path = Paths.get("data/nio-data.txt"); Path path = Paths.get("data/test-write2.txt"); AsynchronousFileChannel fileChannelW = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE); ByteBuffer buffer = ByteBuffer.allocate(1024); long positionW = 0; buffer.put("test data\r using Java SE NIO \r async with Future".getBytes()); buffer.flip(); Future<Integer> operationW = fileChannelW.write(buffer, positionW); buffer.clear(); while(!operationW.isDone()); System.out.println("Write done"); AsynchronousFileChannel fileChannelR = AsynchronousFileChannel.open(path, StandardOpenOption.READ); //ByteBuffer buffer = ByteBuffer.allocate(1024); buffer = ByteBuffer.allocate(1024); long positionR = 0; Future<Integer> operationR = fileChannelR.read(buffer, positionR); while(!operationR.isDone()); buffer.flip(); byte[] data = new byte[buffer.limit()]; buffer.get(data); System.out.println(new String(data)); buffer.clear(); } catch(IOException ioe) { ioe.printStackTrace(); } }