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 {

    char b = 'F';

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

    // write something in the file
    oout.writeChar(b);//from   www . j  av  a 2s .  co  m
    oout.writeChar('A');
    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 char
    System.out.println(ois.readChar());

    // read and print a char
    System.out.println(ois.readChar());
    ois.close();
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("clients.ser"));

    AccountRecordSerializable record;//from w w  w .  j  a  v a  2s . co m

    record = new AccountRecordSerializable(1, "firstName", "lastName", 0.1);
    output.writeObject(record);

    output.close();
}

From source file:Main.java

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

    byte b = 12;/*w w  w  .  j  a v a 2s . com*/

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

    // write something in the file
    oout.writeByte(b);
    oout.writeByte(21);
    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 byte
    System.out.println((char) ois.readByte());

    // read and print a byte
    System.out.println((char) ois.readByte());
    ois.close();

}

From source file:Main.java

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

    int i = 123456;

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

    // write something in the file
    oout.writeInt(i);/*from   w w w  .  ja v  a  2 s.  c o  m*/
    oout.writeInt(54321);
    oout.flush();
    oout.close();
    // create an ObjectInputStream for the file we created before
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    // read and print an int
    System.out.println(ois.readInt());

    // read and print an int
    System.out.println(ois.readInt());
    ois.close();
}

From source file:DataIODemo.java

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

    FileOutputStream fout = new FileOutputStream("Test.dat");
    DataOutputStream out = new DataOutputStream(fout);

    out.writeDouble(98.6);/*from www.  j ava  2 s . com*/
    out.writeInt(1000);
    out.writeBoolean(true);

    out.close();

    FileInputStream fin = new FileInputStream("Test.dat");
    DataInputStream in = new DataInputStream(fin);

    double d = in.readDouble();
    int i = in.readInt();
    boolean b = in.readBoolean();

    System.out.println("Here are the values:  " + d + " " + i + " " + b);

    in.close();
}

From source file:Main.java

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

    FileChannel sourceChannel = new FileInputStream("sourceFile").getChannel();
    FileChannel sinkChannel = new FileOutputStream("newFile").getChannel();

    // Copy source file contents to the sink file
    sourceChannel.transferTo(0, sourceChannel.size(), sinkChannel);
}

From source file:MainClass.java

public static void main(String[] args) {
    try {//from  w  w w .j av  a  2 s . co  m
        File tempFile = File.createTempFile("myfile", ".tmp");
        FileOutputStream fout = new FileOutputStream(tempFile);
        PrintStream out = new PrintStream(fout);
        out.println("some text");
    } catch (IOException ex) {
        System.out.println("There was a problem creating/writing to the temp file");
        ex.printStackTrace();
    }
}

From source file:Test.java

public static void main(String[] args) throws Exception {
    LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<String>();
    FileOutputStream fos = new FileOutputStream("out.log");
    DataOutputStream dos = new DataOutputStream(fos);
    while (!queue.isEmpty()) {
        dos.writeUTF(queue.take());/*from  w  w w.j a v  a 2  s. c o  m*/
    }
}

From source file:Main.java

public static void main(String[] args) {
    byte[] b = { 'h', 'e', 'l', 'l', 'o' };
    try {// w  w w. j  av a  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) 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 www .  jav  a 2s. com
    oout.writeLong(987654321L);
    oout.flush();
    oout.close();

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

    System.out.println(ois.readLine());

    ois.close();
}