Example usage for java.io BufferedWriter close

List of usage examples for java.io BufferedWriter close

Introduction

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

Prototype

@SuppressWarnings("try")
    public void close() throws IOException 

Source Link

Usage

From source file:Main.java

public static void appendLog(String text) {
    Log.d("LOGFILE", text);
    SimpleDateFormat sTime = new SimpleDateFormat("dd/MMM/yyyy - hh:mm:ss");
    String timeFormat = sTime.format(new Date());

    File sdCard = Environment.getExternalStorageDirectory();
    File dir = new File(sdCard.getAbsolutePath() + "/Cura/Logs/");
    dir.mkdirs();/*from  ww  w.j  a v a 2  s.  c o m*/
    File logFile = new File(dir, "Cura_Logs_DEBUG.txt");
    if (!logFile.exists()) {
        try {
            logFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    try {
        BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
        if (text.compareTo("wipe") == 0)
            logFile.delete();
        else {
            buf.append("[" + timeFormat + "] - ");
            buf.append(text);
            buf.newLine();
            buf.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:eu.cloud4soa.c4sgitservice.utils.Util.java

public static synchronized void appendToFile(String filename, String text) {
    FileWriter fWriter = null;//from  w  w  w  . jav a 2  s . c  om
    BufferedWriter writer = null;
    try {
        fWriter = new FileWriter(filename, true);
        writer = new BufferedWriter(fWriter);
        writer.append(text);
        writer.newLine();
        writer.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:commonUtils.FunctionUtils.java

public static boolean WriteTextFile(String pathFileText, String fileContent) {
    try {/*from   ww  w.  j av a 2  s .  com*/
        File file = new File(pathFileText);

        //if file doesnt exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        }

        //Collecting Data
        String fileDat = "";
        if (!IsNullOrEmpty(fileContent)) {
            String[] listContent = StringUtils.split(fileContent, "\n");
            fileDat += listContent.length + "\n";

            for (String tmpPost : listContent) {
                if (tmpPost != null) {
                    fileDat += tmpPost + "\n";
                }
            }
            //true = append file
            FileWriter fileWritter = new FileWriter(file.getPath(), false);
            BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
            bufferWritter.write(fileDat);
            bufferWritter.close();

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

From source file:com.alibaba.wasp.ZNodeClearer.java

/**
 * Logs the errors without failing on exception.
 */// w ww  .j av  a  2  s  .c o  m
public static void writeMyEphemeralNodeOnDisk(String fileContent) {
    String fileName = ZNodeClearer.getMyEphemeralNodeFileName();

    if (fileName == null) {
        LOG.warn("No filename given to save the znode used, it won't be saved "
                + "(Environment variable WASP_ZNODE_FILE is not set).");
        return;
    }

    FileWriter fstream;
    try {
        fstream = new FileWriter(fileName);
    } catch (IOException e) {
        LOG.warn("Can't write znode file " + fileName, e);
        return;
    }

    BufferedWriter out = new BufferedWriter(fstream);

    try {
        try {
            out.write(fileContent + "\n");
        } finally {
            try {
                out.close();
            } finally {
                fstream.close();
            }
        }
    } catch (IOException e) {
        LOG.warn("Can't write znode file " + fileName, e);
    }
}

From source file:com.wso2telco.services.bw.FileUtil.java

public static void fileWrite(String filePath, String data) throws IOException {
    BufferedWriter out = null;
    try {/*from   w w w . j  av  a2 s.  c o  m*/
        out = new BufferedWriter(new FileWriter(filePath));
        out.write(data);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        out.close();
    }

}

From source file:jmc.util.UtlFbComents.java

public static void getURLComentarios(String url, Long numComents) throws JMCException {

    File f = null;/*www.  ja va  2s  . com*/

    try {
        Properties props = ConfigPropiedades.getProperties("props_config.properties");
        CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
        URL ur = new URL(
                "http://graph.facebook.com/comments?id=" + url + "&limit=" + numComents + "&filter=stream");
        HttpURLConnection cn = (HttpURLConnection) ur.openConnection();

        cn.setRequestProperty("user-agent", props.getProperty("navegador"));
        cn.setInstanceFollowRedirects(false);
        cn.setUseCaches(false);
        cn.connect();

        BufferedReader br = new BufferedReader(new InputStreamReader(cn.getInputStream()));
        String dirTempHtml = props.getProperty("archive_json") + "/";
        File fld = new File(dirTempHtml);

        boolean creado = fld.exists() ? true : fld.mkdir();

        if (creado) {
            String archFile = "archivo.json";
            String archivo = dirTempHtml + archFile;
            String linea = null;

            f = new File(archivo);
            BufferedWriter bw = new BufferedWriter(new FileWriter(f));
            while ((linea = br.readLine()) != null) {
                bw.append(linea);
            }
            bw.close();

            cn.disconnect();

            List<Fbcoment> lfb = Convertidor.getFbcomentObjects(1l, archivo);
            // Fbcoment.actComents(idContenido, lfb);

        } else {
            cn.disconnect();
        }

    } catch (IOException e) {
        throw new JMCException(e);
    }
}

From source file:Main.java

public static boolean isRooted() {
    Process p;/*  ww  w  .  j a  v a2s . c  om*/
    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:jmc.util.UtlFbComents.java

public static void getComentarios(Long idContenido, Long numComents) throws JMCException {

    File f = null;//  w  w  w . j a va  2s.  c o m

    try {
        Properties props = ConfigPropiedades.getProperties("props_config.properties");
        CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
        URL ur = new URL("http://graph.facebook.com/comments?id="
                + Contenido.getContenido("Where cont_id = '" + idContenido + "'").get(0).getEnlaceURL()
                + "&limit=" + numComents + "&filter=stream");
        HttpURLConnection cn = (HttpURLConnection) ur.openConnection();

        cn.setRequestProperty("user-agent", props.getProperty("navegador"));
        cn.setInstanceFollowRedirects(false);
        cn.setUseCaches(false);
        cn.connect();

        BufferedReader br = new BufferedReader(new InputStreamReader(cn.getInputStream()));
        String dirTempHtml = props.getProperty("archive_json") + "/";
        File fld = new File(dirTempHtml);

        boolean creado = fld.exists() ? true : fld.mkdir();

        if (creado) {
            String archFile = idContenido + ".json";
            String archivo = dirTempHtml + archFile;
            String linea = null;

            f = new File(archivo);
            BufferedWriter bw = new BufferedWriter(new FileWriter(f));
            while ((linea = br.readLine()) != null) {
                bw.append(linea);
            }
            bw.close();

            cn.disconnect();

            List<Fbcoment> lfb = Convertidor.getFbcomentObjects(idContenido, archivo);
            Fbcoment.actComents(idContenido, lfb);

        } else {
            cn.disconnect();
        }

    } catch (IOException e) {
        throw new JMCException(e);
    }
}

From source file:com.slidespeech.server.service.TextToSpeechService.java

private static String createXML4Cereproc(String fileName, String speakernotes) throws IOException {
    List<String> voices = new ArrayList<String>();

    try {// w  ww .  j  ava2s.  com
        Document doc = Jsoup.parse(speakernotes, "");
        doc.outputSettings().prettyPrint(false);
        Elements voiceNodes = doc.select("voice");

        for (Element voiceNode : voiceNodes) {
            String lang = (voiceNode.hasAttr("xml:lang") && !voiceNode.attr("xml:lang").equals(""))
                    ? voiceNode.attr("xml:lang")
                    : "en";
            String gender = (voiceNode.hasAttr("gender") && !voiceNode.attr("gender").equals(""))
                    ? voiceNode.attr("gender")
                    : "female";
            String voiceName = (voiceNode.hasAttr("name") && !voiceNode.attr("name").equals(""))
                    ? voiceNode.attr("name")
                    : "";

            //voice name not set by user -> choose one depending on language and gender
            if (voiceName.equals("")) {
                voiceName = "isabella";//default
                //if(lang.equalsIgnoreCase("en") && gender.equalsIgnoreCase("female")) voiceName = "isabella";
                if (lang.equalsIgnoreCase("en") && gender.equalsIgnoreCase("male"))
                    voiceName = "william";
                if (lang.equalsIgnoreCase("de"))
                    voiceName = "alex";

                voiceNode.attr("name", voiceName);

            }
            if (!voices.contains(voiceName)) {
                voices.add(voiceName);

            }
        }

        BufferedWriter out = new BufferedWriter(new FileWriter(fileName));
        out.write(doc.select("body").first().html());
        //out.write(doc.select("body").first().html());
        out.close();

        for (int i = 0; i < voices.size(); i++) {
            if (voices.get(i).equals("william"))
                voices.set(i, "/opt/cereproc/cerevoice_william_3.0.5_22k.voice");
            if (voices.get(i).equals("isabella"))
                voices.set(i, "/opt/cereproc/cerevoice_isabella_3.0.3_22k.voice");
            if (voices.get(i).equals("alex"))
                voices.set(i, "/opt/cereproc/cerevoice_alex_3.0.0_beta_22k.voice");
        }
    } catch (Exception e) {
        //Fallback if ssml parsing fails
        Writer out = new OutputStreamWriter(new FileOutputStream(fileName));
        try {
            out.write(speakernotes);
        } finally {
            out.close();
        }
        voices.add("ssml parsing failed");
    }

    return StringUtils.join(voices, ",");
}

From source file:com.hp.test.framework.htmparse.UpdateTestCaseDesciption.java

public static void replaceDetailsTable(String path) throws IOException {

    File source = new File(path);
    Document report = null;//from  w w  w  .jav  a2s  .c  om
    try {
        report = Jsoup.parse(source, "UTF-8");
    } catch (IOException e) {
        System.out.println("Unable to open [" + source.getAbsolutePath() + "] for parsing!");
    }
    Elements dom = report.children();
    Elements tds = report.select("table[id=tableStyle] td"); // select the tds from your table
    String temp_key = "";
    for (Element td : tds) { // loop through them

        String[] temp_ar = td.toString().split("\"");
        String Key = temp_ar[1];
        String Status = "";

        if (td.toString().contains("pass.png")) {
            Status = "pass";
        }
        if (td.toString().contains("fail.png")) {
            Status = "fail";
        }
        if (td.toString().contains("skip.png")) {
            Status = "skip";
        }

        if (TestCaseDesMap.containsKey(temp_key) && Status.length() > 1) {
            TestcaseStatusMap.put(temp_key, Status);
            temp_key = "";
        }

        if (td.text().contains("Test Method")) { // found the one you want
            String TestcaseDes;
            if (!TestCaseDesMap.containsKey(Key)) {
                TestcaseDes = "  ---------       ";
                TestCaseDesMap.put(Key, TestcaseDes);
                temp_key = Key;

            } else {
                TestcaseDes = TestCaseDesMap.get(Key);
                temp_key = Key;
                // TestcaseStatusMap.put(Key, Status);
            }
            td.text(TestcaseDes);
            // Replace with your text
        }
    }

    Elements ths = report.select("table[id=tableStyle] th"); // select the tds from your table
    for (Element th : ths) { // loop through them

        if (th.text().contains("Method Type")) { // found the one you want
            th.text("TestCase Description");

        }
        if (th.text().contains("Test Case Name")) { // found the one you want
            th.text("Testng Method");

        }
    }

    if (!source.canWrite()) {
        System.out.println("Can't write this file!");//Just check if the file is writable or not
    }
    BufferedWriter bw = new BufferedWriter(new FileWriter(source));
    bw.write(dom.toString()); //toString will give all the elements as a big string
    bw.close(); //Close to apply the changes
    //  genarateFailureReport(new File("C:\\Users\\yanamalp\\Desktop\\Gen_jelly\\HTML_Design_Files\\CSS\\HtmlReport.html"), "c:\\");

}