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

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

    String s = "Hello World!";
    FileOutputStream out = new FileOutputStream("test.txt");
    ObjectOutputStream oout = new ObjectOutputStream(out);

    oout.writeUTF(s);/*from  ww  w  .j  a v a 2 s. c o m*/
    oout.writeUTF("This is an example from java2s.com");
    oout.flush();
    oout.close();

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

    ois.skipBytes(4);
    for (int i = 0; i < ois.available() - 4; i++) {
        System.out.print((char) ois.readByte());
    }
    ois.close();
}

From source file:Main.java

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

    double b = 123.234d;

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

    // write something in the file
    oout.writeDouble(b);//  w  w w .  j  av a 2  s .  c o  m
    oout.writeDouble(456.789d);
    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 double
    System.out.println(ois.readDouble());

    // read and print a double
    System.out.println(ois.readDouble());
    ois.close();
}

From source file:Main.java

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

    byte[] cbuf = new byte[10];

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

    oout.writeUTF("Hello World from java2s.com");
    oout.flush();/*from w ww  . j av a 2 s.  c om*/
    oout.close();
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    ois.read(cbuf, 0, 7);

    for (int i = 0; i < 7; i++) {
        System.out.print((char) cbuf[i]);
    }
    ois.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    FileOutputStream fos = new FileOutputStream("books.dat");
    ObjectOutputStream oos = new ObjectOutputStream(fos);

    Book book = new Book("1", "Java", "A");

    oos.writeObject(book);/*from   ww  w . java 2 s . c  o  m*/
    oos.flush();
    oos.close();
    FileInputStream fis = new FileInputStream("books.dat");
    ObjectInputStream ois = new ObjectInputStream(fis);
    book = (Book) ois.readObject();
    System.out.println(book.toString());

    ois.close();
}

From source file:shutdown.java

License:asdf

public static void main(String[] args) throws ClassNotFoundException, IOException {
    FileOutputStream fos = new FileOutputStream("objects.tmp");

    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject("asdf");
    oos.flush();//from   ww w  . j a v  a  2 s .com
    fos.close();

    FileInputStream fis = new FileInputStream("objects.tmp");
    ObjectInputStream ois = new ObjectInputStream(fis);
    String t = (String) ois.readObject();
    fis.close();

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    FileChannel fc = new FileOutputStream("data.txt").getChannel();
    fc.write(ByteBuffer.wrap("Some text ".getBytes()));
    fc.close();//from  ww w. ja  v  a2 s  .  c o m
}

From source file:Main.java

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

    long l = 12345678909876L;

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

    // write something in the file
    oout.writeLong(l);/*from  w ww .j av a2s. c om*/
    oout.writeLong(987654321L);
    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 long
    System.out.println(ois.readLong());

    // read and print a long
    System.out.println(ois.readLong());
    ois.close();
}

From source file:Main.java

public static void main(String[] args) {

    String s = "from java2s.com!";

    try {/* w  w w.  j av  a 2s.c o m*/

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

        FileInputStream in = new FileInputStream("test.txt");

        writer.write(s, 0, 5);

        writer.flush();

        for (int i = 0; i < 5; i++) {
            System.out.print((char) in.read());
        }
        writer.close();
        in.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("html1.pdf"));
    HtmlParser.parse(document, "example.html");
}

From source file:FileInputOutputExample.java

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

    InputStream is = new FileInputStream("in.txt");
    OutputStream os = new FileOutputStream("out.txt");
    int c;/* ww  w . j a  va2 s  .c  om*/
    while ((c = is.read()) != -1) {
        System.out.print((char) c);
        os.write(c);
    }
    is.close();
    os.close();

}