Java Write String to File writeFile(String filePath, String content)

Here you can find the source of writeFile(String filePath, String content)

Description

Write content to file.

License

Open Source License

Parameter

Parameter Description
filePath a parameter
content a parameter

Declaration

public static void writeFile(String filePath, String content) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2006 Bruno G. Braga. All rights reserved. This program and the
 * accompanying materials are made available under the terms of the Eclipse
 * Public License v1.0 which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * /* w  w w . j  a  va  2 s .c o m*/
 * Contributors: Bruno G. Braga - initial API and implementation
 *******************************************************************************/

import java.io.FileDescriptor;

import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    /**
     * Write content to file.
     * @param filePath
     * @param content
     */
    public static void writeFile(String filePath, String content) {
        try {
            // Open or create the output file
            FileOutputStream os = new FileOutputStream(filePath);
            FileDescriptor fd = os.getFD();

            // Write some data to the stream
            os.write(content.getBytes());

            // Flush the data from the streams and writers into system buffers.
            // The data may or may not be written to disk.
            os.flush();

            // Block until the system buffers have been written to disk.
            // After this method returns, the data is guaranteed to have
            // been written to disk.
            fd.sync();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Related

  1. writeFile(String filename, StringBuilder sb)
  2. writeFile(String filePath, String content)
  3. writeFile(String filePath, String content)
  4. WriteFile(String filepath, String content)
  5. writeFile(String filePath, String content)
  6. writeFile(String filePath, String content, boolean append)
  7. writeFile(String filePath, String contents)
  8. writeFile(String filePath, String fileContent)
  9. writeFile(String filePath, String fileData, boolean isAppend)