Example usage for java.io FileOutputStream flush

List of usage examples for java.io FileOutputStream flush

Introduction

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

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes this output stream and forces any buffered output bytes to be written out.

Usage

From source file:Main.java

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

    int howMany = 20;

    ByteArrayOutputStream bout = new ByteArrayOutputStream(howMany * 4);
    DataOutputStream dout = new DataOutputStream(bout);

    for (int i = 0; i <= 20; i++) {
        dout.writeInt(i);/*from ww w .  ja  v  a 2s. c  o  m*/
    }

    FileOutputStream fout = new FileOutputStream("fibonacci.dat");
    try {
        bout.writeTo(fout);
        fout.flush();
    } finally {
        fout.close();
    }
}

From source file:MainClass.java

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

    int howMany = 20;

    // To avoid resizing the buffer, calculate the size of the
    // byte array in advance.
    ByteArrayOutputStream bout = new ByteArrayOutputStream(howMany * 4);
    DataOutputStream dout = new DataOutputStream(bout);

    for (int i = 0; i <= 20; i++) {
        dout.writeInt(i);/* w  w w  .j a va 2  s.c  om*/
    }

    FileOutputStream fout = new FileOutputStream("fibonacci.dat");
    try {
        bout.writeTo(fout);
        fout.flush();
    } finally {
        fout.close();
    }
}

From source file:Main.java

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

    FileOutputStream fos = new FileOutputStream("C://text.txt");

    fos.close();//from  w w  w.j av a2s.  co m

    // try to write into underlying stream
    fos.write(65);
    fos.flush();
    fos.close();

}

From source file:Main.java

public static void main(String[] args) throws IOException {
    byte[] b = { 65, 66, 67, 68, 69 };
    int i = 0;//from  www.  j  av  a  2s . co  m
    FileOutputStream fos = new FileOutputStream("C://test.txt");

    fos.write(b);

    // flushes the content to the underlying stream
    fos.flush();

    // create new file input stream
    FileInputStream fis = new FileInputStream("C://test.txt");

    // read till the end of the file
    while ((i = fis.read()) != -1) {
        // convert integer to character
        char c = (char) i;
        System.out.print(c);
    }
}

From source file:Main.java

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

    byte b[] = { 65, 66, 67, 68, 69 };

    FileOutputStream fos = new FileOutputStream("C://test.txt");

    fos.write(b);//w  w  w  .j a  v a 2  s  .  co  m

    // pushes stream content to the underlying file
    fos.flush();

    // returns file channel associated with this stream
    FileChannel fc = fos.getChannel();

    // returns the number of bytes written
    long pos = fc.position();

    System.out.print(pos);

}

From source file:Main.java

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

    byte[] b = { 65, 66, 67, 68, 69 };
    int i = 0;//from   w  ww. j av a  2  s .c o m

    FileOutputStream fos = new FileOutputStream("C://test.txt");

    // writes byte to the output stream
    fos.write(b, 2, 3);

    // flushes the content to the underlying stream
    fos.flush();

    // create new file input stream
    FileInputStream fis = new FileInputStream("C://test.txt");

    // read till the end of the file
    while ((i = fis.read()) != -1) {
        // convert integer to character
        char c = (char) i;
        System.out.print(c);
    }
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    SecretKey key = KeyGenerator.getInstance("DES").generateKey();

    // for CBC; must be 8 bytes
    byte[] initVector = new byte[] { 0x10, 0x10, 0x01, 0x04, 0x01, 0x01, 0x01, 0x02 };

    AlgorithmParameterSpec algParamSpec = new IvParameterSpec(initVector);
    Cipher m_encrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");
    Cipher m_decrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");

    m_encrypter.init(Cipher.ENCRYPT_MODE, key, algParamSpec);
    m_decrypter.init(Cipher.DECRYPT_MODE, key, algParamSpec);

    FileInputStream fis = new FileInputStream("cipherTest.in");
    FileOutputStream fos = new FileOutputStream("cipherTest.out");
    int dataInputSize = fis.available();

    byte[] inputBytes = new byte[dataInputSize];
    fis.read(inputBytes);/*w w  w  .  j a  v  a 2  s  .  c  o m*/
    write(inputBytes, fos);
    fos.flush();
    fis.close();
    fos.close();

    String inputFileAsString = new String(inputBytes);
    System.out.println("INPUT FILE CONTENTS\n" + inputFileAsString + "\n");

    System.out.println("File encrypted and saved to disk\n");

    fis = new FileInputStream("cipherTest.out");

    byte[] decrypted = new byte[dataInputSize];
    read(decrypted, fis);

    fis.close();
    String decryptedAsString = new String(decrypted);

    System.out.println("DECRYPTED FILE:\n" + decryptedAsString + "\n");

}

From source file:Main.java

public static void main(String[] args) throws IOException {
    Deflater def = new Deflater();
    byte[] input = new byte[1024];
    byte[] output = new byte[1024];

    FileInputStream fin = new FileInputStream("a.dat");
    FileOutputStream fout = new FileOutputStream("b.dat");

    int numRead = fin.read(input);

    def.setInput(input, 0, numRead);//from   w ww  . j  a va 2s  . c  om

    while (!def.needsInput()) {
        int numCompressedBytes = def.deflate(output, 0, output.length);
        if (numCompressedBytes > 0) {
            fout.write(output, 0, numCompressedBytes);
        }
    }
    def.finish();
    fin.close();
    fout.flush();
    fout.close();
    def.reset();
}

From source file:com.moscona.dataSpace.debug.BadLocks.java

public static void main(String[] args) {
    try {/* www.j a  va2  s  .c  o m*/
        String lockFile = "C:\\Users\\Admin\\projects\\intellitrade\\tmp\\bad.lock";
        FileUtils.touch(new File(lockFile));
        FileOutputStream stream = new FileOutputStream(lockFile);
        //            FileInputStream stream = new FileInputStream(lockFile);
        FileChannel channel = stream.getChannel();
        //            FileLock lock = channel.lock(0,Long.MAX_VALUE, true);
        FileLock lock = channel.lock();
        stream.write(new UndocumentedJava().pid().getBytes());
        stream.flush();
        long start = System.currentTimeMillis();
        //            while (System.currentTimeMillis()-start < 10000) {
        //                Thread.sleep(500);
        //            }
        lock.release();
        stream.close();
        File f = new File(lockFile);
        System.out.println("Before write: " + FileUtils.readFileToString(f));
        FileUtils.writeStringToFile(f, "written after lock released");
        System.out.println("After write: " + FileUtils.readFileToString(f));
    } catch (Exception e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }

}

From source file:com.run.FtpClientExample.java

public static void main(String[] args) {

    FTPClient client = new FTPClient();
    FileOutputStream fos = null;

    try {/* w  w w. j a v a 2 s  .c o m*/
        client.connect(ARDroneConstants.IP_ADDRESS, ARDroneConstants.FTP_PORT);

        if (!client.login("anonymous", ""))
            System.err.println("login failed");

        client.setFileType(FTP.BINARY_FILE_TYPE);

        String filename = "version.txt";
        fos = new FileOutputStream(filename);

        if (!client.retrieveFile("/" + filename, fos))
            System.err.println("cannot find file");

        System.out.println("done");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fos != null) {
                fos.flush();
                fos.close();
            }
            client.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}