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 {

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

    // writes buffer to the output stream
    fos.write(65);//from  ww w. jav  a2s . c  o  m

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

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

    // get byte from the file
    int i = fis.read();

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

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

From source file:Copy.java

public static void main(String[] args) throws Exception {
    FileInputStream fis = null;/*from  ww  w . jav a 2  s.  co  m*/
    FileOutputStream fos = null;
    fis = new FileInputStream(args[0]);
    fos = new FileOutputStream(args[1]);

    int byte_;
    while ((byte_ = fis.read()) != -1)
        fos.write(byte_);
}

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();/* www. j  a va  2s  .c om*/

    // create an ObjectInputStream for the file we created before
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    // read from the stream
    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 IOException {

    byte b = 66;/* ww w  .j  ava  2 s .  co  m*/
    int i = 0;
    FileOutputStream fos = new FileOutputStream("C://test.txt");

    fos.write(b);

    fos.flush();

    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 Exception {
    Properties p = new Properties();
    p.load(new FileInputStream("test.txt"));

    p.store(new FileOutputStream("t.txt"), "no comments");
}

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);//from  w  w w  .jav a  2  s .  c om

    // 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:MainClass.java

public static void main(String[] args) throws Exception {
    FileChannel in = new FileInputStream("source.txt").getChannel(),
            out = new FileOutputStream("target.txt").getChannel();
    in.transferTo(0, in.size(), out);/*w  ww. ja v  a  2  s.c  o  m*/
    // Or:
    // out.transferFrom(in, 0, in.size());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Formatter fmtCon = new Formatter(System.out);
    Formatter fmtFile;//w w  w . j  av a 2  s . co m
    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();

    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 {

    byte b = 127;

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

    // write something in the file
    oout.writeByte(b);/*  w  ww  .  j  a va 2 s .  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 the unshared object
    System.out.println(ois.readUnsignedByte());
    ois.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  a v  a2 s .c o 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);
    }
}