Example usage for java.io FileOutputStream FileOutputStream

List of usage examples for java.io FileOutputStream FileOutputStream

Introduction

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

Prototype

public FileOutputStream(FileDescriptor fdObj) 

Source Link

Document

Creates a file output stream to write to the specified file descriptor, which represents an existing connection to an actual file in the file system.

Usage

From source file:Main.java

License:asdf

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

    Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("outfilename"), "UTF8"));
    out.write("asdf");
    out.close();/* www  .ja v a 2 s  .com*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Formatter fmtCon = new Formatter(System.out);
    Formatter fmtFile = new Formatter(new FileOutputStream("test.fmt"));
    fmtCon.format("a negative number: %(.2f\n\n", -123.34);
    fmtCon.format("%8s %8s\n", "Value", "Square");

    for (int i = 1; i < 20; i++)
        fmtCon.format("%8d %8d\n", i, i * i);

    // write to the file.
    fmtFile.format("This is a negative number: %(.2f\n\n", -123.34);

    fmtFile.format("%8s %8s\n", "Value", "Square");
    for (int i = 1; i < 20; i++)
        fmtFile.format("%8d %8d\n", i, i * i);

    fmtFile.close();//from   w  ww.  jav a  2s .c o m

    if (fmtFile.ioException() != null) {
        System.out.println("File I/O Error Occurred");
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    FileOutputStream out = new FileOutputStream("test.txt");
    ObjectOutputStream oout = new ObjectOutputStream(out);

    // write something in the file
    oout.writeBoolean(true);/*from  www. j av a2s.c om*/
    oout.flush();
    oout.close();
    // create an ObjectInputStream for the file we created before
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    // read and print a boolean
    System.out.println(ois.readBoolean());
    ois.close();

}

From source file:Main.java

public static void main(String[] args) {
    try {/*from  w ww  .j a v  a 2s  .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:Main.java

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

    FileOutputStream out = new FileOutputStream("test.txt");
    ObjectOutputStream oout = new ObjectOutputStream(out);

    oout.writeUTF("Hello World from java2s.com");
    oout.flush();/*from w w  w. j av  a  2  s .  c  o  m*/
    oout.close();

    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    for (int i = 0; i < ois.available();) {
        System.out.print((char) ois.read());
    }
    ois.close();
}

From source file:Main.java

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

    FileOutputStream out = new FileOutputStream("test.txt");
    ObjectOutputStream oout = new ObjectOutputStream(out);

    // write something in the file
    oout.writeUTF("Hello World from java2s.com");
    oout.flush();/*from   ww  w. j  a  va 2  s  . c  om*/
    oout.close();
    // create an ObjectInputStream for the file we created before
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    // check how many bytes are available
    System.out.println(ois.available());
    ois.close();
}

From source file:Main.java

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

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

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

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

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

        // write something else
        os.write('B');

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

}

From source file:Main.java

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

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

    fos.write(65);/*from w  w  w  . j  a  v a  2 s .  c  o  m*/

    // forces byte contents to written out to the stream
    fos.flush();

    // create output streams
    FileInputStream fis = new FileInputStream("C://test.txt");

    // read byte
    int i = fis.read();

    // convert integer to characters
    char c = (char) i;

    System.out.print("Character read: " + c);
    fos.close();
    fis.close();
}

From source file:Main.java

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

    byte[] buf = { 12, 11, 22, 33, 44 };

    FileOutputStream fos = new FileOutputStream("c:/test.txt");
    DataOutputStream dos = new DataOutputStream(fos);

    int size = 0;

    for (byte b : buf) {
        dos.write(b);//from www .  j  av a 2 s .  co m
        size = dos.size();
        System.out.print("Size: " + size + "; ");
    }
}

From source file:Main.java

public static void main(String[] args) {
    String destFile = "primitives.dat";

    try (DataOutputStream dos = new DataOutputStream(new FileOutputStream(destFile))) {
        dos.writeInt(765);/*from ww w. java2  s  .c  o  m*/
        dos.writeDouble(6789.50);
        dos.writeBoolean(true);
        dos.writeUTF("Java Input/Output  is cool!");

        dos.flush();

        System.out.println("Data has  been  written to " + (new File(destFile)).getAbsolutePath());
    } catch (Exception e) {
        e.printStackTrace();
    }
}