java.nio.channels.AsynchronousFileChannel#write()源码实例Demo

下面列出了java.nio.channels.AsynchronousFileChannel#write() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: javase   文件: ProgMainNio.java
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();
}
  }
 
源代码2 项目: javase   文件: ProgMainNio.java
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();
}
  }
 
@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);
}
 
源代码4 项目: coroutines   文件: FileWrite.java
/***************************************
 * {@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;
	}
}
 
源代码5 项目: jimfs   文件: JimfsAsynchronousFileChannelTest.java
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);
}
 
源代码6 项目: javase   文件: ProgMainNio.java
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();
	}
       
}