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:Main.java

public static void writeListToFile(String filepath, List<String> data) {
    try {//  w  ww  .  j  a  v a2s .c om
        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 writeFile(String filename, String text) throws Exception {
    File f = new File(filename);
    if (!f.exists())
        f.createNewFile();// w  ww  . ja  v a 2s .co  m
    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

public static void exportXmlFile(ArrayList<?> listObject, String rootElement, String interfaceName,
        String pathSaveFile) {/*from  w w w . java 2  s  . c  o m*/
    try {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
        // creating a new instance of a DOM to build a DOM tree.
        Document doc = docBuilder.newDocument();

        Element root = doc.createElement(rootElement);
        // adding a node after the last child node of the specified node.
        doc.appendChild(root);

        Element interfaceElement = null;

        Element child = null;
        Text text;

        for (Object obj : listObject) {
            Class srcClass = obj.getClass();
            Field[] field = srcClass.getFields();
            interfaceElement = doc.createElement(interfaceName);
            for (int i = 0; i < field.length; i++) {
                // System.out.println(field[i].getName() + ":" +
                // field[i].get(obj));
                child = doc.createElement(field[i].getName());

                text = doc.createTextNode((field[i].get(obj)).toString());
                child.appendChild(text);

                interfaceElement.appendChild(child);
            }
            root.appendChild(interfaceElement);
        }

        // TransformerFactory instance is used to create Transformer
        // objects.
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();

        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        // create string from xml tree
        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);
        String xmlString = sw.toString();

        File file = new File(pathSaveFile);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
        bw.write(xmlString);
        bw.flush();
        bw.close();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:Main.java

public static String UpdateScore(String userName, int br) {

    String retStr = "";

    try {//from   w w w. ja v  a 2s .c  o  m
        URL url = new URL("http://" + IP_ADDRESS + "/RiddleQuizApp/ServerSide/AzurirajHighScore.php");

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(15000);
        conn.setReadTimeout(10000);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);
        Log.e("http", "por1");

        JSONObject data = new JSONObject();

        data.put("username", userName);
        data.put("broj", br);

        Log.e("http", "por3");

        Uri.Builder builder = new Uri.Builder().appendQueryParameter("action", data.toString());
        String query = builder.build().getEncodedQuery();

        Log.e("http", "por4");

        OutputStream os = conn.getOutputStream();
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
        bw.write(query);
        Log.e("http", "por5");
        bw.flush();
        bw.close();
        os.close();
        int responseCode = conn.getResponseCode();

        Log.e("http", String.valueOf(responseCode));
        if (responseCode == HttpURLConnection.HTTP_OK) {
            retStr = inputStreamToString(conn.getInputStream());
        } else
            retStr = String.valueOf("Error: " + responseCode);

        Log.e("http", retStr);

    } catch (Exception e) {
        Log.e("http", "greska");
    }
    return retStr;
}

From source file:Main.java

public static String UdatePlayerBT(String userName, String device) {

    String retStr = "";

    try {/*w ww  . j  a v a  2  s.c  om*/
        URL url = new URL("http://" + IP_ADDRESS + "/RiddleQuizApp/ServerSide/PostaviBTdevice.php");

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(15000);
        conn.setReadTimeout(10000);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);

        Log.e("http", "por1");

        JSONObject data = new JSONObject();

        data.put("username", userName);
        data.put("bt_device", device);

        Log.e("http", "por3");

        Uri.Builder builder = new Uri.Builder().appendQueryParameter("action", data.toString());
        String query = builder.build().getEncodedQuery();

        Log.e("http", "por4");

        OutputStream os = conn.getOutputStream();
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
        bw.write(query);
        Log.e("http", "por5");
        bw.flush();
        bw.close();
        os.close();
        int responseCode = conn.getResponseCode();

        Log.e("http", String.valueOf(responseCode));
        if (responseCode == HttpURLConnection.HTTP_OK) {
            retStr = inputStreamToString(conn.getInputStream());
        } else
            retStr = String.valueOf("Error: " + responseCode);

        Log.e("http", retStr);

    } catch (Exception e) {
        Log.e("http", "greska");
    }
    return retStr;
}

From source file:co.foxdev.foxbot.utils.Utils.java

public static boolean addCustomCommand(String channel, String command, String text) {
    String filePath = "data/custcmds/" + channel.substring(1);
    File path = new File(filePath);

    try {//from  w  w  w. j  a va2 s .  c  o m
        if (!path.exists() && !path.mkdirs()) {
            foxbot.getLogger().warn("Error occurred while creating custom command folders!");
        }

        File file = new File(filePath + "/" + command);

        if (file.exists() && !file.delete()) {
            foxbot.getLogger().warn(
                    String.format("Error occurred while deleting command '%s' for %s!", command, channel));
        }

        if (text.isEmpty() || text.equalsIgnoreCase("delete")) {
            if (file.delete()) {
                foxbot.getLogger().warn(String.format("Command '%s' deleted for %s!", command, channel));
            }
            return false;
        }

        FileWriter fw = new FileWriter(filePath + "/" + command);
        BufferedWriter bw = new BufferedWriter(fw);

        bw.write(text);
        bw.flush();
        bw.close();
        fw.flush();
        fw.close();
        foxbot.getLogger()
                .warn(String.format("Command '%s' set for %s at %s", command, channel, file.getAbsolutePath()));
    } catch (IOException ex) {
        foxbot.getLogger().error("Error occurred while adding custom command", ex);
    }
    return true;
}

From source file:net.idlesoft.android.apps.github.utils.GravatarCache.java

/**
 * Returns a Gravatar ID associated with the provided name
 * //from  w  ww . j  ava 2s.  c om
 * @param name
 * @return the gravatar ID associated with the name
 */
public static String getGravatarID(final String name) {
    String id = "";
    try {
        final File gravatars = ensure_directory(ROOT_DIR);
        final File gravatar_id = new File(gravatars, name + ".id");

        if (gravatar_id.isFile()) {
            final FileReader fr = new FileReader(gravatar_id);
            final BufferedReader in = new BufferedReader(fr);
            id = in.readLine();
            in.close();
        } else {
            id = getGravatarIdFromGithub(name);
            if (!id.equals("")) {
                final FileWriter fw = new FileWriter(gravatar_id);
                final BufferedWriter bw = new BufferedWriter(fw);
                bw.write(id);
                bw.flush();
                bw.close();
            }
        }
    } catch (final IOException e) {
        e.printStackTrace();
    }
    return id;
}

From source file:org.esupportail.twitter.services.OAuthTwitterApplicationOnlyService.java

private static boolean writeRequest(HttpURLConnection connection, String textBody) {
    try {/*from w  w  w  .j  av a2  s . com*/
        BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
        wr.write(textBody);
        wr.flush();
        wr.close();

        return true;
    } catch (IOException e) {
        return false;
    }
}

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

public static String sendPostRequest(String urlString, String payload)
        throws UnsupportedEncodingException, IOException, JSONException {
    final URL url = new URL(urlString);
    final URLConnection conn = url.openConnection();

    conn.setDoOutput(true);// w  w w  .j a  va  2s. c om
    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);
    }

    return sb.toString();
}

From source file:com.thinkit.operationsys.util.FileUtil.java

public static void copyFile(String src, String dest) {
    System.out.println("copy");
    File f1 = new File(src);
    File f2 = new File(dest);
    //int b=0;/* w  w  w  . j a  v a2 s . co  m*/
    String line = "";
    try {
        FileReader reader = new FileReader(f1);
        FileWriter writer = new FileWriter(f2);
        BufferedReader br = new BufferedReader(reader);
        BufferedWriter bw = new BufferedWriter(writer);
        while ((line = br.readLine()) != null) {
            System.out.println(line);
            bw.write(line);
            bw.newLine();
            bw.flush();
        }
        reader.close();
        writer.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}