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 toStream(final String str, final OutputStream stream, final Charset charset)
        throws IOException {
    final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stream, charset));
    try {/*w w  w  .j a  v a 2s.c o m*/
        writer.write(str);
    } finally {
        writer.flush();
        writer.close();
    }
}

From source file:Main.java

public static boolean saveFile(String str, File path) {
    BufferedWriter bw = null;

    try {//from   ww  w  .  j a  v  a2s. co  m
        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 void writeTextFile(String[] text, String filename) throws IOException {
    BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
    for (int i = 0; i < text.length; i++) {
        writer.write(text[i]);
        writer.newLine();/* w ww  . java  2 s  .c o m*/
    }
    writer.close();
}

From source file:Main.java

public static boolean writeTextContentToFile(String content, String dstPath) {
    BufferedWriter writer = null;
    try {//from   ww  w  .  j a v  a2s. c  o  m
        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 void writeListToFile(String filepath, List<String> data) {
    try {// w  w w  . j  a v  a 2s.  c  o m
        File file = new File(filepath);
        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter fileWriter = new FileWriter(file);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        for (String line : data) {
            bufferedWriter.write(line);
            bufferedWriter.newLine();
        }

        bufferedWriter.flush();
        bufferedWriter.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void writeParam(HttpURLConnection urlConnection, String param) {
    try {/*from   w w  w  . ja v  a 2  s. c om*/
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(urlConnection.getOutputStream(), "UTF-8"));
        writer.write(param);
        writer.flush();
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Write data column./*from  w ww  . j  a  v a  2 s.  c  o m*/
 * 
 * @param data the data
 * @param outputPath the output path
 */
public static void writeDataColumn(List<? extends Number> data, String outputPath) {
    File file = new File(outputPath);
    try {
        file.createNewFile();
    } catch (IOException e1) {
        e1.printStackTrace();
        System.exit(0);
    }
    try {
        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        for (Number value : data) {
            writer.write(value.toString() + "\n");
        }
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(0);
    }
}

From source file:com.devdungeon.httpexamples.DownloadFile.java

private static void download(String url, String filepath) {
    HttpClient client = new DefaultHttpClient();

    HttpParams params = client.getParams();
    HttpConnectionParams.setConnectionTimeout(params, 1000 * 5);
    HttpConnectionParams.setSoTimeout(params, 1000 * 5);

    HttpGet request = new HttpGet(url);

    HttpResponse response = null;//from  w ww.j  a  v a 2 s  . c  o m
    try {
        response = client.execute(request);
    } catch (IOException ex) {
        Logger.getLogger(HttpGetExample.class.getName()).log(Level.SEVERE, null, ex);
    }
    BufferedReader rd;
    String line = null;
    try {
        rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        FileWriter fileWriter = new FileWriter(filepath);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        while ((line = rd.readLine()) != null) {
            bufferedWriter.write(line);
            bufferedWriter.newLine();
        }
        bufferedWriter.close();

    } catch (IOException ex) {
        Logger.getLogger(HttpGetExample.class.getName()).log(Level.SEVERE, null, ex);
    } catch (UnsupportedOperationException ex) {
        Logger.getLogger(HttpGetExample.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:Main.java

/**
 * Write an object that is already base64 encoded.
 *//*ww w .  j  a v  a  2s  .c om*/
public static void writeBase64Object(BufferedWriter bw, String type, String object) throws IOException {

    bw.write("-----BEGIN ");
    bw.write(type);
    bw.write("-----");
    bw.newLine();

    bw.write(object);

    char lastChar = object.charAt(object.length() - 1);

    if (('\n' != lastChar) && ('\r' != lastChar)) {
        bw.newLine();
    }

    bw.write("-----END ");
    bw.write(type);
    bw.write("-----");
    bw.newLine();

    bw.flush();
}

From source file:Main.java

public static boolean saveStringToFile(String fileName, String saveString) {

    boolean saved = false;
    BufferedWriter bw = null;

    try {/*  w ww .jav a 2  s. co  m*/
        bw = new BufferedWriter(new FileWriter(fileName));

        try {
            bw.write(saveString);
            saved = true;
        } finally {
            bw.close();
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    return saved;
}