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 boolean saveFile(String str, File path) {
    BufferedWriter bw = null;/* w  w w  .jav  a2 s  . c  o m*/

    try {
        bw = new BufferedWriter(new FileWriter(path));
        bw.write(str);
        bw.flush();
        return true;

    } catch (IOException e) {
        e.printStackTrace();
        return false;

    } finally {
        if (bw != null) {
            try {
                bw.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }
}

From source file:Main.java

public static File WriteStringToFile(String str) {
    File tmpFile = null;/*w ww. j a  va 2  s.  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

public static boolean writeXMLFile(String outFileName, String xmlFile) {
    BufferedWriter writer;//from w  w w  . java  2 s .  co m
    try {
        writer = new BufferedWriter(new FileWriter(outFileName, false));
        writer.write(xmlFile);
        writer.close();
        return true;
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        return false;
    }
}

From source file:Main.java

/**
 * Opens a buffered writer that uses UTF-8 encoding.
 * TODO: handle compression automatically based on file extension.
 * @param path/*from   w ww. j a v  a2  s.c om*/
 * @return
 * @throws IOException
 */
public static BufferedWriter openWriter(File path) throws IOException {
    return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), "UTF-8"));
}

From source file:PipedCharacters.java

public static void writeStuff(Writer rawOut) {
    try {/*from ww w  . j  a v  a 2 s  .  com*/
        BufferedWriter out = new BufferedWriter(rawOut);

        String[][] line = { { "Java", "Source", "and", "Support." } };

        for (int i = 0; i < line.length; i++) {
            String[] word = line[i];

            for (int j = 0; j < word.length; j++) {
                out.write(word[j]);
            }

            out.newLine();
        }

        out.flush();
        out.close();
    } catch (IOException x) {
        x.printStackTrace();
    }
}

From source file:Main.java

/**
 * Opens a buffered writer that uses UTF-8 encoding.
 * TODO: handle compression automatically based on file extension.
 * @param path//from ww  w .ja v a 2 s.  co  m
 * @return
 * @throws IOException
 */
public static BufferedWriter openWriterForAppend(File path) throws IOException {
    return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path, true), "UTF-8"));
}

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());//from  w  w w .  j av a 2s  . com
    bw.close();
}

From source file:Main.java

public static boolean writeTextContentToFile(String content, String dstPath) {
    BufferedWriter writer = null;
    try {//from  w  w  w. jav  a  2s .  com
        writer = new BufferedWriter(new FileWriter(dstPath));
        writer.write(content);
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return false;
}

From source file:Main.java

public static boolean isRooted() {
    Process p;/*from w  w  w  .ja v  a  2 s.co m*/
    try {
        p = new ProcessBuilder("su").start();
        BufferedWriter stdin = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
        BufferedReader stdout = new BufferedReader(new InputStreamReader(p.getInputStream()));

        stdin.write("whoami");
        stdin.newLine();
        stdin.write("exit");
        stdin.newLine();
        stdin.close();
        try {
            p.waitFor();
            if (!stdout.ready())
                return false;
            String user = stdout.readLine(); //We only expect one line of output
            stdout.close();
            if (user == "root") {
                return true;
            } else {
                return false;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            return false;
        }
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

From source file:Main.java

/**
 * Write the provided String, line by line, in the provided OutputStream.
 *//*  www.j  av a2s .c  o  m*/
private static void writeStream(String toWrite, OutputStream out) throws IOException {
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
    BufferedReader reader = new BufferedReader(new StringReader(toWrite));
    for (String line = reader.readLine(); line != null; line = reader.readLine()) {
        writer.write(line);
    }
    reader.close();
    writer.close();
}