Java by API/java.io/FileOutputStream

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

FileOutputStream: getChannel()

 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class Main {
  private static final int BSIZE = 1024;
  public static void main(String[] args) throws Exception {
    FileChannel in = new FileInputStream("source.txt").getChannel(), out = new FileOutputStream(
        "target.txt").getChannel();
    ByteBuffer buffer = ByteBuffer.allocate(BSIZE);
    while (in.read(buffer) != -1) {
      buffer.flip();
      out.write(buffer);
      buffer.clear();
    }
  }
}





FileOutputStream: getFD()

 
import java.io.FileDescriptor;
import java.io.FileOutputStream;
public class Main {
  public static void main(String[] argv) throws Exception {
    FileOutputStream os = new FileOutputStream("outfilename");
    FileDescriptor fd = os.getFD();
    os.write(1);
    os.flush();
    fd.sync();
  }
}





FileOutputStream: write(int b)

  
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.InflaterInputStream;
public class Main {
  public static void main(String[] args) {
    for (int i = 0; i < args.length; i++) {
      if (args[i].toLowerCase().endsWith(".dfl")) {
        try {
          FileInputStream fin = new FileInputStream(args[i]);
          InflaterInputStream iis = new InflaterInputStream(fin);
          FileOutputStream fout = new FileOutputStream(args[i].substring(0, args[i].length() - 4));
          for (int c = iis.read(); c != -1; c = iis.read()) {
            fout.write(c);
          }
          fout.close();
        } catch (IOException ex) {
          System.err.println(ex);
        }
      } else {
        System.err.println(args[i] + " does not appear to be a deflated file.");
      }
    }
  }
}





new FileOutputStream(File file)

  
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class MainClass {
  public static void main(String[] args) {
    try {
      File tempFile = File.createTempFile("myfile", ".tmp");
      FileOutputStream fout = new FileOutputStream(tempFile);
      PrintStream out = new PrintStream(fout);
      out.println("some text");
    } catch (IOException ex) {
      System.out.println("There was a problem creating/writing to the temp file");
      ex.printStackTrace();
    }
  }
}





new FileOutputStream(String name)

  
/*
 * Output:
 * 
 * 
 * 
 *  
 */
import java.io.*;
public class MainClass {
  
  public static void main(String args[]) throws Exception {
    byte buf[] = new byte[]{66,67,68,69};
    OutputStream f0 = new FileOutputStream("file1.txt");
    OutputStream f1 = new FileOutputStream("file2.txt");
    OutputStream f2 = new FileOutputStream("file3.txt");
    for (int i = 0; i < buf.length; i++) {
      f0.write(buf[i]);
    }
    f0.close();
    f1.write(buf);
    f1.close();
    f2.write(buf, buf.length / 4, buf.length / 2);
    f2.close();
  }
}