Example usage for java.io OutputStream close

List of usage examples for java.io OutputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

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

Usage

From source file:Main.java

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

    InputStream in = new FileInputStream("test.txt");
    OutputStream out = System.out;

    PrintableOutputStream pout = new PrintableOutputStream(out);
    for (int c = in.read(); c != -1; c = in.read()) {
        pout.write(c);/*from  w ww. j  a v a2s .c om*/
    }
    out.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String inFilename = "infile.gzip";
    GZIPInputStream in = new GZIPInputStream(new FileInputStream(inFilename));

    String outFilename = "outfile";
    OutputStream out = new FileOutputStream(outFilename);

    byte[] buf = new byte[1024];
    int len;// w w w . j  a  v a2s . c  o m
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }

    in.close();
    out.close();
}

From source file:dk.alexandra.fresco.framework.configuration.ConfigurationGenerator.java

public static void main(String args[]) {
    if (args.length == 0) {
        usage();/*www  . j a va 2s.co m*/
    }

    if (args.length % 2 != 0) {
        System.out.println("Incorrect number of arguments: " + Arrays.asList(args) + ".");
        System.out.println();
        usage();
    }

    List<Pair<String, Integer>> hosts = new LinkedList<Pair<String, Integer>>();
    int inx = 0;
    try {

        for (; inx < args.length; inx = inx + 2) {
            hosts.add(new Pair<String, Integer>(args[inx], new Integer(args[inx + 1])));
        }
    } catch (NumberFormatException ex) {
        System.out.println("Invalid argument for port: \"" + args[inx + 1] + "\".");
        System.exit(1);
    }

    inx = 1;
    try {
        for (; inx < hosts.size() + 1; inx++) {
            String filename = "party-" + inx + ".ini";
            OutputStream out = new FileOutputStream(filename);
            new ConfigurationGenerator().generate(inx, out, hosts);
            out.close();
            System.out.println("Created configuration file: " + filename + ".");
        }
    } catch (Exception ex) {
        System.out.println("Could not write to file: party-" + inx + ".ini.");
        System.exit(1);
    }

}

From source file:PKCS12Import.java

public static void main(String[] args) throws Exception {
    if (args.length < 1) {
        System.err.println("usage: java PKCS12Import {pkcs12file} [newjksfile]");
        System.exit(1);/*w  w  w  .j av a  2s.  co  m*/
    }

    File fileIn = new File(args[0]);
    File fileOut;
    if (args.length > 1) {
        fileOut = new File(args[1]);
    } else {
        fileOut = new File("newstore.jks");
    }

    if (!fileIn.canRead()) {
        System.err.println("Unable to access input keystore: " + fileIn.getPath());
        System.exit(2);
    }

    if (fileOut.exists() && !fileOut.canWrite()) {
        System.err.println("Output file is not writable: " + fileOut.getPath());
        System.exit(2);
    }

    KeyStore kspkcs12 = KeyStore.getInstance("pkcs12");
    KeyStore ksjks = KeyStore.getInstance("jks");

    System.out.print("Enter input keystore passphrase: ");
    char[] inphrase = readPassphrase();
    System.out.print("Enter output keystore passphrase: ");
    char[] outphrase = readPassphrase();

    kspkcs12.load(new FileInputStream(fileIn), inphrase);

    ksjks.load((fileOut.exists()) ? new FileInputStream(fileOut) : null, outphrase);

    Enumeration eAliases = kspkcs12.aliases();
    int n = 0;
    while (eAliases.hasMoreElements()) {
        String strAlias = (String) eAliases.nextElement();
        System.err.println("Alias " + n++ + ": " + strAlias);

        if (kspkcs12.isKeyEntry(strAlias)) {
            System.err.println("Adding key for alias " + strAlias);
            Key key = kspkcs12.getKey(strAlias, inphrase);

            Certificate[] chain = kspkcs12.getCertificateChain(strAlias);

            ksjks.setKeyEntry(strAlias, key, outphrase, chain);
        }
    }

    OutputStream out = new FileOutputStream(fileOut);
    ksjks.store(out, outphrase);
    out.close();
}

From source file:MainClass.java

public static void main(String[] args) {

    try {/*from  w w  w  .j  a v a  2 s  . c  o  m*/
        URL u = new URL("http://www.java2s.com");
        OutputStream out = new FileOutputStream("test.htm");
        InputStream in = u.openStream();
        DTD html = DTD.getDTD("html");
        System.out.println(html.getName());
        in.close();
        out.flush();
        out.close();
    } catch (Exception e) {
        System.err.println("Usage: java PageSaver url local_file");
    }

}

From source file:MainClass.java

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]);//from  ww w.j a  v a2s  .  c o  m
    }
    f0.close();
    f1.write(buf);
    f1.close();
    f2.write(buf, buf.length / 4, buf.length / 2);
    f2.close();
}

From source file:Main.java

public static void main(String[] args) {
    byte[] b = { 'h', 'e', 'l', 'l', 'o' };
    try {/*www.ja va  2  s . c  o  m*/

        OutputStream os = new FileOutputStream("test.txt");

        InputStream is = new FileInputStream("test.txt");

        os.write(b, 0, 3);

        for (int i = 0; i < 3; i++) {
            System.out.print((char) is.read());
        }
        os.close();
        is.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {
    try {//from   w  w w .  j  a  v  a  2  s.c  o m

        OutputStream os = new FileOutputStream("test.txt");

        InputStream is = new FileInputStream("test.txt");

        os.write(70);
        os.write(71);

        for (int i = 0; i < 2; i++) {
            System.out.print((char) is.read());
        }
        os.close();
        is.close();

    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {
    try {// w  w w. j a  v a  2 s  .c o  m

        OutputStream os = new FileOutputStream("test.txt");

        InputStream is = new FileInputStream("test.txt");

        // write something
        os.write('A');

        // flush the stream
        os.flush();

        // close the stream but it does nothing
        os.close();

        // read what we wrote
        System.out.println((char) is.read());
        is.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

From source file:com.rest.samples.getImage.java

public static void main(String[] args) {
    // TODO code application logic here
    String url = "https://api.adorable.io/avatars/eyes1";
    try {//w  ww  . jav a 2s. co m
        HttpClient hc = HttpClientBuilder.create().build();
        HttpGet getMethod = new HttpGet(url);
        getMethod.addHeader("accept", "application/png");
        HttpResponse res = hc.execute(getMethod);
        if (res.getStatusLine().getStatusCode() != 200) {
            throw new RuntimeException("Failed : HTTP eror code: " + res.getStatusLine().getStatusCode());
        }

        InputStream is = res.getEntity().getContent();

        OutputStream os = new FileOutputStream(new File("img.png"));
        int read = 0;
        byte[] bytes = new byte[2048];
        while ((read = is.read(bytes)) != -1) {
            os.write(bytes, 0, read);
        }
        is.close();
        os.close();
    } catch (IOException ex) {
        Logger.getLogger(SamplesUseHttpclient.class.getName()).log(Level.SEVERE, null, ex);
    }
}