Example usage for java.io BufferedOutputStream BufferedOutputStream

List of usage examples for java.io BufferedOutputStream BufferedOutputStream

Introduction

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

Prototype

public BufferedOutputStream(OutputStream out) 

Source Link

Document

Creates a new buffered output stream to write data to the specified underlying output stream.

Usage

From source file:Data.java

public static void main(String[] args) {
    Data data = new Data(1);
    try {/* www. j a v  a  2 s. co m*/
        ObjectOutputStream objectOut = new ObjectOutputStream(
                new BufferedOutputStream(new FileOutputStream("C:/test.bin")));
        objectOut.writeObject(data);
        System.out.println("1st Object written has value: " + data.getValue());
        data.setValue(2);
        objectOut.writeObject(data);
        System.out.println("2nd Object written has value: " + data.getValue());
        data.setValue(3);
        objectOut.writeObject(data);
        System.out.println("3rd Object written has value: " + data.getValue());
        objectOut.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
    try {
        ObjectInputStream objectIn = new ObjectInputStream(
                new BufferedInputStream(new FileInputStream("C:/test.bin")));
        Data data1 = (Data) objectIn.readObject();
        Data data2 = (Data) objectIn.readObject();
        Data data3 = (Data) objectIn.readObject();
        System.out.println(data1.equals(data2));
        System.out.println(data2.equals(data3));
        System.out.println(data1.getValue());
        System.out.println(data2.getValue());
        System.out.println(data3.getValue());
        objectIn.close();
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }
}

From source file:PrintFile.java

public static void main(String args[]) throws Exception {
    if (args.length != 2) {
        System.err.println("usage : java PrintFile port file");
        System.err.println("sample: java PrintFile LPT1 sample.prn");
        System.exit(-1);/*from   www  .  j av a 2 s.com*/
    }

    String portname = args[0];
    String filename = args[1];

    // Get port
    CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(portname);

    // Open port
    // Requires owner name and timeout
    CommPort port = portId.open("Java Printing", 30000);

    // Setup reading from file
    FileInputStream fis = new FileInputStream(filename);
    BufferedInputStream bis = new BufferedInputStream(fis);

    // Setup output
    OutputStream os = port.getOutputStream();
    BufferedOutputStream bos = new BufferedOutputStream(os);

    int c;
    while ((c = bis.read()) != -1) {
        bos.write(c);
    }

    // Close
    bos.close();
    bis.close();
    port.close();
}

From source file:GZIPcompress.java

public static void main(String[] args) throws IOException {
    if (args.length == 0) {
        System.out.println("Usage: \nGZIPcompress file\n" + "\tUses GZIP compression to compress "
                + "the file to test.gz");
        System.exit(1);/*from   w w  w.ja  v  a 2s  .  c  o  m*/
    }
    BufferedReader in = new BufferedReader(new FileReader(args[0]));
    BufferedOutputStream out = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream("test.gz")));
    System.out.println("Writing file");
    int c;
    while ((c = in.read()) != -1)
        out.write(c);
    in.close();
    out.close();
    System.out.println("Reading file");
    BufferedReader in2 = new BufferedReader(
            new InputStreamReader(new GZIPInputStream(new FileInputStream("test.gz"))));
    String s;
    while ((s = in2.readLine()) != null)
        System.out.println(s);
}

From source file:MyBean.java

public static void main(String args[]) throws IOException {
    // Create//from  w w w  . j  a va 2  s .  c o  m
    MyBean saveme = new MyBean();
    saveme.setNames(new String[] { "one", "two", "three" });
    saveme.setPoint(new Point(15, 27));
    saveme.setLength(6);
    // Save
    XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("saveme.xml")));
    encoder.writeObject(saveme);
    encoder.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    BeanInfo bi = Introspector.getBeanInfo(BeanToXmlTransient.class);
    PropertyDescriptor[] pds = bi.getPropertyDescriptors();
    for (int i = 0; i < pds.length; i++) {
        PropertyDescriptor propertyDescriptor = pds[i];
        if (propertyDescriptor.getName().equals("itemQuantities")) {
            propertyDescriptor.setValue("transient", Boolean.TRUE);
        }//from   w  w  w  .  j  av  a  2 s .c  om
    }
    BeanToXmlTransient bean = new BeanToXmlTransient();
    bean.setId(new Long(1));
    bean.setItemName("Item");
    bean.setItemColour("Red");
    bean.setItemQuantities(new Integer(100));

    XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("BeanTransient.xml")));

    encoder.writeObject(bean);
    encoder.close();

}

From source file:ZipCompress.java

public static void main(String[] args) throws IOException {
    FileOutputStream f = new FileOutputStream("test.zip");
    CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
    ZipOutputStream zos = new ZipOutputStream(csum);
    BufferedOutputStream out = new BufferedOutputStream(zos);
    zos.setComment("A test of Java Zipping");
    // No corresponding getComment(), though.
    for (int i = 0; i < args.length; i++) {
        System.out.println("Writing file " + args[i]);
        BufferedReader in = new BufferedReader(new FileReader(args[i]));
        zos.putNextEntry(new ZipEntry(args[i]));
        int c;/*from   w w w .  j  a v a  2 s . c  o m*/
        while ((c = in.read()) != -1)
            out.write(c);
        in.close();
    }
    out.close();
    // Checksum valid only after the file has been closed!
    System.out.println("Checksum: " + csum.getChecksum().getValue());
    // Now extract the files:
    System.out.println("Reading file");
    FileInputStream fi = new FileInputStream("test.zip");
    CheckedInputStream csumi = new CheckedInputStream(fi, new Adler32());
    ZipInputStream in2 = new ZipInputStream(csumi);
    BufferedInputStream bis = new BufferedInputStream(in2);
    ZipEntry ze;
    while ((ze = in2.getNextEntry()) != null) {
        System.out.println("Reading file " + ze);
        int x;
        while ((x = bis.read()) != -1)
            System.out.write(x);
    }
    System.out.println("Checksum: " + csumi.getChecksum().getValue());
    bis.close();
    // Alternative way to open and read zip files:
    ZipFile zf = new ZipFile("test.zip");
    Enumeration e = zf.entries();
    while (e.hasMoreElements()) {
        ZipEntry ze2 = (ZipEntry) e.nextElement();
        System.out.println("File: " + ze2);
        // ... and extract the data as before
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
        Socket sock = new Socket("127.0.0.1", 123456);
        byte[] mybytearray = new byte[1024];
        InputStream is = sock.getInputStream();
        FileOutputStream fos = new FileOutputStream("s.pdf");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        int bytesRead = is.read(mybytearray, 0, mybytearray.length);
        bos.write(mybytearray, 0, bytesRead);
        bos.close();//from w  w w  .  ja va  2  s .co  m
        sock.close();
    }

From source file:MyTest.DownloadFileTest.java

public static void main(String args[]) {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    String url = "http://www.myexperiment.org/workflows/16/download/Pathways_and_Gene_annotations_for_QTL_region-v7.t2flow?version=7";
    System.out.println(url.charAt(50));
    HttpGet httpget = new HttpGet(url);
    HttpEntity entity = null;//from  w w  w  .  java2s.  c o  m
    try {
        HttpResponse response = httpclient.execute(httpget);
        entity = response.getEntity();
        if (entity != null) {
            InputStream is = entity.getContent();
            String filename = "testdata/Pathways_and_Gene_annotations_for_QTL_region-v7.t2flow";
            BufferedInputStream bis = new BufferedInputStream(is);
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filename)));
            int readedByte;
            while ((readedByte = bis.read()) != -1) {
                bos.write(readedByte);
            }
            bis.close();
            bos.close();
        }
        httpclient.close();
    } catch (IOException ex) {
        Logger.getLogger(DownloadFileTest.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:com.textocat.textokit.morph.lemmatizer.util.GenerateNormalizedTextWriterDescriptor.java

public static void main(String[] args) throws ResourceInitializationException, IOException, SAXException {
    AnalysisEngineDescription anDesc = createEngineDescription(NormalizedTextWriter.class);
    try (FileOutputStream out = FileUtils.openOutputStream(new File(
            "src/main/resources/" + NormalizedTextWriter.class.getName().replace('.', '/') + ".xml"))) {
        anDesc.toXML(new BufferedOutputStream(out));
    }/*from  www .  j  av  a2s . c om*/
}

From source file:Blobs.java

public static void main(String args[]) {
    if (args.length != 1) {
        System.err.println("Syntax: <java Blobs [driver] [url] " + "[uid] [pass] [file]");
        return;/*  w ww  .  j a v a 2s. co  m*/
    }
    try {
        Class.forName(args[0]).newInstance();
        Connection con = DriverManager.getConnection(args[1], args[2], args[3]);
        File f = new File(args[4]);
        PreparedStatement stmt;

        if (!f.exists()) {
            // if the file does not exist
            // retrieve it from the database and write it to the named file
            ResultSet rs;

            stmt = con.prepareStatement("SELECT blobData " + "FROM BlobTest " + "WHERE fileName = ?");

            stmt.setString(1, args[0]);
            rs = stmt.executeQuery();
            if (!rs.next()) {
                System.out.println("No such file stored.");
            } else {
                Blob b = rs.getBlob(1);
                BufferedOutputStream os;

                os = new BufferedOutputStream(new FileOutputStream(f));
                os.write(b.getBytes(0, (int) b.length()), 0, (int) b.length());
                os.flush();
                os.close();
            }
        } else {
            // otherwise read it and save it to the database
            FileInputStream fis = new FileInputStream(f);
            byte[] tmp = new byte[1024];
            byte[] data = null;
            int sz, len = 0;

            while ((sz = fis.read(tmp)) != -1) {
                if (data == null) {
                    len = sz;
                    data = tmp;
                } else {
                    byte[] narr;
                    int nlen;

                    nlen = len + sz;
                    narr = new byte[nlen];
                    System.arraycopy(data, 0, narr, 0, len);
                    System.arraycopy(tmp, 0, narr, len, sz);
                    data = narr;
                    len = nlen;
                }
            }
            if (len != data.length) {
                byte[] narr = new byte[len];

                System.arraycopy(data, 0, narr, 0, len);
                data = narr;
            }
            stmt = con.prepareStatement("INSERT INTO BlobTest(fileName, " + "blobData) VALUES(?, ?)");
            stmt.setString(1, args[0]);
            stmt.setObject(2, data);
            stmt.executeUpdate();
            f.delete();
        }
        con.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}