Example usage for java.io FileOutputStream close

List of usage examples for java.io FileOutputStream close

Introduction

In this page you can find the example usage for java.io FileOutputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes this file output stream and releases any system resources associated with this stream.

Usage

From source file:GetURL.java

public static void main(String args[]) throws Exception {
    URL url = new URL("http://www.google.com");
    InputStream urlstream = url.openStream();
    byte[] buffer = new byte[0];
    byte[] chunk = new byte[4096];
    int count;/*from   w  ww .  j ava2  s  .  co  m*/

    while ((count = urlstream.read(chunk)) >= 0) {
        byte[] t = new byte[buffer.length + count];
        System.arraycopy(buffer, 0, t, 0, buffer.length);
        System.arraycopy(chunk, 0, t, buffer.length, count);
        buffer = t;
    }

    String filename = (url.getFile()).replace('/', File.separatorChar);
    File f1 = new File(filename);
    filename = f1.getName();
    FileOutputStream f = null;
    f = new FileOutputStream(filename);
    f.write(buffer);
    f.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    byte[] vals = { 65, 66, 67, 68, 69, 70, 71, 72, 73, 74 };
    FileOutputStream fout = new FileOutputStream("Test.dat");
    for (int i = 0; i < vals.length; i += 2)
        fout.write(vals[i]);/*from  w ww  .  j  a  v a 2  s . c  o  m*/
    fout.close();
}

From source file:Main.java

    public static void main(String[] args) throws Exception {
  byte[] imageBytes = LoadImage("C:/Java_Dev/bear.png");
  MongoClient mongo = new MongoClient("127.0.0.1");
  String dbName = "GridFSTestJava";
  DB db = mongo.getDB(dbName);/* ww  w .  jav a 2s.  c  om*/

  GridFS fs = new GridFS(db);
  GridFSInputFile in = fs.createFile(imageBytes);
  in.save();

  GridFSDBFile out = fs.findOne(new BasicDBObject("_id", in.getId()));

  FileOutputStream outputImage = new FileOutputStream("C:/Temp/bearCopy.bmp");
  out.writeTo(outputImage);
  outputImage.close();
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    String cacert = "mytest.cer";
    String lfcert = "lf_signed.cer";
    String lfstore = "lfkeystore";
    char[] lfstorepass = "wshr.ut".toCharArray();
    char[] lfkeypass = "wshr.ut".toCharArray();
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    FileInputStream in1 = new FileInputStream(cacert);
    java.security.cert.Certificate cac = cf.generateCertificate(in1);
    in1.close();/*from w w  w  . ja v a2s . c o m*/
    FileInputStream in2 = new FileInputStream(lfcert);
    java.security.cert.Certificate lfc = cf.generateCertificate(in2);
    in2.close();
    java.security.cert.Certificate[] cchain = { lfc, cac };
    FileInputStream in3 = new FileInputStream(lfstore);
    KeyStore ks = KeyStore.getInstance("JKS");
    ks.load(in3, lfstorepass);
    PrivateKey prk = (PrivateKey) ks.getKey("lf", lfkeypass);
    ks.setKeyEntry("lf_signed", prk, lfstorepass, cchain);
    FileOutputStream out4 = new FileOutputStream("lfnewstore");
    ks.store(out4, "newpass".toCharArray());
    out4.close();
}

From source file:FileSplitter.java

public static void main(String args[]) throws Exception {
    FileInputStream fis = new FileInputStream(args[0]);
    int size = 1024;
    byte buffer[] = new byte[size];

    int count = 0;
    while (true) {
        int i = fis.read(buffer, 0, size);
        if (i == -1)
            break;

        String filename = args[1] + count;
        FileOutputStream fos = new FileOutputStream(filename);
        fos.write(buffer, 0, i);/*w w  w . ja v  a  2 s.  c  o m*/
        fos.flush();
        fos.close();

        ++count;
    }
}

From source file:de.brazzy.nikki.util.ThumbnailBenchmark.java

public static void main(String[] args) throws Exception {
    ImageReader r = new ImageReader(new File("C:/tmp/test.JPG"), DateTimeZone.UTC);
    r.readMainImage();// w ww .j a va2s. co  m
    long start = System.nanoTime();
    byte[] t = r.scale(150, false, true);
    System.out.println();
    System.out.println("ThumpnailRescaleOp: " + (System.nanoTime() - start) / (1000 * 1000 * 1000.0));
    File out = File.createTempFile("thumbnail", ".jpg");
    FileOutputStream stream = new FileOutputStream(out);
    IOUtils.write(t, stream);
    stream.close();
    Desktop.getDesktop().open(out);

    start = System.nanoTime();
    t = r.scale(150, false, false);
    System.out.println();
    System.out.println("ResampleOp: " + (System.nanoTime() - start) / (1000 * 1000 * 1000.0));
    out = File.createTempFile("thumbnail", ".jpg");
    stream = new FileOutputStream(out);
    IOUtils.write(t, stream);
    stream.close();
    Desktop.getDesktop().open(out);
}

From source file:Main.java

public static void main(String args[]) throws Exception {

    FileOutputStream fos = new FileOutputStream(args[0]);

    DataOutputStream dos = new DataOutputStream(fos);

    dos.writeShort(1);//from w  w w .  j  a v a 2  s.  c  o  m

    fos.close();
}

From source file:Main.java

public static void main(String[] arguments) {
    int[] data = { 137, 89, 82, 181, 50, 220, 103, 20, 0, 59 };
    try {/*  ww w . j  a  va2s .c  om*/
        FileOutputStream file = new FileOutputStream("pic.dat");
        for (int i = 0; i < data.length; i++)
            file.write(data[i]);
        file.close();
    } catch (IOException e) {
        System.out.println("Error - " + e.toString());
    }
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {

    URL u = new URL("http://www.java2s.com/binary.dat");
    URLConnection uc = u.openConnection();
    String contentType = uc.getContentType();
    int contentLength = uc.getContentLength();
    if (contentType.startsWith("text/") || contentLength == -1) {
        throw new IOException("This is not a binary file.");
    }//from w  ww  . j av a  2s  .  c  om
    InputStream raw = uc.getInputStream();
    InputStream in = new BufferedInputStream(raw);
    byte[] data = new byte[contentLength];
    int bytesRead = 0;
    int offset = 0;
    while (offset < contentLength) {
        bytesRead = in.read(data, offset, data.length - offset);
        if (bytesRead == -1)
            break;
        offset += bytesRead;
    }
    in.close();

    if (offset != contentLength) {
        throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes");
    }

    String filename = u.getFile().substring(filename.lastIndexOf('/') + 1);
    FileOutputStream out = new FileOutputStream(filename);
    out.write(data);
    out.flush();
    out.close();
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    byte[] b = new byte[1];
    URL url = new URL("http://www.server.com/a.gif");
    URLConnection urlConnection = url.openConnection();
    urlConnection.connect();/*from   ww w.j a va2 s.  c  o  m*/
    DataInputStream di = new DataInputStream(urlConnection.getInputStream());

    FileOutputStream fo = new FileOutputStream("a.gif");
    while (-1 != di.read(b, 0, 1))
        fo.write(b, 0, 1);
    di.close();
    fo.close();
}