Example usage for java.io BufferedWriter flush

List of usage examples for java.io BufferedWriter flush

Introduction

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

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes the stream.

Usage

From source file:es.sotileza.plugin.utils.Utilidades.java

public static void addXml(String xml, List<String> lineas) throws IOException {
    FileWriter output = new FileWriter(xml, true);
    BufferedWriter writer = new BufferedWriter(output);
    for (String i : lineas)
        writer.write(i + "\n");
    writer.flush();
}

From source file:com.zimbra.cs.session.WaitSetValidator.java

private static void printError(String text) {
    PrintStream ps = System.err;
    try {/*from w ww.  j a va 2s  . com*/
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(ps, "UTF-8"));
        writer.write(text + "\n");
        writer.flush();
    } catch (UnsupportedEncodingException e) {
        ps.println(text);
    } catch (IOException e) {
        ps.println(text);
    }
}

From source file:com.linkedin.pinot.tools.admin.command.AbstractBaseAdminCommand.java

public static JSONObject postQuery(String query, String brokerBaseApiUrl) throws Exception {
    final JSONObject json = new JSONObject();
    json.put("pql", query);

    final long start = System.currentTimeMillis();
    final URLConnection conn = new URL(brokerBaseApiUrl + "/query").openConnection();
    conn.setDoOutput(true);/*from w  ww .j a  v a  2 s. c  o  m*/
    final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));
    final String reqStr = json.toString();
    System.out.println("reqStr = " + reqStr);

    writer.write(reqStr, 0, reqStr.length());
    writer.flush();
    final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));

    final StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line);
    }

    final long stop = System.currentTimeMillis();

    final String res = sb.toString();
    System.out.println("res = " + res);
    final JSONObject ret = new JSONObject(res);
    ret.put("totalTime", (stop - start));

    return ret;
}

From source file:PipedCharacters.java

public static void writeStuff(Writer rawOut) {
    try {/*from  w  ww. ja v a 2 s  .  co m*/
        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:it.geosolutions.tools.io.file.writer.Writer.java

/**
 * Open 'destination' file in append mode and append content of the
 * 'toAppend' file//  w  w  w  .j a  va2 s.co m
 * 
 * @param toAppend
 * @param destination
 * @throws IOException
 */
public static void appendFile(File toAppend, File destination) throws IOException {
    FileWriter fw = null;
    BufferedWriter bw = null;
    LineIterator it = null;
    try {
        fw = new FileWriter(destination, true);
        bw = new BufferedWriter(fw);
        it = FileUtils.lineIterator(toAppend);
        while (it.hasNext()) {
            bw.append(it.nextLine());
            bw.newLine();
        }
        bw.flush();
    } finally {
        if (it != null) {
            it.close();
        }
        if (bw != null) {
            IOUtils.closeQuietly(bw);
        }
        if (fw != null) {
            IOUtils.closeQuietly(fw);
        }
    }
}

From source file:Main.java

public static void getStringFromXML(Node node, String dtdFilename, String outputFileName)
        throws TransformerException, IOException {
    File file = new File(outputFileName);
    if (!file.isFile())
        file.createNewFile();//from  w w w  . j  a  va 2 s  .c o m
    BufferedWriter out = new BufferedWriter(new FileWriter(file));

    String workingPath = System.setProperty("user.dir", file.getAbsoluteFile().getParent());
    try {
        getStringFromXML(node, dtdFilename, out);
    } finally {
        System.setProperty("user.dir", workingPath);
    }

    out.flush();
    out.close();
}

From source file:common.Utilities.java

public static boolean writeStringToFile(String filename, String stringToWrite) {
    try {//from   w w w  .j  av  a 2 s.  c  o m
        FileWriter writer = new FileWriter(new File(filename));
        BufferedWriter bw = new BufferedWriter(writer);
        bw.write(stringToWrite);
        bw.flush();
        bw.close();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:com.germinus.easyconf.FileUtil.java

public static void write(File file, String s) throws IOException {
    if (file.getParent() != null) {
        mkdirs(file.getParent());//w  w  w .ja  v a  2s. com
    }

    BufferedWriter bw = new BufferedWriter(new FileWriter(file));

    bw.flush();
    bw.write(s);
    bw.flush();

    bw.close();
}

From source file:com.example.makerecg.NetworkUtilities.java

/**
 * Connects to the SampleSync test server, authenticates the provided
 * username and password./*  w ww .ja  v  a 2  s.  co m*/
 * This is basically a standard OAuth2 password grant interaction.
 *
 * @param username The server account username
 * @param password The server account password
 * @return String The authentication token returned by the server (or null)
 */
public static String authenticate(String username, String password) {
    String token = null;

    try {

        Log.i(TAG, "Authenticating to: " + AUTH_URI);
        URL urlToRequest = new URL(AUTH_URI);
        HttpURLConnection conn = (HttpURLConnection) urlToRequest.openConnection();
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("grant_type", "password"));
        params.add(new BasicNameValuePair("client_id", "CLIENT_ID"));
        params.add(new BasicNameValuePair("username", username));
        params.add(new BasicNameValuePair("password", password));

        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
        writer.write(getQuery(params));
        writer.flush();
        writer.close();
        os.close();

        int responseCode = conn.getResponseCode();

        if (responseCode == HttpsURLConnection.HTTP_OK) {
            String response = "";
            String line;
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while ((line = br.readLine()) != null) {
                response += line;
            }
            br.close();

            // Response body will look something like this:
            // {
            //    "token_type": "bearer",
            //    "access_token": "0dd18fd38e84fb40e9e34b1f82f65f333225160a",
            //    "expires_in": 3600
            //  }

            JSONObject jresp = new JSONObject(new JSONTokener(response));

            token = jresp.getString("access_token");
        } else {
            Log.e(TAG, "Error authenticating");
            token = null;
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        Log.v(TAG, "getAuthtoken completing");
    }

    return token;

}

From source file:com.linkedin.pinot.controller.helix.ControllerTest.java

public static String sendPutRequest(String urlString, String payload) throws IOException {
    LOGGER.info("Sending PUT to " + urlString + " with payload " + payload);
    final long start = System.currentTimeMillis();
    final URL url = new URL(urlString);
    final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setDoOutput(true);/*from  w  ww . jav  a  2 s .  co  m*/
    conn.setRequestMethod("PUT");
    final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));

    writer.write(payload, 0, payload.length());
    writer.flush();

    final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
    final StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line);
    }

    final long stop = System.currentTimeMillis();

    LOGGER.info(" Time take for Request : " + urlString + " in ms:" + (stop - start));

    return sb.toString();
}