Java by API/java.io/BufferedOutputStream — различия между версиями

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

Текущая версия на 14:32, 31 мая 2010

BufferedOutputStream: write(byte[] b, int off, int len)

 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Main {
  public static void main(String[] args) throws Exception {
    String fromFileName = "from.txt";
    String toFileName = "to.txt";
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(fromFileName));
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(toFileName));
    byte[] buff = new byte[32 * 1024];
    int len;
    while ((len = in.read(buff)) > 0)
      out.write(buff, 0, len);
    in.close();
    out.close();
  }
}





BufferedOutputStream: write(int i)

  
/*
 * Output:
 */
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
public class MainClass {
  public static void main(String args[]) {
    try {
      FileOutputStream fos = new FileOutputStream(args[0]);
      BufferedOutputStream bos = new BufferedOutputStream(fos);
      for (int i = 0; i < 12; i++) {
        bos.write(i);
      }
      bos.close();
    } catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
}





new BufferedOutputStream(OutputStream out)

  
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Main {
  public static void main(String[] arguments) {
    int[] primes = new int[400];
    int numPrimes = 0;
    int candidate = 2;
    while (numPrimes < 400) {
      primes[numPrimes] = candidate;
      numPrimes++;
      candidate++;
    }
    try {
      FileOutputStream file = new FileOutputStream("p.dat");
      BufferedOutputStream buff = new BufferedOutputStream(file);
      DataOutputStream data = new DataOutputStream(buff);
      for (int i = 0; i < 400; i++)
        data.writeInt(primes[i]);
      data.close();
    } catch (IOException e) {
      System.out.println("Error - " + e.toString());
    }
  }
}