Java by API/java.nio.channels/Channels

Материал из Java эксперт
Перейти к: навигация, поиск

Channels: newInputStream(ReadableByteChannel ch)

   <source lang="java">

import java.io.File; import java.io.InputStream; import java.io.RandomAccessFile; import java.nio.channels.Channels; import java.nio.channels.FileChannel; public class Main {

 public static void main(String[] argv) throws Exception {
   // Create a read/writeable file channel
   File file = new File("filename");
   FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
   InputStream is = Channels.newInputStream(channel);
   // Close the channel
   is.close();
 }

}

 </source>
   
  
 
  



Channels: newOutputStream(WritableByteChannel ch)

   <source lang="java">

import java.io.File; import java.io.OutputStream; import java.io.RandomAccessFile; import java.nio.channels.Channels; import java.nio.channels.FileChannel; public class Main {

 public static void main(String[] argv) throws Exception {
   // Create a read/writeable file channel
   File file = new File("filename");
   FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
   OutputStream os = Channels.newOutputStream(channel);
   os.close();
 }

}

 </source>