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(File file, boolean append) throws FileNotFoundException 

Source Link

Document

Creates a file output stream to write to the file represented by the specified File object.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    FileOutputStream fos = new FileOutputStream("C:/demo.txt", true);
    fos.write("Appended".getBytes());
    fos.close();/*from  w ww.java2s. c  o m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    FileOutputStream fos = new FileOutputStream("test.txt", true);
    fos.write("Appended".getBytes());
    fos.close();//w  w w .j ava  2s  . c om
}

From source file:Main.java

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

    byte b = 66;//from   ww  w. ja va  2  s  .  c  o  m
    int i = 0;
    FileOutputStream fos = new FileOutputStream("C://test.txt", true);

    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 IOException {

    byte b = 66;/*from   w w  w .j a v  a 2 s  .  com*/
    int i = 0;
    FileOutputStream fos = new FileOutputStream(new File("C://test1.txt"), true);

    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);
    }
    fos.close();
    fis.close();
}

From source file:MainClass.java

public static void main(String[] args) {

    try {/*from   www.j a  v a2  s .c o m*/
        String FILE = "c:\\systemin.txt";
        FileOutputStream outStr = new FileOutputStream(FILE, true);
        PrintStream printStream = new PrintStream(outStr);

        System.setErr(printStream);

        Timestamp now = new Timestamp(System.currentTimeMillis());
        System.out.println(now.toString() + ": This is text that should go to the file");

        outStr.close();
        printStream.close();
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
        System.exit(-1);
    } catch (IOException ex) {
        ex.printStackTrace();
        System.exit(-1);
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ByteBuffer bbuf = ByteBuffer.allocate(100);
    File file = new File("filename");

    boolean append = false;

    FileChannel wChannel = new FileOutputStream(file, append).getChannel();

    wChannel.write(bbuf);/*from   ww w. j  a v  a2 s  .  c o  m*/

    wChannel.close();
}

From source file:MainClass.java

public static void main(String[] args) {
    try {//from  w w w  . j av  a2 s.  c o  m
        File aFile = new File("test.txt");

        FileOutputStream outputFile = null;
        outputFile = new FileOutputStream(aFile, true);
        FileChannel outChannel = outputFile.getChannel();

        ByteBuffer buf = ByteBuffer.allocate(200);

        buf.putInt(10).asCharBuffer().put("www.java2s.com");

        buf.position(10).flip();
        outChannel.write(buf);
        buf.clear();

        outputFile.close();

    } catch (Exception e) {
        e.printStackTrace();

    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String phrase = new String("www.java2s.com\n");

    File aFile = new File("test.txt");
    FileOutputStream outputFile = null;
    outputFile = new FileOutputStream(aFile, true);
    System.out.println("File stream created successfully.");

    FileChannel outChannel = outputFile.getChannel();

    ByteBuffer buf = ByteBuffer.allocate(1024);
    System.out.println("New buffer:           position = " + buf.position() + "\tLimit = " + buf.limit()
            + "\tcapacity = " + buf.capacity());
    // Load the data into the buffer
    for (char ch : phrase.toCharArray()) {
        buf.putChar(ch);/* ww w.j a  va2  s  .c o m*/
    }
    System.out.println("Buffer after loading: position = " + buf.position() + "\tLimit = " + buf.limit()
            + "\tcapacity = " + buf.capacity());
    buf.flip();
    System.out.println("Buffer after flip:    position = " + buf.position() + "\tLimit = " + buf.limit()
            + "\tcapacity = " + buf.capacity());

    outChannel.write(buf);
    outputFile.close();
    System.out.println("Buffer contents written to file.");
}

From source file:MainClass.java

public static void main(String[] args) {
    File aFile = new File("afile.txt");
    FileOutputStream outputFile = null;
    try {/* ww  w  .  ja v  a2s.  com*/
        outputFile = new FileOutputStream(aFile, true);
        System.out.println("File stream created successfully.");
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    }
    ByteBuffer buf = ByteBuffer.allocate(1024);
    System.out.println("\nByte buffer:");
    System.out.printf("position = %2d  Limit = %4d  capacity = %4d%n", buf.position(), buf.limit(),
            buf.capacity());

    // Create a view buffer
    CharBuffer charBuf = buf.asCharBuffer();
    System.out.println("Char view buffer:");
    System.out.printf("position = %2d  Limit = %4d  capacity = %4d%n", charBuf.position(), charBuf.limit(),
            charBuf.capacity());
    try {
        outputFile.close(); // Close the O/P stream & the channel
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}

From source file:Logging.java

public static void main(String args[]) throws Exception {
    FileOutputStream errors = new FileOutputStream("StdErr.txt", true);
    PrintStream stderr = new PrintStream(errors);
    PrintWriter errLog = new PrintWriter(errors, true);
    System.setErr(stderr);/*from  w w w . j av a  2s .c o m*/

    String query = "SELECT Name,Description,Qty,Cost,Sell_Price FROM Stock";

    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con = DriverManager.getConnection("jdbc:odbc:Inventory");
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        while (rs.next()) {
            String name = rs.getString("Name");
            String desc = rs.getString("Description");
            int qty = rs.getInt("Qty");
            float cost = rs.getFloat("Cost");
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace(errLog);
    } catch (SQLException e) {
        System.err.println((new GregorianCalendar()).getTime());
        System.err.println("Thread: " + Thread.currentThread());
        System.err.println("ErrorCode: " + e.getErrorCode());
        System.err.println("SQLState:  " + e.getSQLState());
        System.err.println("Message:   " + e.getMessage());
        System.err.println("NextException: " + e.getNextException());
        e.printStackTrace(errLog);
        System.err.println();
    }
    stderr.close();
}