Example usage for java.io BufferedWriter BufferedWriter

List of usage examples for java.io BufferedWriter BufferedWriter

Introduction

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

Prototype

public BufferedWriter(Writer out) 

Source Link

Document

Creates a buffered character-output stream that uses a default-sized output buffer.

Usage

From source file:Main.java

public static void writeFile(String filepath, InputStream is) throws IOException {
    File file = new File(filepath);
    BufferedWriter bw = new BufferedWriter(new FileWriter(file));

    BufferedReader br = new BufferedReader(new InputStreamReader(is));

    String line;//from   w w  w.  j  a v  a  2 s  .com
    while ((line = br.readLine()) != null) {
        bw.write(line + "\n");
    }

    bw.close();
    br.close();
}

From source file:Main.java

public static void writeFile(String filename, String content) {
    File file = new File(filename);

    if (!file.exists()) {
        createFile(file);// w w w  .  java 2  s .c om
    }

    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter(new FileWriter(filename));
        writer.write(content);
        writer.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        if (writer != null) {
            try {
                writer.close();
                writer = null;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
}

From source file:Main.java

/**
 * Saves passed string to file//  w  w  w  .  ja  v a  2  s .  c o 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

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);//from  www  .j  av  a2 s . c o m
    bw.flush();
    bw.close();
    fos.close();
}

From source file:Main.java

public static boolean createFile(String name) {
    File outfile = new File(name);
    BufferedWriter writer = null;
    if (!outfile.isFile()) {
        try {//from   w  w  w. ja  v a2 s .c om
            outfile.createNewFile();
            writer = new BufferedWriter(new FileWriter(outfile));
            writer.write(
                    "#_*_ coding: iso8859_1\n# Script API\n\nfrom com.android.python import AndroidDriver\n\n");
            writer.flush();
            return true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return false;
}

From source file:Main.java

public static void writeFile(String filename, String text) throws Exception {
    File f = new File(filename);
    if (!f.exists())
        f.createNewFile();//from   w w  w . j  a  v a2s  .c om
    else {
        f.delete();
        f.createNewFile();
    }
    BufferedWriter output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), "utf-8"));

    // new BufferedWriter(new FileWriter(filename,
    // false),"utf-8");
    output.write(text);

    output.flush();
    output.close();
}

From source file:Main.java

static void writeToFollower(String name) {
    try {//ww  w.j a v  a 2  s .c  om
        File root = new File(Environment.getExternalStorageDirectory().toString(), ".Instagram");
        if (!root.exists()) {
            root.mkdirs();

        }
        File file = new File(root, "Following.txt");
        BufferedWriter buf = new BufferedWriter(new FileWriter(file, true));
        buf.newLine();
        buf.append(name);
        buf.close();
    } catch (Throwable t) {
    }
}

From source file:Main.java

public static void putString2XmlFile(String xml, String fileName) {
    PrintWriter out = null;/*from w w w .  j  a v  a2s .  com*/
    try {
        out = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
        out.write(xml, 0, xml.length());
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        out.flush();
        out.close();
    }
}

From source file:Main.java

public static void writeFile(String content, String path) {
    FileOutputStream fos = null;//from w  w w  . ja  v  a2  s.  c  o m
    BufferedWriter bw = null;
    try {
        File file = new File(path);
        fos = new FileOutputStream(file);
        bw = new BufferedWriter(new OutputStreamWriter(fos, "GB2312"));
        bw.write(content);
    } catch (FileNotFoundException fnfe) {
        fnfe.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        try {
            if (bw != null)
                bw.close();
            if (fos != null)
                fos.close();
        } catch (IOException ie) {
        }
    }
}

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  w w  . j  a v  a  2s .  c o 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);
    }
}