Java Tutorial/File/Stream

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

File copier

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class MainClass {
  public static void main(String[] args) {
    if (args.length != 2) {
      System.err.println("Usage: java MainClass infile outfile");
    }
    try {
      copy(args[0], args[1]);
    } catch (IOException ex) {
      System.err.println(ex);
    }
  }
  public static void copy(String inFile, String outFile) throws IOException {
    FileInputStream fin = null;
    FileOutputStream fout = null;
    try {
      fin = new FileInputStream(inFile);
      fout = new FileOutputStream(outFile);
      copy(fin, fout);
    } finally {
      try {
        if (fin != null)
          fin.close();
      } catch (IOException ex) {
      }
      try {
        if (fout != null)
          fout.close();
      } catch (IOException ex) {
      }
    }
  }
  public static void copy(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    while (true) {
      int bytesRead = in.read(buffer);
      if (bytesRead == -1)
        break;
      out.write(buffer, 0, bytesRead);
    }
  }
}





File typer

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class MainClass {
  public static void main(String[] args) throws IOException {
    if (args.length != 1) {
      System.err.println("Usage: java FileTyper filename");
      return;
    }
    typeFile(args[0]);
  }
  public static void typeFile(String filename) throws IOException {
    FileInputStream fin = new FileInputStream(filename);
    try {
      copy(fin, System.out);
    } finally {
      fin.close();
    }
  }
  public static void copy(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    while (true) {
      int bytesRead = in.read(buffer);
      if (bytesRead == -1)
        break;
      out.write(buffer, 0, bytesRead);
    }
  }
}





Open Stream from URL

import java.net.*;
import java.io.*;
public class MainClass {
  public static void main(String[] args) throws IOException {
    InputStream in = null;
    try {
      URL u = new URL("http://www.jexp.ru");
      in = u.openStream();
      for (int c = in.read(); c != -1; c = in.read()) {
        System.out.write(c);
      }
      in.close();
    } 
    catch (MalformedURLException ex) {
      System.err.println("not a URL Java understands.");
    }
    finally {
      if (in != null) in.close();
    }
  }
}





Stream Copier

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class MainClass {
  public static void main(String[] args) {
    try {
      copy(System.in, System.out);
    } catch (IOException ex) {
      System.err.println(ex);
    }
  }
  public static void copy(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    while (true) {
      int bytesRead = in.read(buffer);
      if (bytesRead == -1)
        break;
      out.write(buffer, 0, bytesRead);
    }
  }
}





Understanding Streams

  1. A stream "is" a sequence of bytes
  2. When writing data to a stream, the stream is called an output stream.
  3. When reading data from a stream, the stream is called an input stream.
  4. If a stream has a buffer in memory, it is a buffered stream.
  5. Binary Streams contain binary data.
  6. Character Streams have character data and are used for storing and retrieving text

ClassDescriptionInputStreamThe base class for byte stream input operations.OutputStreamThe base class for byte stream output operations.BufferedInputStreamBuffers input from another stream in memory to make the read operations more efficient.