Java Tutorial/File/BufferedOutputStream

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

Append string to a text file

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
public class Main {
  public static void main(String[] argv) {
    append(new File("c:\\a.txt"), "value");
  }
  public static void append(File aFile, String content) {
    try {
      PrintStream p = new PrintStream(new BufferedOutputStream(new FileOutputStream(aFile, true)));
      p.println(content);
      p.close();
    } catch (Exception e) {
      e.printStackTrace();
      System.err.println(aFile);
    }
  }
}





BufferedOutputStream

You should always wrap your OutputStream with a BufferedOutputStream for better performance. The BufferedOutputStream class has two constructors that accept an OutputStream.



public BufferedOutputStream (OutputStream out)
public BufferedOutputStream (OutputStream out, int bufferSize)





Create DataInputStream out of BufferedInputStream

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
public class PrimeReader {
  public static void main(String[] arguments) {
    try {
      FileInputStream file = new FileInputStream("400primes.dat");
      BufferedInputStream buff = new BufferedInputStream(file);
      DataInputStream data = new DataInputStream(buff);
      try {
        while (true) {
          int in = data.readInt();
          System.out.print(in + " ");
        }
      } catch (EOFException eof) {
        buff.close();
      }
    } catch (IOException e) {
      System.out.println("Error - " + e.toString());
    }
  }
}





Create DataOutputStream out of BufferedOutputStream

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class PrimeWriter {
  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());
    }
  }
}





Write byte array to file using BufferedOutputStream

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
public class Main {
  public static void main(String[] args) throws Exception {
    FileOutputStream fos = new FileOutputStream(new File("C:/Demo"));
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    bos.write("this is a test".getBytes());
    bos.flush();
    bos.close();
  }
}





Write byte to file using BufferedOutputStream

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
public class Main {
  public static void main(String[] args) throws Exception {
    FileOutputStream fos = new FileOutputStream("C:/Demo.txt");
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    byte b = 66;
    // write a character represented by ascii 66 i.e. "B"
    bos.write(b);
    bos.flush();
    bos.close();
  }
}





Write to file using BufferedOutputStream

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
public class Main {
  public static void main(String[] args) throws Exception {
    BufferedOutputStream bufferedOutput = new BufferedOutputStream(new FileOutputStream(
        "yourFile.txt"));
    bufferedOutput.write("Line one".getBytes());
    bufferedOutput.write("\n".getBytes());
    bufferedOutput.write(65);
    bufferedOutput.close();
  }
}