Example usage for java.io BufferedWriter write

List of usage examples for java.io BufferedWriter write

Introduction

In this page you can find the example usage for java.io BufferedWriter write.

Prototype

public void write(int c) throws IOException 

Source Link

Document

Writes a single character.

Usage

From source file:Main.java

public static void writeToFile(Context context, String file, String content) throws IOException {
    FileOutputStream fos = context.openFileOutput(file, Context.MODE_PRIVATE);
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
    bw.write(content);
    bw.flush();/*from  ww w. jav a 2  s .  c  o  m*/
    bw.close();
    fos.close();
}

From source file:Main.java

public static void generateFile(String str) throws Exception {
    File target = new File(System.getProperty("user.dir") + "/src/sql1.txt");
    FileOutputStream fos = new FileOutputStream(target);
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"));
    bw.write(str.toString());
    bw.close();/*from   w  ww.j  a v a 2s .c  o m*/
}

From source file:Main.java

public static void writeNewFile(String filePath, String fileContents) {
    File f = new File(filePath);
    if (f.exists()) {
        f.delete();/*  ww  w .j  av a2  s .  co  m*/
    }

    try {
        // Create file 
        FileWriter fstream = new FileWriter(f);
        BufferedWriter out = new BufferedWriter(fstream);
        out.write(fileContents);
        //Close the output stream
        out.close();
    } catch (Exception e) {
        Log.d(TAG, "Failed to create " + filePath + " File contents: " + fileContents);
    }
}

From source file:Main.java

public static void writeNewFile(String filePath, String fileContents) {
    File f = new File(filePath);
    if (f.exists()) {
        f.delete();//from   w ww  .  ja  va2 s  .co  m
    }

    try {
        // Create file
        FileWriter fstream = new FileWriter(f);
        BufferedWriter out = new BufferedWriter(fstream);
        out.write(fileContents);
        //Close the output stream
        out.close();
    } catch (Exception e) {
        Log.d(TAG, "Failed to create " + filePath + " File contents: " + fileContents);
    }
}

From source file:Main.java

/**
 * Write to file in given folder//from  w w  w .  ja  va  2s  . co  m
 * @param fcontent
 * @return
 */
public static boolean writeFile(String fcontent, String path) {

    /*
     * Write file contents to file path
     */
    try {
        File file = new File(path);
        // If file does not exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        }
        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(fcontent);
        bw.close();
        return true;
    } catch (Exception e) {
        return false;
    }
}

From source file:Main.java

public static File WriteStringToFile(String str) {
    File tmpFile = null;/*from  w  ww .  j  av  a  2s. c o m*/
    try {
        tmpFile = File.createTempFile("z4-", ".tmp");
        BufferedWriter out = new BufferedWriter(new FileWriter(tmpFile));
        out.write(str);
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return tmpFile;
}

From source file:Main.java

/**
 * Saves passed string to file/*  w  w w .j  a v a 2  s  .  co  m*/
 *
 * @param filename      path to file
 * @param stringToWrite string to save
 */
public static void stringToFile(String filename, String stringToWrite) {
    try {
        BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
        writer.write(stringToWrite);
        writer.flush();
        writer.close();
    } catch (Exception ignored) {
    }
}

From source file:Main.java

/**
 * Write in a text file//from w  w  w.  ja  v  a 2s  .c  om
 * 
 * @param file
 *            in which we write
 * 
 * @param text
 *            that we write
 * 
 * @param append
 *            indicates whether or not to append to an existing file
 * 
 * @throws IOException
 *             Input/Output exceptions
 */
public static void writeTextFile(File file, String text, boolean append) throws IOException {
    BufferedWriter writer = new BufferedWriter(new FileWriter(file, append));
    writer.write(text);
    writer.close();
}

From source file:Main.java

public static String writeXMLToFile(String resString, String fileName) {
    try {/*from w  ww .jav a2  s  .c o  m*/
        BufferedWriter writer = new BufferedWriter(new FileWriter(new File(fileName)));
        writer.write(resString);
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    return fileName;
}

From source file:Main.java

public static void writeToFile(final String str, final File file) {
    try {/*  w  ww  .j a va2s . co  m*/
        final BufferedWriter output = new BufferedWriter(new FileWriter(file), 8192);
        output.write(str);
        output.close();
    } catch (final IOException exception) {
        exception.printStackTrace();
    }
}