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

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java/File_Input_Output/FileInputStream&amp;diff=6215&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/File_Input_Output/FileInputStream&amp;diff=6215&amp;oldid=prev"/>
				<updated>2010-06-01T06:04:27Z</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:04, 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://www.jexp.ru/index.php?title=Java/File_Input_Output/FileInputStream&amp;diff=6214&amp;oldid=prev</id>
		<title> в 18:01, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/File_Input_Output/FileInputStream&amp;diff=6214&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 ==&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;
import java.io.File;&lt;br /&gt;
import java.io.FileInputStream;&lt;br /&gt;
import java.io.FileOutputStream;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] args) throws Exception {&lt;br /&gt;
    FileInputStream fin = null;&lt;br /&gt;
    FileOutputStream fout = null;&lt;br /&gt;
    File file = new File(&amp;quot;C:/myfile1.txt&amp;quot;);&lt;br /&gt;
    fin = new FileInputStream(file);&lt;br /&gt;
    fout = new FileOutputStream(&amp;quot;C:/myfile2.txt&amp;quot;);&lt;br /&gt;
    byte[] buffer = new byte[1024];&lt;br /&gt;
    int bytesRead;&lt;br /&gt;
    while ((bytesRead = fin.read(buffer)) &amp;gt; 0) {&lt;br /&gt;
      fout.write(buffer, 0, bytesRead);&lt;br /&gt;
    }&lt;br /&gt;
    fin.close();&lt;br /&gt;
    fout.close();&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;
== Copy a file with FileOutputStream and FileInputStream ==&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;
/*&lt;br /&gt;
 * Copyright (c) 2004 David Flanagan.  All rights reserved.&lt;br /&gt;
 * This code is from the book Java Examples in a Nutshell, 3nd Edition.&lt;br /&gt;
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.&lt;br /&gt;
 * You may study, use, and modify it for any non-commercial purpose,&lt;br /&gt;
 * including teaching and use in open-source projects.&lt;br /&gt;
 * You may distribute it non-commercially as long as you retain this notice.&lt;br /&gt;
 * For a commercial use license, or to purchase the book, &lt;br /&gt;
 * please visit http://www.davidflanagan.ru/javaexamples3.&lt;br /&gt;
 */&lt;br /&gt;
import java.io.BufferedReader;&lt;br /&gt;
import java.io.File;&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.io.InputStreamReader;&lt;br /&gt;
/**&lt;br /&gt;
 * This class is a standalone program to copy a file, and also defines a static&lt;br /&gt;
 * copy() method that other programs can use to copy files.&lt;br /&gt;
 */&lt;br /&gt;
public class FileCopy {&lt;br /&gt;
  /** The main() method of the standalone program. Calls copy(). */&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    if (args.length != 2) // Check arguments&lt;br /&gt;
      System.err.println(&amp;quot;Usage: java FileCopy &amp;lt;source&amp;gt; &amp;lt;destination&amp;gt;&amp;quot;);&lt;br /&gt;
    else {&lt;br /&gt;
      // Call copy() to do the copy; display any error messages&lt;br /&gt;
      try {&lt;br /&gt;
        copy(args[0], args[1]);&lt;br /&gt;
      } catch (IOException e) {&lt;br /&gt;
        System.err.println(e.getMessage());&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * The static method that actually performs the file copy. Before copying the&lt;br /&gt;
   * file, however, it performs a lot of tests to make sure everything is as it&lt;br /&gt;
   * should be.&lt;br /&gt;
   */&lt;br /&gt;
  public static void copy(String from_name, String to_name) throws IOException {&lt;br /&gt;
    File from_file = new File(from_name); // Get File objects from Strings&lt;br /&gt;
    File to_file = new File(to_name);&lt;br /&gt;
    // First make sure the source file exists, is a file, and is readable.&lt;br /&gt;
    // These tests are also performed by the FileInputStream constructor&lt;br /&gt;
    // which throws a FileNotFoundException if they fail.&lt;br /&gt;
    if (!from_file.exists())&lt;br /&gt;
      abort(&amp;quot;no such source file: &amp;quot; + from_name);&lt;br /&gt;
    if (!from_file.isFile())&lt;br /&gt;
      abort(&amp;quot;can&amp;quot;t copy directory: &amp;quot; + from_name);&lt;br /&gt;
    if (!from_file.canRead())&lt;br /&gt;
      abort(&amp;quot;source file is unreadable: &amp;quot; + from_name);&lt;br /&gt;
    // If the destination is a directory, use the source file name&lt;br /&gt;
    // as the destination file name&lt;br /&gt;
    if (to_file.isDirectory())&lt;br /&gt;
      to_file = new File(to_file, from_file.getName());&lt;br /&gt;
    // If the destination exists, make sure it is a writeable file&lt;br /&gt;
    // and ask before overwriting it. If the destination doesn&amp;quot;t&lt;br /&gt;
    // exist, make sure the directory exists and is writeable.&lt;br /&gt;
    if (to_file.exists()) {&lt;br /&gt;
      if (!to_file.canWrite())&lt;br /&gt;
        abort(&amp;quot;destination file is unwriteable: &amp;quot; + to_name);&lt;br /&gt;
      // Ask whether to overwrite it&lt;br /&gt;
      System.out.print(&amp;quot;Overwrite existing file &amp;quot; + to_file.getName() + &amp;quot;? (Y/N): &amp;quot;);&lt;br /&gt;
      System.out.flush();&lt;br /&gt;
      // Get the user&amp;quot;s response.&lt;br /&gt;
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));&lt;br /&gt;
      String response = in.readLine();&lt;br /&gt;
      // Check the response. If not a Yes, abort the copy.&lt;br /&gt;
      if (!response.equals(&amp;quot;Y&amp;quot;) &amp;amp;&amp;amp; !response.equals(&amp;quot;y&amp;quot;))&lt;br /&gt;
        abort(&amp;quot;existing file was not overwritten.&amp;quot;);&lt;br /&gt;
    } else {&lt;br /&gt;
      // If file doesn&amp;quot;t exist, check if directory exists and is&lt;br /&gt;
      // writeable. If getParent() returns null, then the directory is&lt;br /&gt;
      // the current dir. so look up the user.dir system property to&lt;br /&gt;
      // find out what that is.&lt;br /&gt;
      String parent = to_file.getParent(); // The destination directory&lt;br /&gt;
      if (parent == null) // If none, use the current directory&lt;br /&gt;
        parent = System.getProperty(&amp;quot;user.dir&amp;quot;);&lt;br /&gt;
      File dir = new File(parent); // Convert it to a file.&lt;br /&gt;
      if (!dir.exists())&lt;br /&gt;
        abort(&amp;quot;destination directory doesn&amp;quot;t exist: &amp;quot; + parent);&lt;br /&gt;
      if (dir.isFile())&lt;br /&gt;
        abort(&amp;quot;destination is not a directory: &amp;quot; + parent);&lt;br /&gt;
      if (!dir.canWrite())&lt;br /&gt;
        abort(&amp;quot;destination directory is unwriteable: &amp;quot; + parent);&lt;br /&gt;
    }&lt;br /&gt;
    // If we&amp;quot;ve gotten this far, then everything is okay.&lt;br /&gt;
    // So we copy the file, a buffer of bytes at a time.&lt;br /&gt;
    FileInputStream from = null; // Stream to read from source&lt;br /&gt;
    FileOutputStream to = null; // Stream to write to destination&lt;br /&gt;
    try {&lt;br /&gt;
      from = new FileInputStream(from_file); // Create input stream&lt;br /&gt;
      to = new FileOutputStream(to_file); // Create output stream&lt;br /&gt;
      byte[] buffer = new byte[4096]; // To hold file contents&lt;br /&gt;
      int bytes_read; // How many bytes in buffer&lt;br /&gt;
      // Read a chunk of bytes into the buffer, then write them out,&lt;br /&gt;
      // looping until we reach the end of the file (when read() returns&lt;br /&gt;
      // -1). Note the combination of assignment and comparison in this&lt;br /&gt;
      // while loop. This is a common I/O programming idiom.&lt;br /&gt;
      while ((bytes_read = from.read(buffer)) != -1)&lt;br /&gt;
        // Read until EOF&lt;br /&gt;
        to.write(buffer, 0, bytes_read); // write&lt;br /&gt;
    }&lt;br /&gt;
    // Always close the streams, even if exceptions were thrown&lt;br /&gt;
    finally {&lt;br /&gt;
      if (from != null)&lt;br /&gt;
        try {&lt;br /&gt;
          from.close();&lt;br /&gt;
        } catch (IOException e) {&lt;br /&gt;
          ;&lt;br /&gt;
        }&lt;br /&gt;
      if (to != null)&lt;br /&gt;
        try {&lt;br /&gt;
          to.close();&lt;br /&gt;
        } catch (IOException e) {&lt;br /&gt;
          ;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  /** A convenience method to throw an exception */&lt;br /&gt;
  private static void abort(String msg) throws IOException {&lt;br /&gt;
    throw new IOException(&amp;quot;FileCopy: &amp;quot; + msg);&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;
== Copying One File to Another ==&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;
import java.io.File;&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.io.InputStream;&lt;br /&gt;
import java.io.OutputStream;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] argv) throws Exception {&lt;br /&gt;
    copy(new File(&amp;quot;src.dat&amp;quot;), new File(&amp;quot;dst.dat&amp;quot;));&lt;br /&gt;
  }&lt;br /&gt;
  static void copy(File src, File dst) throws IOException {&lt;br /&gt;
    InputStream in = new FileInputStream(src);&lt;br /&gt;
    OutputStream out = new FileOutputStream(dst);&lt;br /&gt;
    byte[] buf = new byte[1024];&lt;br /&gt;
    int len;&lt;br /&gt;
    while ((len = in.read(buf)) &amp;gt; 0) {&lt;br /&gt;
      out.write(buf, 0, len);&lt;br /&gt;
    }&lt;br /&gt;
    in.close();&lt;br /&gt;
    out.close();&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;
== Copying One File to Another with FileChannel ==&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;
import java.io.FileInputStream;&lt;br /&gt;
import java.io.FileOutputStream;&lt;br /&gt;
import java.nio.channels.FileChannel;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] argv) throws Exception {&lt;br /&gt;
    FileChannel srcChannel = new FileInputStream(&amp;quot;srcFilename&amp;quot;).getChannel();&lt;br /&gt;
    FileChannel dstChannel = new FileOutputStream(&amp;quot;dstFilename&amp;quot;).getChannel();&lt;br /&gt;
    dstChannel.transferFrom(srcChannel, 0, srcChannel.size());&lt;br /&gt;
    srcChannel.close();&lt;br /&gt;
    dstChannel.close();&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;
== Count characters with FileInputStream ==&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;
/*&lt;br /&gt;
 * Copyright (c) 1995 - 2008 Sun Microsystems, Inc.  All rights reserved.&lt;br /&gt;
 *&lt;br /&gt;
 * Redistribution and use in source and binary forms, with or without&lt;br /&gt;
 * modification, are permitted provided that the following conditions&lt;br /&gt;
 * are met:&lt;br /&gt;
 *&lt;br /&gt;
 *   - Redistributions of source code must retain the above copyright&lt;br /&gt;
 *     notice, this list of conditions and the following disclaimer.&lt;br /&gt;
 *&lt;br /&gt;
 *   - Redistributions in binary form must reproduce the above copyright&lt;br /&gt;
 *     notice, this list of conditions and the following disclaimer in the&lt;br /&gt;
 *     documentation and/or other materials provided with the distribution.&lt;br /&gt;
 *&lt;br /&gt;
 *   - Neither the name of Sun Microsystems nor the names of its&lt;br /&gt;
 *     contributors may be used to endorse or promote products derived&lt;br /&gt;
 *     from this software without specific prior written permission.&lt;br /&gt;
 *&lt;br /&gt;
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &amp;quot;AS&lt;br /&gt;
 * IS&amp;quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,&lt;br /&gt;
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR&lt;br /&gt;
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR&lt;br /&gt;
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,&lt;br /&gt;
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,&lt;br /&gt;
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR&lt;br /&gt;
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF&lt;br /&gt;
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING&lt;br /&gt;
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS&lt;br /&gt;
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&lt;br /&gt;
 */&lt;br /&gt;
import java.io.FileInputStream;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.io.InputStream;&lt;br /&gt;
public class Count {&lt;br /&gt;
  public static void countChars(InputStream in) throws IOException {&lt;br /&gt;
    int count = 0;&lt;br /&gt;
    while (in.read() != -1)&lt;br /&gt;
      count++;&lt;br /&gt;
    System.out.println(&amp;quot;Counted &amp;quot; + count + &amp;quot; chars.&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
  public static void main(String[] args) throws Exception {&lt;br /&gt;
    if (args.length &amp;gt;= 1)&lt;br /&gt;
      countChars(new FileInputStream(args[0]));&lt;br /&gt;
    else&lt;br /&gt;
      System.err.println(&amp;quot;Usage: Count filename&amp;quot;);&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;
== Display file contents in hexadecimal ==&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;
 &lt;br /&gt;
import java.io.FileInputStream;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] args) throws Exception {&lt;br /&gt;
    FileInputStream fis = new FileInputStream(&amp;quot;/home/username/data.txt&amp;quot;);&lt;br /&gt;
    int i = 0;&lt;br /&gt;
    int count = 0;&lt;br /&gt;
    while ((i = fis.read()) != -1) {&lt;br /&gt;
      if (i != -1) {&lt;br /&gt;
        System.out.printf(&amp;quot;%02X &amp;quot;, i);&lt;br /&gt;
        count++;&lt;br /&gt;
      }&lt;br /&gt;
      if (count == 16) {&lt;br /&gt;
        System.out.println(&amp;quot;&amp;quot;);&lt;br /&gt;
        count = 0;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    fis.close();&lt;br /&gt;
  }&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;
== Read and copy with FileInputStream and FileOutputStream ==&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;
import java.io.FileInputStream;&lt;br /&gt;
import java.io.FileOutputStream;&lt;br /&gt;
import java.io.InputStream;&lt;br /&gt;
import java.io.OutputStream;&lt;br /&gt;
public class FileInputOutputExample {&lt;br /&gt;
  public static void main(String[] args) throws Exception {&lt;br /&gt;
    InputStream is = new FileInputStream(&amp;quot;in.txt&amp;quot;);&lt;br /&gt;
    OutputStream os = new FileOutputStream(&amp;quot;out.txt&amp;quot;);&lt;br /&gt;
    int c;&lt;br /&gt;
    while ((c = is.read()) != -1) {&lt;br /&gt;
      System.out.print((char) c);&lt;br /&gt;
      os.write(c);&lt;br /&gt;
    }&lt;br /&gt;
    is.close();&lt;br /&gt;
    os.close();&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;
== Read bytes and display their hexadecimal values. ==&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;
import java.io.FileInputStream;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] argv) throws Exception {&lt;br /&gt;
    FileInputStream fin = new FileInputStream(&amp;quot;text.txt&amp;quot;);&lt;br /&gt;
    int len;&lt;br /&gt;
    byte data[] = new byte[16];&lt;br /&gt;
    // Read bytes until EOF is encountered.&lt;br /&gt;
    do {&lt;br /&gt;
      len = fin.read(data);&lt;br /&gt;
      for (int j = 0; j &amp;lt; len; j++)&lt;br /&gt;
        System.out.printf(&amp;quot;%02X &amp;quot;, data[j]);&lt;br /&gt;
    } while (len != -1);&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;
== Read file character by character ==&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;
import java.io.File;&lt;br /&gt;
import java.io.FileInputStream;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    File file = new File(args[0]);&lt;br /&gt;
    if (!file.exists()) {&lt;br /&gt;
      System.out.println(args[0] + &amp;quot; does not exist.&amp;quot;);&lt;br /&gt;
      return;&lt;br /&gt;
    }&lt;br /&gt;
    if (!(file.isFile() &amp;amp;&amp;amp; file.canRead())) {&lt;br /&gt;
      System.out.println(file.getName() + &amp;quot; cannot be read from.&amp;quot;);&lt;br /&gt;
      return;&lt;br /&gt;
    }&lt;br /&gt;
    try {&lt;br /&gt;
      FileInputStream fis = new FileInputStream(file);&lt;br /&gt;
      char current;&lt;br /&gt;
      while (fis.available() &amp;gt; 0) {&lt;br /&gt;
        current = (char) fis.read();&lt;br /&gt;
        System.out.print(current);&lt;br /&gt;
      }&lt;br /&gt;
    } catch (IOException e) {&lt;br /&gt;
      e.printStackTrace();&lt;br /&gt;
    }&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;
== Read file in byte array using FileInputStream ==&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;
import java.io.File;&lt;br /&gt;
import java.io.FileInputStream;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] args) throws Exception {&lt;br /&gt;
    File file = new File(&amp;quot;C:/String.txt&amp;quot;);&lt;br /&gt;
    FileInputStream fin = new FileInputStream(file);&lt;br /&gt;
    byte fileContent[] = new byte[(int) file.length()];&lt;br /&gt;
    fin.read(fileContent);&lt;br /&gt;
    System.out.println(new String(fileContent));&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;
== Read file using FileInputStream ==&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;
import java.io.File;&lt;br /&gt;
import java.io.FileInputStream;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] args) throws Exception {&lt;br /&gt;
    File file = new File(&amp;quot;C:/String.txt&amp;quot;);&lt;br /&gt;
    StringBuffer strContent = new StringBuffer();&lt;br /&gt;
    FileInputStream fin = new FileInputStream(file);&lt;br /&gt;
    int ch;&lt;br /&gt;
    while ((ch = fin.read()) != -1) {&lt;br /&gt;
      strContent.append((char) ch);&lt;br /&gt;
    }&lt;br /&gt;
    fin.close();&lt;br /&gt;
    System.out.println(strContent);&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;
== Reading a File into a Byte Array: reads the entire contents of a file into a byte array ==&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;
import java.io.File;&lt;br /&gt;
import java.io.FileInputStream;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.io.InputStream;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] argv) throws Exception {&lt;br /&gt;
    File file = new File(&amp;quot;c:\\a.bat&amp;quot;);&lt;br /&gt;
    InputStream is = new FileInputStream(file);&lt;br /&gt;
    long length = file.length();&lt;br /&gt;
    if (length &amp;gt; Integer.MAX_VALUE) {&lt;br /&gt;
      System.out.println(&amp;quot;File is too large&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    byte[] bytes = new byte[(int) length];&lt;br /&gt;
    int offset = 0;&lt;br /&gt;
    int numRead = 0;&lt;br /&gt;
    while (numRead &amp;gt;= 0) {&lt;br /&gt;
      numRead = is.read(bytes, offset, bytes.length - offset);&lt;br /&gt;
      offset += numRead;&lt;br /&gt;
    }&lt;br /&gt;
    if (offset &amp;lt; bytes.length) {&lt;br /&gt;
      throw new IOException(&amp;quot;Could not completely read file &amp;quot; + file.getName());&lt;br /&gt;
    }&lt;br /&gt;
    is.close();&lt;br /&gt;
    System.out.println(new String(bytes));&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;
== Read one byte from 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;
import java.io.FileInputStream;&lt;br /&gt;
import java.io.FileNotFoundException;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public void readOneByte() throws FileNotFoundException {&lt;br /&gt;
    FileInputStream file = null;&lt;br /&gt;
    byte x = -1;&lt;br /&gt;
    try {&lt;br /&gt;
      file = new FileInputStream(&amp;quot;fileName&amp;quot;);&lt;br /&gt;
      x = (byte) file.read();&lt;br /&gt;
    } catch (FileNotFoundException f) {&lt;br /&gt;
      throw f;&lt;br /&gt;
    } catch (IOException i) {&lt;br /&gt;
      i.printStackTrace();&lt;br /&gt;
    } finally {&lt;br /&gt;
      try {&lt;br /&gt;
        if (file != null) {&lt;br /&gt;
          file.close();&lt;br /&gt;
        }&lt;br /&gt;
      } catch (IOException e) {&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;/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;
== Resettable File InputStream ==&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;
/****************************************************************&lt;br /&gt;
 * Licensed to the Apache Software Foundation (ASF) under one   *&lt;br /&gt;
 * or more contributor license agreements.  See the NOTICE file *&lt;br /&gt;
 * distributed with this work for additional information        *&lt;br /&gt;
 * regarding copyright ownership.  The ASF licenses this file   *&lt;br /&gt;
 * to you under the Apache License, Version 2.0 (the            *&lt;br /&gt;
 * &amp;quot;License&amp;quot;); you may not use this file except in compliance   *&lt;br /&gt;
 * with the License.  You may obtain a copy of the License at   *&lt;br /&gt;
 *                                                              *&lt;br /&gt;
 *   http://www.apache.org/licenses/LICENSE-2.0                 *&lt;br /&gt;
 *                                                              *&lt;br /&gt;
 * Unless required by applicable law or agreed to in writing,   *&lt;br /&gt;
 * software distributed under the License is distributed on an  *&lt;br /&gt;
 * &amp;quot;AS IS&amp;quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *&lt;br /&gt;
 * KIND, either express or implied.  See the License for the    *&lt;br /&gt;
 * specific language governing permissions and limitations      *&lt;br /&gt;
 * under the License.                                           *&lt;br /&gt;
 ****************************************************************/&lt;br /&gt;
&lt;br /&gt;
import java.io.BufferedInputStream;&lt;br /&gt;
import java.io.File;&lt;br /&gt;
import java.io.FileInputStream;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.io.InputStream;&lt;br /&gt;
/**&lt;br /&gt;
 * @author  Federico Barbieri &amp;lt;fede@apache.org&amp;gt;&lt;br /&gt;
 */&lt;br /&gt;
public class ResettableFileInputStream&lt;br /&gt;
    extends InputStream&lt;br /&gt;
{&lt;br /&gt;
    protected static final int DEFAULT_BUFFER_SIZE = 1024;&lt;br /&gt;
    protected final String m_filename;&lt;br /&gt;
    protected int m_bufferSize;&lt;br /&gt;
    protected InputStream m_inputStream;&lt;br /&gt;
    protected long m_position;&lt;br /&gt;
    protected long m_mark;&lt;br /&gt;
    protected boolean m_isMarkSet;&lt;br /&gt;
    public ResettableFileInputStream( final File file )&lt;br /&gt;
        throws IOException&lt;br /&gt;
    {&lt;br /&gt;
        this( file.getCanonicalPath() );&lt;br /&gt;
    }&lt;br /&gt;
    public ResettableFileInputStream( final String filename )&lt;br /&gt;
        throws IOException&lt;br /&gt;
    {&lt;br /&gt;
        this( filename, DEFAULT_BUFFER_SIZE );&lt;br /&gt;
    }&lt;br /&gt;
    public ResettableFileInputStream( final String filename, final int bufferSize )&lt;br /&gt;
        throws IOException&lt;br /&gt;
    {&lt;br /&gt;
        m_bufferSize = bufferSize;&lt;br /&gt;
        m_filename = filename;&lt;br /&gt;
        m_position = 0;&lt;br /&gt;
        m_inputStream = newStream();&lt;br /&gt;
    }&lt;br /&gt;
    public void mark( final int readLimit )&lt;br /&gt;
    {&lt;br /&gt;
        m_isMarkSet = true;&lt;br /&gt;
        m_mark = m_position;&lt;br /&gt;
        m_inputStream.mark( readLimit );&lt;br /&gt;
    }&lt;br /&gt;
    public boolean markSupported()&lt;br /&gt;
    {&lt;br /&gt;
        return true;&lt;br /&gt;
    }&lt;br /&gt;
    public void reset()&lt;br /&gt;
        throws IOException&lt;br /&gt;
    {&lt;br /&gt;
        if( !m_isMarkSet )&lt;br /&gt;
        {&lt;br /&gt;
            throw new IOException( &amp;quot;Unmarked Stream&amp;quot; );&lt;br /&gt;
        }&lt;br /&gt;
        try&lt;br /&gt;
        {&lt;br /&gt;
            m_inputStream.reset();&lt;br /&gt;
        }&lt;br /&gt;
        catch( final IOException ioe )&lt;br /&gt;
        {&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                m_inputStream.close();&lt;br /&gt;
                m_inputStream = newStream();&lt;br /&gt;
                m_inputStream.skip( m_mark );&lt;br /&gt;
                m_position = m_mark;&lt;br /&gt;
            }&lt;br /&gt;
            catch( final Exception e )&lt;br /&gt;
            {&lt;br /&gt;
                throw new IOException( &amp;quot;Cannot reset current Stream: &amp;quot; + e.getMessage() );&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    protected InputStream newStream()&lt;br /&gt;
        throws IOException&lt;br /&gt;
    {&lt;br /&gt;
        return new BufferedInputStream( new FileInputStream( m_filename ), m_bufferSize );&lt;br /&gt;
    }&lt;br /&gt;
    public int available()&lt;br /&gt;
        throws IOException&lt;br /&gt;
    {&lt;br /&gt;
        return m_inputStream.available();&lt;br /&gt;
    }&lt;br /&gt;
    public void close() throws IOException&lt;br /&gt;
    {&lt;br /&gt;
        m_inputStream.close();&lt;br /&gt;
    }&lt;br /&gt;
    public int read() throws IOException&lt;br /&gt;
    {&lt;br /&gt;
        m_position++;&lt;br /&gt;
        return m_inputStream.read();&lt;br /&gt;
    }&lt;br /&gt;
    public int read( final byte[] bytes, final int offset, final int length )&lt;br /&gt;
        throws IOException&lt;br /&gt;
    {&lt;br /&gt;
        final int count = m_inputStream.read( bytes, offset, length );&lt;br /&gt;
        m_position += count;&lt;br /&gt;
        return count;&lt;br /&gt;
    }&lt;br /&gt;
    public long skip( final long count )&lt;br /&gt;
        throws IOException&lt;br /&gt;
    {&lt;br /&gt;
        m_position += count;&lt;br /&gt;
        return m_inputStream.skip( count );&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;
== Skip n bytes while reading the file using FileInputStream ==&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;
       &lt;br /&gt;
import java.io.File;&lt;br /&gt;
import java.io.FileInputStream;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] args) throws Exception {&lt;br /&gt;
    File file = new File(&amp;quot;C:/String.txt&amp;quot;);&lt;br /&gt;
    FileInputStream fin = new FileInputStream(file);&lt;br /&gt;
    int ch;&lt;br /&gt;
    // skip first 10 bytes&lt;br /&gt;
    fin.skip(10);&lt;br /&gt;
    while ((ch = fin.read()) != -1){&lt;br /&gt;
      System.out.print((char) ch);&lt;br /&gt;
    }&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;
== Use Java NIO to Copy 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;
import java.io.FileInputStream;&lt;br /&gt;
import java.io.FileOutputStream;&lt;br /&gt;
import java.nio.ByteBuffer;&lt;br /&gt;
import java.nio.channels.FileChannel;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] args) throws Exception {&lt;br /&gt;
    String source = &amp;quot;s.txt&amp;quot;;&lt;br /&gt;
    String destination = &amp;quot;d.txt&amp;quot;;&lt;br /&gt;
    FileInputStream fis = new FileInputStream(source);&lt;br /&gt;
    FileOutputStream fos = new FileOutputStream(destination);&lt;br /&gt;
    FileChannel fci = fis.getChannel();&lt;br /&gt;
    FileChannel fco = fos.getChannel();&lt;br /&gt;
    ByteBuffer buffer = ByteBuffer.allocate(1024);&lt;br /&gt;
    while (true) {&lt;br /&gt;
      int read = fci.read(buffer);&lt;br /&gt;
      if (read == -1)&lt;br /&gt;
        break;&lt;br /&gt;
      buffer.flip();&lt;br /&gt;
      fco.write(buffer);&lt;br /&gt;
      buffer.clear();&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;/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>