<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2FFile_Input_Output%2FNew_IO</id>
		<title>Java/File Input Output/New IO - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2FFile_Input_Output%2FNew_IO"/>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java/File_Input_Output/New_IO&amp;action=history"/>
		<updated>2026-04-07T00:43:14Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://jexp.ru/index.php?title=Java/File_Input_Output/New_IO&amp;diff=6185&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java/File_Input_Output/New_IO&amp;diff=6185&amp;oldid=prev"/>
				<updated>2010-06-01T06:03:53Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 06:03, 1 июня 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	<entry>
		<id>http://jexp.ru/index.php?title=Java/File_Input_Output/New_IO&amp;diff=6184&amp;oldid=prev</id>
		<title> в 18:01, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java/File_Input_Output/New_IO&amp;diff=6184&amp;oldid=prev"/>
				<updated>2010-05-31T18:01:43Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Copy a file using NIO ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.FileInputStream;&lt;br /&gt;
import java.io.FileOutputStream;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.nio.MappedByteBuffer;&lt;br /&gt;
import java.nio.channels.FileChannel;&lt;br /&gt;
public class MainClass {&lt;br /&gt;
  public static void main(String args[]) {&lt;br /&gt;
    FileInputStream fIn;&lt;br /&gt;
    FileOutputStream fOut;&lt;br /&gt;
    FileChannel fIChan, fOChan;&lt;br /&gt;
    long fSize;&lt;br /&gt;
    MappedByteBuffer mBuf;&lt;br /&gt;
    try {&lt;br /&gt;
      fIn = new FileInputStream(args[0]);&lt;br /&gt;
      fOut = new FileOutputStream(args[1]);&lt;br /&gt;
      fIChan = fIn.getChannel();&lt;br /&gt;
      fOChan = fOut.getChannel();&lt;br /&gt;
      fSize = fIChan.size();&lt;br /&gt;
      mBuf = fIChan.map(FileChannel.MapMode.READ_ONLY, 0, fSize);&lt;br /&gt;
      fOChan.write(mBuf); // this copies the file&lt;br /&gt;
      fIChan.close();&lt;br /&gt;
      fIn.close();&lt;br /&gt;
      fOChan.close();&lt;br /&gt;
      fOut.close();&lt;br /&gt;
    } catch (IOException exc) {&lt;br /&gt;
      System.out.println(exc);&lt;br /&gt;
      System.exit(1);&lt;br /&gt;
    } catch (ArrayIndexOutOfBoundsException exc) {&lt;br /&gt;
      System.out.println(&amp;quot;Usage: Copy from to&amp;quot;);&lt;br /&gt;
      System.exit(1);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== FileChannel: map(FileChannel.MapMode mode,long position,long size) ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.FileInputStream;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.nio.MappedByteBuffer;&lt;br /&gt;
import java.nio.channels.FileChannel;&lt;br /&gt;
public class MainClass {&lt;br /&gt;
  public static void main(String args[]) {&lt;br /&gt;
    FileInputStream fileInputStream;&lt;br /&gt;
    FileChannel fileChannel;&lt;br /&gt;
    long fileSize;&lt;br /&gt;
    MappedByteBuffer mBuf;&lt;br /&gt;
    try {&lt;br /&gt;
      fileInputStream = new FileInputStream(&amp;quot;test.txt&amp;quot;);&lt;br /&gt;
      fileChannel = fileInputStream.getChannel();&lt;br /&gt;
      fileSize = fileChannel.size();&lt;br /&gt;
      mBuf = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileSize);&lt;br /&gt;
      for (int i = 0; i &amp;lt; fileSize; i++)&lt;br /&gt;
        System.out.print((char) mBuf.get());&lt;br /&gt;
      fileChannel.close();&lt;br /&gt;
      fileInputStream.close();&lt;br /&gt;
    } catch (IOException exc) {&lt;br /&gt;
      System.out.println(exc);&lt;br /&gt;
      System.exit(1);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Use a mapped file to read a text file ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.FileInputStream;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.nio.MappedByteBuffer;&lt;br /&gt;
import java.nio.channels.FileChannel;&lt;br /&gt;
public class MainClass {&lt;br /&gt;
  public static void main(String args[]) {&lt;br /&gt;
    FileInputStream fileInputStream;&lt;br /&gt;
    FileChannel fileChannel;&lt;br /&gt;
    long fileSize;&lt;br /&gt;
    MappedByteBuffer mBuf;&lt;br /&gt;
    try {&lt;br /&gt;
      fileInputStream = new FileInputStream(&amp;quot;test.txt&amp;quot;);&lt;br /&gt;
      fileChannel = fileInputStream.getChannel();&lt;br /&gt;
      fileSize = fileChannel.size();&lt;br /&gt;
      mBuf = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileSize);&lt;br /&gt;
      for (int i = 0; i &amp;lt; fileSize; i++)&lt;br /&gt;
        System.out.print((char) mBuf.get());&lt;br /&gt;
      fileChannel.close();&lt;br /&gt;
      fileInputStream.close();&lt;br /&gt;
    } catch (IOException exc) {&lt;br /&gt;
      System.out.println(exc);&lt;br /&gt;
      System.exit(1);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Use New Java IO to write string into a file ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 * Output:&lt;br /&gt;
File stream created successfully.&lt;br /&gt;
New buffer:           position = 0  Limit = 1024  capacity = 1024&lt;br /&gt;
Buffer after loading: position = 30  Limit = 1024  capacity = 1024&lt;br /&gt;
Buffer after flip:    position = 0  Limit = 30  capacity = 1024&lt;br /&gt;
Buffer contents written to file.&lt;br /&gt;
 */&lt;br /&gt;
import java.io.File;&lt;br /&gt;
import java.io.FileOutputStream;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.io.FileNotFoundException;&lt;br /&gt;
import java.nio.ByteBuffer;&lt;br /&gt;
import java.nio.channels.FileChannel;&lt;br /&gt;
public class MainClass {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    String phrase = new String(&amp;quot;www.jexp.ru\n&amp;quot;);&lt;br /&gt;
    File aFile = new File(&amp;quot;test.txt&amp;quot;);   &lt;br /&gt;
    FileOutputStream outputFile = null;  &lt;br /&gt;
    try {&lt;br /&gt;
      outputFile = new FileOutputStream(aFile, true);&lt;br /&gt;
      System.out.println(&amp;quot;File stream created successfully.&amp;quot;);&lt;br /&gt;
    } catch (FileNotFoundException e) {&lt;br /&gt;
      e.printStackTrace(System.err);&lt;br /&gt;
    } &lt;br /&gt;
    FileChannel outChannel = outputFile.getChannel();&lt;br /&gt;
    &lt;br /&gt;
    ByteBuffer buf = ByteBuffer.allocate(1024);&lt;br /&gt;
    System.out.println(&amp;quot;New buffer:           position = &amp;quot; + buf.position()&lt;br /&gt;
                       + &amp;quot;\tLimit = &amp;quot; + buf.limit() + &amp;quot;\tcapacity = &amp;quot;&lt;br /&gt;
                       + buf.capacity());&lt;br /&gt;
    // Load the data into the buffer&lt;br /&gt;
    for (char ch : phrase.toCharArray()) {&lt;br /&gt;
      buf.putChar(ch);&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;Buffer after loading: position = &amp;quot; + buf.position()&lt;br /&gt;
                       + &amp;quot;\tLimit = &amp;quot; + buf.limit() + &amp;quot;\tcapacity = &amp;quot;&lt;br /&gt;
                       + buf.capacity());&lt;br /&gt;
    buf.flip();&lt;br /&gt;
    System.out.println(&amp;quot;Buffer after flip:    position = &amp;quot; + buf.position() &lt;br /&gt;
                       + &amp;quot;\tLimit = &amp;quot; + buf.limit() + &amp;quot;\tcapacity = &amp;quot; &lt;br /&gt;
                       + buf.capacity());&lt;br /&gt;
    try {&lt;br /&gt;
      outChannel.write(buf);&lt;br /&gt;
      outputFile.close();&lt;br /&gt;
      System.out.println(&amp;quot;Buffer contents written to file.&amp;quot;);&lt;br /&gt;
    } catch (IOException e) {&lt;br /&gt;
      e.printStackTrace(System.err);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Write A String As Bytes ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 * Output:&lt;br /&gt;
 */&lt;br /&gt;
import java.io.File;&lt;br /&gt;
import java.io.FileNotFoundException;&lt;br /&gt;
import java.io.FileOutputStream;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.nio.ByteBuffer;&lt;br /&gt;
import java.nio.channels.FileChannel;&lt;br /&gt;
public class MainClass {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    String phrase = new String(&amp;quot;www.jexp.ru API \n&amp;quot;);&lt;br /&gt;
    File aFile = new File(&amp;quot;test.txt&amp;quot;);&lt;br /&gt;
    FileOutputStream file = null;&lt;br /&gt;
    try {&lt;br /&gt;
      file = new FileOutputStream(aFile, true);&lt;br /&gt;
    } catch (FileNotFoundException e) {&lt;br /&gt;
      e.printStackTrace(System.err);&lt;br /&gt;
    }&lt;br /&gt;
    FileChannel outChannel = file.getChannel();&lt;br /&gt;
    ByteBuffer buf = ByteBuffer.allocate(phrase.length());&lt;br /&gt;
    byte[] bytes = phrase.getBytes();&lt;br /&gt;
    buf.put(bytes);&lt;br /&gt;
    buf.flip();&lt;br /&gt;
    try {&lt;br /&gt;
      outChannel.write(buf);&lt;br /&gt;
      file.close();&lt;br /&gt;
    } catch (IOException e) {&lt;br /&gt;
      e.printStackTrace(System.err);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Write to a file using the new I/O ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.FileOutputStream;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.nio.ByteBuffer;&lt;br /&gt;
import java.nio.channels.FileChannel;&lt;br /&gt;
public class MainClass {&lt;br /&gt;
  public static void main(String args[]) {&lt;br /&gt;
    FileOutputStream fileOutputStream;&lt;br /&gt;
    FileChannel fileChannel;&lt;br /&gt;
    ByteBuffer byteBuffer;&lt;br /&gt;
    try {&lt;br /&gt;
      fileOutputStream = new FileOutputStream(&amp;quot;test.txt&amp;quot;);&lt;br /&gt;
      fileChannel = fileOutputStream.getChannel();&lt;br /&gt;
      byteBuffer = ByteBuffer.allocateDirect(26);&lt;br /&gt;
      for (int i = 0; i &amp;lt; 26; i++)&lt;br /&gt;
        byteBuffer.put((byte) (&amp;quot;A&amp;quot; + i));&lt;br /&gt;
      byteBuffer.rewind();&lt;br /&gt;
      fileChannel.write(byteBuffer);&lt;br /&gt;
      fileChannel.close();&lt;br /&gt;
      fileOutputStream.close();&lt;br /&gt;
    } catch (IOException exc) {&lt;br /&gt;
      System.out.println(exc);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Write to a mapped file ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.io.RandomAccessFile;&lt;br /&gt;
import java.nio.ByteBuffer;&lt;br /&gt;
import java.nio.channels.FileChannel;&lt;br /&gt;
public class MainClass {&lt;br /&gt;
  public static void main(String args[]) {&lt;br /&gt;
    RandomAccessFile randomAccessFile;&lt;br /&gt;
    FileChannel fileChannel;&lt;br /&gt;
    ByteBuffer byteBuffer;&lt;br /&gt;
    try {&lt;br /&gt;
      randomAccessFile = new RandomAccessFile(&amp;quot;test.txt&amp;quot;, &amp;quot;rw&amp;quot;);&lt;br /&gt;
      fileChannel = randomAccessFile.getChannel();&lt;br /&gt;
      byteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, 26);&lt;br /&gt;
      for (int i = 0; i &amp;lt; 10; i++)&lt;br /&gt;
        byteBuffer.put((byte) (&amp;quot;A&amp;quot; + i));&lt;br /&gt;
      fileChannel.close();&lt;br /&gt;
      randomAccessFile.close();&lt;br /&gt;
    } catch (IOException exc) {&lt;br /&gt;
      System.out.println(exc);&lt;br /&gt;
      System.exit(1);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;/div&gt;</summary>
			</entry>

	</feed>