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:de.tudarmstadt.ukp.lmf.transform.sensealignments.FramenetWordnetAlignment.java

/**
 * Write output lines to given file//from w  ww .  j  a v a  2  s  .co  m
 *
 * @param outFile
 * @param lines
 * @throws IOException
 */
protected static void writeLines(String outFile, Collection<String> lines) throws IOException {
    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter(new FileWriter(new File(outFile)));
        for (String line : lines) {
            writer.write(line + "\n");
        }
    } catch (IOException e) {
        System.err.println("Exception" + e + "could not write to" + outFile);
    } finally {
        if (writer != null) {
            writer.close();
        }
    }
}

From source file:org.opentides.util.FileUtil.java

/**
 * Helper class to write specified content to certain file.
 * //  w  w w  .ja v a2  s  . co  m
 * @param filename
 * @param content
 * @return true if writing is successful
 */
public static void writeFile(String filename, String content) {
    BufferedWriter writer;
    try {
        writer = new BufferedWriter(new FileWriter(filename));
        writer.write(content);
        writer.close();
    } catch (IOException ioe) {
        String msg = "Cannot access file [" + filename + "].";
        _log.error(ioe, ioe);
        throw new InvalidImplementationException(msg, ioe);
    }
}

From source file:Main.java

public static boolean createFile(String name) {
    File outfile = new File(name);
    BufferedWriter writer = null;
    if (!outfile.isFile()) {
        try {/*from   w w w  . jav a  2  s.  co  m*/
            outfile.createNewFile();
            writer = new BufferedWriter(new FileWriter(outfile));
            writer.write(
                    "#_*_ coding: iso8859_1\n# Script API\n\nfrom com.android.python import AndroidDriver\n\n");
            writer.flush();
            return true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return false;
}

From source file:com.hp.test.framework.Utlis.java

public static void replaceMainTable(Boolean onlyMainTable, File f) {

    String content = "";
    String content1 = "";
    String replace = "";
    String imagePath = "../../../../../../../";
    if (f.getName().contentEquals("index.html")) {
        imagePath = "";
    }//  ww  w  .jav  a2s  .  com

    if (f.getName().contentEquals("ConsolidatedPage.html")) {
        imagePath = "../";
    }

    if (f.getName().contentEquals("index.html")) {
        imagePath = "";
    }

    if (f.getName().contentEquals("CurrentRun.html")) {
        imagePath = "../../";
    }

    replace = "<tr id=\"header\" >" + "\n";
    replace = replace + "<td id=\"logo\"><img src=\"" + imagePath
            + "HTML_Design_Files/IMG/Framework_Logo.jpg\" alt=\"Logo\" height=\"70\" width=\"140\" /> <br/><i style=\"float:left;padding-left:20px;font-size:12px\">Reflections of Visionary Minds</i></td>";
    replace = replace + " <td id=\"headertext\">";
    replace = replace + "Intelligent Solutions Test Framework";
    replace = replace + "<div style=\"padding-right:20px;float:right\"><img src=\"" + imagePath
            + "HTML_Design_Files/IMG/hp.png\" height=\"70\" width=\"140\" /> </i></div>                </td>";
    replace = replace + " </tr>";

    try {
        BufferedReader in = new BufferedReader(new FileReader(f));

        String str;
        Boolean found = false;
        boolean readingdone = false;
        while ((str = in.readLine()) != null) {
            //      System.out.println(""+str);
            content1 += str;
            if (!onlyMainTable) {
                if (str.contains("<td id=\"content\">")) {

                    content1 += " <img src=\"HTML_Design_Files\\\\IMG\\reports.jpg\"  alt=\"BackGround\" height=\"500\" width=\"500\" /> ";
                    in.readLine();
                    continue;
                }
            }
            if (str.contains("<table id=\"mainTable\">")) {
                found = true;

            }

            if (found) {
                while ((str = in.readLine()) != null) {

                    //   System.out.println("" + str + "\n");
                    if (str.contains("</tr>")) {
                        found = false;
                        readingdone = true;
                        content1 += replace;
                        break;

                    }
                }

            }

        }
        in.close();
        BufferedWriter out = new BufferedWriter(new FileWriter(f));
        // System.out.println("***"+content1);
        out.write(content1);
        out.close();

    } catch (IOException e) {
    }
}

From source file:com.almarsoft.GroundhogReader.lib.FSUtils.java

public static void writeStringToDiskFile(String data, String fullPath, String fileName) throws IOException {

    File outDir = new File(fullPath);

    if (!outDir.exists())
        outDir.mkdirs();/*w  w w. j a  v  a 2s  .com*/

    BufferedWriter out = null;

    try {
        FileWriter writer = new FileWriter(fullPath + fileName);
        out = new BufferedWriter(writer);
        out.write(data);
        out.flush();
    } finally {
        if (out != null)
            out.close();
    }
}

From source file:org.opentides.util.FileUtil.java

/**
 * Helper class to append specified content to certain file.
 * //from   w ww.ja  v a 2 s .c  o m
 * @param filename
 * @param content
 * @return true if writing is successful
 */
public static void appendFile(String filename, String content) {
    BufferedWriter writer;
    try {
        writer = new BufferedWriter(new FileWriter(filename, true));
        writer.write(content);
        writer.close();
    } catch (IOException ioe) {
        String msg = "Cannot access file [" + filename + "].";
        _log.error(ioe, ioe);
        throw new InvalidImplementationException(msg, ioe);
    }
}

From source file:io.jari.geenstijl.API.API.java

public static String postUrl(String url, List<NameValuePair> params, String cheader, boolean refererandorigin)
        throws IOException {
    HttpURLConnection http = (HttpURLConnection) new URL(url).openConnection();
    http.setRequestMethod("POST");
    http.setDoInput(true);/*from w  ww.ja v  a 2 s  .co m*/
    http.setDoOutput(true);
    if (cheader != null)
        http.setRequestProperty("Cookie", cheader);
    if (refererandorigin) {
        http.setRequestProperty("Referer",
                "http://www.geenstijl.nl/mt/archieven/2014/01/brein_chanteert_ondertitelaars.html");
        http.setRequestProperty("Origin", "http://www.geenstijl.nl");
    }
    OutputStream os = http.getOutputStream();
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
    writer.write(getQuery(params));
    writer.flush();
    writer.close();
    os.close();

    http.connect();

    InputStream in = http.getInputStream();
    String encoding = http.getContentEncoding();
    encoding = encoding == null ? "UTF-8" : encoding;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buf = new byte[8192];
    int len = 0;
    while ((len = in.read(buf)) != -1) {
        baos.write(buf, 0, len);
    }
    return new String(baos.toByteArray(), encoding);
}

From source file:com.hmiard.blackwater.projects.Builder.java

/**
 * Building a new project.// w  w  w. j a v a 2 s . co  m
 *
 * @param projectName String
 * @param serverNames ArrayList
 * @param location String
 */
public static void buildNewProject(String projectName, ArrayList<String> serverNames, String location,
        ConsoleEmulator consoleListener) {

    try {
        consoleListener.clean();
        consoleListener.push("Project creation started...\n\n");
        projectName = projectName.substring(0, 1).toUpperCase() + projectName.substring(1);
        String projectUrl = location + "\\" + projectName;

        buildFolder(projectUrl);
        buildFolder(projectUrl + "\\bin");
        buildFolder(projectUrl + "\\src");
        buildFolder(projectUrl + "\\.blackwater");

        ArrayList<String> realServerNames = new ArrayList<>();
        for (String name : serverNames)
            realServerNames.add(name.replace("!", ""));

        // Creating the configuration
        Properties configuration = new Properties();
        File tokens = new File(projectUrl + "\\.blackwater\\tokens.properties");
        configuration.setProperty("PATH", projectUrl);
        configuration.setProperty("NAME", projectName);
        configuration.setProperty("SERVERS", Parser.parseArrayToStringList(",", realServerNames));
        configuration.setProperty("LAST_UPDATE", new Date().toString());
        configuration.store(new FileOutputStream(tokens), "Blackwater build version : " + Start.VERSION);
        consoleListener.push("Tokens generated.\n");

        // Creating composer.json...
        JSONObject composerJSON = new JSONObject();
        JSONObject autoload = new JSONObject();
        JSONObject psr0 = new JSONObject();
        JSONObject require = new JSONObject();
        autoload.put("psr-0", psr0);
        composerJSON.put("autoload", autoload);
        composerJSON.put("require", require);

        require.put("blackwater/blackwaterp", Start.blackwaterpVersion);
        File composer = new File(projectUrl + "\\composer.json");
        if (!composer.createNewFile()) {
            consoleListener.push("\nWeird composer stuff happened... Aborting.\n");
            return;
        }

        BufferedWriter cw = new BufferedWriter(new FileWriter(composer.getAbsoluteFile()));
        String content = composerJSON.toString(4).replaceAll("\\\\", "");
        cw.write(content);
        cw.close();
        consoleListener.push("File created : composer.json\n");

        // Creating the servers...
        consoleListener.push("Server creation started...\n");
        for (String name : serverNames)
            if (name.charAt(0) == '!')
                appendServer(projectUrl, name.replace("!", ""), consoleListener, false);
            else
                appendServer(projectUrl, name, consoleListener, true);

        // Copying composer.phar
        consoleListener.push("Installing local composer wrapper...\n");
        copyFile(new File("resources/packages/composer.phar"),
                new File(composer.getParent() + "\\bin\\composer.phar"));

        // Building...
        consoleListener.push("Building dependencies...\n");
        new Thread(new ChildProcess("php bin/composer.phar install", composer.getParentFile(), consoleListener,
                () -> {
                    NewProjectAppScreenPresenter presenter = (NewProjectAppScreenPresenter) consoleListener.app;
                    presenter.projectCreatedCallback(projectUrl, consoleListener);
                })).start();

    } catch (JSONException | IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static void writeLog(String log) {
    File file = new File("/sdcard/xh.log");
    if (!file.exists()) {
        try {//from  www  .ja  v  a2 s .  com
            file.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            //            e.printStackTrace();
        }
    }
    BufferedWriter out = null;
    try {
        // Create file 
        FileWriter fstream = new FileWriter("/sdcard/xh.log", true);
        out = new BufferedWriter(fstream);
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm:ss   ");
        String time = sdf.format(new Date(System.currentTimeMillis()));
        out.append(time);
        out.append(log);
        out.append("\r\n\r\n");
    } catch (Exception e) {//Catch exception if any
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
        }
    }
}

From source file:aes.pica.touresbalon.tb_serviciosbolivariano.ServiciosBolivarianos.java

public static void EcribirFichero(File Ffichero, String SCadena) {
    try {//from  w  w w . ja v a  2 s.  c  om
        //Si no Existe el fichero lo crea
        if (!Ffichero.exists()) {
            //Ffichero.createNewFile();
        }
        /*Abre un Flujo de escritura,sobre el fichero con codificacion utf-8. 
        *Adems  en el pedazo de sentencia "FileOutputStream(Ffichero,true)",
        *true es por si existe el fichero seguir aadiendo texto y no borrar lo que tenia*/
        BufferedWriter Fescribe = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(Ffichero, true), "utf-8"));
        /*Escribe en el fichero la cadena que recibe la funcin. 
        *el string "\r\n" significa salto de linea*/
        Fescribe.write(SCadena + "\r\n");
        //Cierra el flujo de escritura
        Fescribe.close();
    } catch (Exception ex) {
        //Captura un posible error le imprime en pantalla 
        System.out.println(ex.getMessage());
    }
}