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:com.hmiard.blackwater.projects.Builder.java

/**
 * Appending a new server to a project.//from  www  .  ja v a  2 s  . c  om
 *
 * @param projectRoot String
 * @param serverName String
 * @param consoleListener ConsoleEmulator
 * @param needsDb Boolean
 */
public static Boolean appendServer(String projectRoot, String serverName, ConsoleEmulator consoleListener,
        Boolean needsDb) {

    try {
        serverName = serverName.substring(0, 1).toUpperCase() + serverName.substring(1).toLowerCase();
        String shortServerName = serverName;
        serverName += "Server";
        String src = projectRoot + "\\src\\" + serverName;
        File checker = new File(projectRoot + "\\src\\" + serverName);
        File composerJSON = new File(projectRoot + "\\composer.json");

        if (checker.exists() && checker.isDirectory()) {
            consoleListener.push("This server already exists ! Operation aborted.");
            return false;
        }
        if (!composerJSON.exists()) {
            consoleListener.push("File composer.json is missing ! Operation aborted.");
            return false;
        }

        if (needsDb)
            copyFolder(new File("resources/packages/DefaultApp"), new File(src));
        else
            copyFolder(new File("resources/packages/NoDbApp"), new File(src));

        FileOutputStream writer;
        File core = new File(src + "\\BlackwaterDefaultApp.php");
        File qf = new File(src + "\\DefaultAppQueryFactory.php");
        File bootstrap = new File(src + "\\bin\\init.php");

        String coreContent = readFile(core.getAbsolutePath());
        coreContent = coreContent.replace("BlackwaterDefaultApp", serverName);
        File newCore = new File(src + "\\" + serverName + ".php");
        if (newCore.createNewFile() && core.delete()) {
            writer = new FileOutputStream(newCore);
            writer.write(coreContent.getBytes());
            writer.flush();
            writer.close();
        }

        if (needsDb) {
            String qfContent = readFile(qf.getAbsolutePath());
            qfContent = qfContent.replace("BlackwaterDefaultApp", serverName);
            qfContent = qfContent.replace("DefaultApp", shortServerName);
            File newQf = new File(src + "\\" + shortServerName + "QueryFactory.php");
            if (newQf.createNewFile() && qf.delete()) {
                writer = new FileOutputStream(newQf);
                writer.write(qfContent.getBytes());
                writer.flush();
                writer.close();
            }
        }

        String bootsrapContent = readFile(bootstrap.getAbsolutePath());
        Random r = new Random();
        bootsrapContent = bootsrapContent.replace("Default", shortServerName);
        bootsrapContent = bootsrapContent.replace("8080", String.valueOf(r.nextInt(2000) + 7000));
        writer = new FileOutputStream(bootstrap);
        writer.write(bootsrapContent.getBytes());
        writer.flush();
        writer.close();

        JSONObject composer = new JSONObject(readFile(composerJSON.getAbsolutePath()));
        JSONObject autoload = composer.getJSONObject("autoload");
        JSONObject psr0 = autoload.getJSONObject("psr-0");
        psr0.put(serverName, "src");

        BufferedWriter cw = new BufferedWriter(new FileWriter(composerJSON.getAbsoluteFile()));
        String content = composer.toString(4).replaceAll("\\\\", "");
        cw.write(content);
        cw.close();

        consoleListener.push(serverName + " created !\n");

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

    return true;
}

From source file:com.thejustdo.util.Utils.java

/**
 * Creates a new temporal file with the content given in the string.
 * @param content What to write son of a bitch.
 *///from  ww w.  ja va 2s  . c om
public static void createTMPFile(String content, File goal) throws IOException {
    // 1. Creating the writers.
    FileOutputStream fos = new FileOutputStream(goal);
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, "ISO-8859-1"));

    // 2. Writing contents.
    writer.write(content);

    // 3. Closing stream.
    writer.flush();
    writer.close();
    fos.close();
}

From source file:at.riemers.velocity2js.velocity.Velocity2Js.java

public static void generateDir(String templateDir, String javascriptDir, List<I18NBundle> bundles)
        throws Exception {

    Properties p = new Properties();
    p.setProperty("resource.loader", "file");

    p.setProperty("file.resource.loader.description", "Velocity File Resource Loader");
    p.setProperty("file.resource.loader.class",
            "org.apache.velocity.runtime.resource.loader.FileResourceLoader");

    p.setProperty("file.resource.loader.path", templateDir);
    Velocity2Js.init(p);//ww w.  ja v a 2  s .co m

    File tDir = new File(templateDir);

    for (I18NBundle bundle : bundles) {
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(javascriptDir + "/vel2js" + bundle.getLocale() + ".js"), "UTF8"));

        //BufferedWriter out = new BufferedWriter(new FileWriter(new File(javascriptDir + "/vel2js" + bundle.getLocale() + ".js")));
        process(tDir, null, out, bundle.getBundle());
        out.flush();
        out.close();
    }

}

From source file:io.cslinmiso.line.utils.Utility.java

/**
 * Java IO (java io write file)//  ww  w.  j av a2  s .c  o  m
 * 
 * @param data
 * @param fileName
 * @throws IOException
 */
public static void writeFile(List<String> data, String fileName) throws IOException {
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8"));
    try {
        for (String d : data) {
            bw.write(d);
            bw.newLine();
        }
        bw.flush();
    } catch (IOException ioe) {
        throw ioe;
    } finally {
        bw.close();
    }
}

From source file:com.dc.util.file.FileSupport.java

public static void appendToFile(File file, String data) throws IOException {
    FileWriter fileWriter = null;
    BufferedWriter bufferedWriter = null;
    try {/*  w w  w  .j  a v a 2s .co m*/
        fileWriter = new FileWriter(file, true);
        bufferedWriter = new BufferedWriter(fileWriter);
        bufferedWriter.write(data);
        bufferedWriter.flush();
    } finally {
        if (fileWriter != null) {
            fileWriter.close();
        }
        if (bufferedWriter != null) {
            bufferedWriter.close();
        }
    }
}

From source file:com.healthmarketscience.jackcess.util.ExportUtil.java

/**
 * Copy a table into a new delimited text file <br>
 * Nearly equivalent to: {@code exportWriter(db, name, new BufferedWriter(f),
 * header, delim, quote, filter);}/*from  w  ww .jav a 2 s. c  o m*/
 * 
 * @param db
 *          Database the table to export belongs to
 * @param tableName
 *          Name of the table to export
 * @param f
 *          New file to create
 * @param header
 *          If <code>true</code> the first line contains the column names
 * @param delim
 *          The column delimiter, <code>null</code> for default (comma)
 * @param quote
 *          The quote character
 * @param filter
 *          valid export filter
 * 
 * @see #exportWriter(Database,String,BufferedWriter,boolean,String,char,ExportFilter)
 * @see Builder
 */
public static void exportFile(Database db, String tableName, File f, boolean header, String delim, char quote,
        ExportFilter filter) throws IOException {
    BufferedWriter out = null;
    try {
        out = new BufferedWriter(new FileWriter(f));
        exportWriter(db, tableName, out, header, delim, quote, filter);
        out.close();
    } finally {
        ByteUtil.closeQuietly(out);
    }
}

From source file:net.duckling.ddl.util.FileUtil.java

/**
 * Brief Intro Here//  w w w  .j a  v  a  2  s  . c  o m
 * @param file ?
 * @param content ?
 * @param charset ?
 * @return ??
 */
public static boolean writeFile(File file, String content, String charset) {
    try {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset));
        bw.write(content);
        bw.flush();
        bw.close();
        return true;
    } catch (IOException e) {
        return false;
    }
}

From source file:application.gen.gen.java

public static void gen(VelocityContext context, String templateFile, String genFilePath) {
    try {//  w  ww  . j  a  v  a  2s  .c  o m
        Template template = null;
        try {
            template = Velocity.getTemplate(templateFile);

        } catch (ResourceNotFoundException rnfe) {
            System.out.println("Example : error : cannot find template " + templateFile);
        } catch (ParseErrorException pee) {
            System.out.println("Example : Syntax error in template " + templateFile + ":" + pee);
        }

        // BufferedWriter writer = new BufferedWriter(new
        // OutputStreamWriter(System.out));
        StringWriter ww = new StringWriter();
        BufferedWriter writer = new BufferedWriter(ww);
        if (template != null)
            template.merge(context, ww);
        // System.out.println(ww.toString());
        FileUtils.writeStringToFile(new File(genFilePath), ww.toString(), "utf-8");
        System.out.println("generate file" + genFilePath);
        /*
         * flush and cleanup
         */
        writer.flush();
        writer.close();
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:com.healthmarketscience.jackcess.ExportUtil.java

/**
 * Copy a table into a new delimited text file <br>
 * Nearly equivalent to: {@code exportWriter(db, name, new BufferedWriter(f),
 * header, delim, quote, filter);}//  ww  w . ja  v  a  2s . co  m
 * 
 * @param db
 *          Database the table to export belongs to
 * @param tableName
 *          Name of the table to export
 * @param f
 *          New file to create
 * @param header
 *          If <code>true</code> the first line contains the column names
 * @param delim
 *          The column delimiter, <code>null</code> for default (comma)
 * @param quote
 *          The quote character
 * @param filter
 *          valid export filter
 * 
 * @see #exportWriter(Database,String,BufferedWriter,boolean,String,char,ExportFilter)
 * @see Builder
 */
public static void exportFile(Database db, String tableName, File f, boolean header, String delim, char quote,
        ExportFilter filter) throws IOException {
    BufferedWriter out = null;
    try {
        out = new BufferedWriter(new FileWriter(f));
        exportWriter(db, tableName, out, header, delim, quote, filter);
        out.close();
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (Exception ex) {
                LOG.warn("Could not close file " + f.getAbsolutePath(), ex);
            }
        }
    }
}

From source file:com.avinashbehera.sabera.network.HttpClient.java

public static JSONObject SendHttpPostUsingUrlConnection(String url, JSONObject jsonObjSend) {

    URL sendUrl;//from   w ww.  j ava  2 s  . com
    try {
        sendUrl = new URL(url);
    } catch (MalformedURLException e) {
        Log.d(TAG, "SendHttpPostUsingUrlConnection malformed URL");
        return null;
    }

    HttpURLConnection conn = null;

    try {
        conn = (HttpURLConnection) sendUrl.openConnection();
        conn.setReadTimeout(15000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);
        conn.setRequestMethod("POST");
        conn.setChunkedStreamingMode(1024);
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Accept", "application/json");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.addRequestProperty("Content-length", jsonObjSend.toJSONString().length() + "");
        conn.setDoInput(true);
        conn.setDoOutput(true);

        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
        //writer.write(getPostDataStringfromJsonObject(jsonObjSend));
        Log.d(TAG, "jsonobjectSend = " + jsonObjSend.toString());
        //writer.write(jsonObjSend.toString());
        writer.write(String.valueOf(jsonObjSend));

        writer.flush();
        writer.close();
        os.close();

        int responseCode = conn.getResponseCode();
        Log.d(TAG, "responseCode = " + responseCode);

        if (responseCode == HttpsURLConnection.HTTP_OK) {

            Log.d(TAG, "responseCode = HTTP OK");

            InputStream instream = conn.getInputStream();

            String resultString = convertStreamToString(instream);
            instream.close();
            Log.d(TAG, "resultString = " + resultString);
            //resultString = resultString.substring(1,resultString.length()-1); // remove wrapping "[" and "]"

            // Transform the String into a JSONObject
            JSONParser parser = new JSONParser();
            JSONObject jsonObjRecv = (JSONObject) parser.parse(resultString);
            // Raw DEBUG output of our received JSON object:
            Log.i(TAG, "<JSONObject>\n" + jsonObjRecv.toString() + "\n</JSONObject>");

            return jsonObjRecv;

        }

    } catch (Exception e) {
        // More about HTTP exception handling in another tutorial.
        // For now we just print the stack trace.
        e.printStackTrace();
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }
    return null;

}