Java/File Input Output/BufferedOutputStream

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

Create BufferedOutputStream from FileOutputStream

 
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
class BufferedOutputStreamDemo {
  public static void main(String args[]) throws Exception {
    FileOutputStream fos = new FileOutputStream(args[0]);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    // Write 12 bytes to the file
    for (int i = 0; i < 12; i++) {
      bos.write(i);
    }
    bos.close();
  }
}





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();
  }
}