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.cemso.util.CheckDeviceStateExcuter.java

public static void checkStart() {
    if (log.isDebugEnabled()) {
        log.debug("CheckDeviceStateExcuter.checkStart()...");
    }/*ww  w .j  a  v  a  2s . c  o m*/

    CheckRunner checker = new CheckDeviceStateExcuter().new CheckRunner();
    Thread t = new Thread(checker, "checker");
    checkerThread = t;

    final ScheduledFuture<?> checkerHandle = scheduler.scheduleAtFixedRate(t, 1, 2, TimeUnit.MINUTES);
    checkerHandle1 = checkerHandle;
    if (log.isInfoEnabled()) {
        log.info("auto refresh is started...");
        try {
            File f = new File("C:/DLLfunctionsTest.txt");
            FileWriter fw = new FileWriter(f, true);
            BufferedWriter bw = new BufferedWriter(fw);
            bw.newLine();
            bw.append(new java.util.Date().toString() + ": auto refresh is started...");
            bw.newLine();
            bw.flush();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:css.variable.converter.CSSVariableConverter.java

/**
 * Go through existing CSS Files and replace variable access with variable
 * values/* w  w w .ja  va  2 s . com*/
 *
 * @param rootCSSVars All css variables you want to convert from name to value
 */
private static void editForRelease(ArrayList<CSSVar> rootCSSVars) {
    for (File cssFile : theCSSList) {

        ArrayList<CSSVar> localCSSVars = getCSSVars(cssFile, ":local");
        for (CSSVar cssVar : rootCSSVars) {
            if (!localCSSVars.contains(cssVar)) {
                localCSSVars.add((CSSVar) (cssVar.clone()));
            }
        }

        // This will store info the new text for the file we will write below
        ArrayList<String> filesNewInfo = new ArrayList<String>();
        try {

            Scanner fileReader = new Scanner(cssFile);
            while (fileReader.hasNextLine()) {

                String currentLine = fileReader.nextLine();

                // If we find variables, replace them, with their value
                if (currentLine.contains("var(")) {
                    for (CSSVar var : localCSSVars) {
                        while (currentLine.contains(var.getName())) {
                            currentLine = currentLine.replace("var(" + var.getName() + ")", var.getValue());
                        }
                    }
                }
                // Add new currentLine to be written
                filesNewInfo.add(currentLine);
            }
            fileReader.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(CSSVariableConverter.class.getName()).log(Level.SEVERE, null, ex);
        }

        // Write the new files below
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(cssFile.getPath()));

            while (!filesNewInfo.isEmpty()) {
                writer.write(filesNewInfo.get(0));
                writer.newLine();
                filesNewInfo.remove(0);
            }
            writer.close();
        } catch (IOException ex) {
            Logger.getLogger(CSSVariableConverter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:gemlite.core.util.RSAUtils.java

/**
 * ?key?//w w w  .j  a v  a 2s.co  m
 * @param filePath
 * @return
 */
public static String writeKey(String filePath, String content) {
    StringBuilder sb = new StringBuilder();
    File file = new File(filePath);
    try {
        String charsetName = "utf-8";
        OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), charsetName);
        BufferedWriter pw = new BufferedWriter(writer);
        pw.write(content);
        pw.close();
    } catch (Exception e) {
        LogUtil.getCoreLog().warn("writeKey file :{} failure :{}", filePath, e);
    }
    return sb.toString();
}

From source file:com.csipsimple.backup.SipProfileJson.java

/**
 * Save current sip configuration/*from  www.  j  a  v  a  2  s  . co m*/
 * 
 * @param ctxt
 * @return
 */
public static boolean saveSipConfiguration(Context ctxt, String filePassword) {
    File dir = PreferencesWrapper.getConfigFolder(ctxt);
    if (dir != null) {
        Date d = new Date();
        File file = new File(dir.getAbsoluteFile() + File.separator + "backup_"
                + DateFormat.format("yy-MM-dd_kkmmss", d) + ".json");
        Log.d(THIS_FILE, "Out dir " + file.getAbsolutePath());

        JSONObject configChain = new JSONObject();
        try {
            configChain.put(KEY_ACCOUNTS, serializeSipProfiles(ctxt));
        } catch (JSONException e) {
            Log.e(THIS_FILE, "Impossible to add profiles", e);
        }
        try {
            configChain.put(KEY_SETTINGS, serializeSipSettings(ctxt));
        } catch (JSONException e) {
            Log.e(THIS_FILE, "Impossible to add profiles", e);
        }

        try {
            // Create file
            OutputStream fos = new FileOutputStream(file);
            if (!TextUtils.isEmpty(filePassword)) {
                Cipher c;
                try {
                    c = Cipher.getInstance("AES");
                    SecretKeySpec k = new SecretKeySpec(filePassword.getBytes(), "AES");
                    c.init(Cipher.ENCRYPT_MODE, k);
                    fos = new CipherOutputStream(fos, c);
                } catch (NoSuchAlgorithmException e) {
                    Log.e(THIS_FILE, "NoSuchAlgorithmException :: ", e);
                } catch (NoSuchPaddingException e) {
                    Log.e(THIS_FILE, "NoSuchPaddingException :: ", e);
                } catch (InvalidKeyException e) {
                    Log.e(THIS_FILE, "InvalidKeyException :: ", e);
                }
            }
            FileWriter fstream = new FileWriter(file.getAbsoluteFile());
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(configChain.toString(2));
            // Close the output stream
            out.close();
            return true;
        } catch (Exception e) {
            // Catch exception if any
            Log.e(THIS_FILE, "Impossible to save config to disk", e);
            return false;
        }
    }
    return false;
}

From source file:com.ushahidi.android.app.util.Util.java

/**
 * For debugging purposes. Append content of a string to a file
 * //  w w  w .j  a  va2 s . co  m
 * @param text
 */
public static void appendLog(String text) {
    File logFile = new File(Environment.getExternalStorageDirectory(), "ush_log.txt");
    if (!logFile.exists()) {
        try {
            logFile.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    try {
        // BufferedWriter for performance, true to set append to file flag
        BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
        buf.append(text);
        buf.newLine();
        buf.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:DiskIO.java

public static void saveStringInFile(File toFile, String insertString) throws IOException {
    BufferedWriter out;

    out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(toFile), "ISO-8859-1"));
    out.write(insertString);/*from   ww w .  ja va2s  . c o m*/
    out.flush();
    out.close();
}

From source file:de.mpg.escidoc.services.syndication.Utils.java

/**
 * Writes <code>content</code> to the file
 * @param fileName//from   www  .j  a va 2 s.com
 * @param content 
 * @throws IOException
 */
public static void writeToFile(String fileName, String content) throws IOException {
    FileWriter fw = new FileWriter(fileName);
    BufferedWriter out = new BufferedWriter(fw);
    out.write(content);
    out.close();
}

From source file:it.polimi.diceH2020.plugin.control.DICEWrap.java

/**
 * Creates JSIM model (.jsimg) from pnml in the given directory  
 * /*from ww w.  j  a  v a2 s. co m*/
 * @throws IOException
 */

public static void genJSIM(int cdid, String alt, String lastTransactionId) throws IOException {
    Configuration conf = Configuration.getCurrent();
    File sparkIdx = new File(Preferences.getSavingDir() + "spark.idx");
    String fileName;

    if (Configuration.getCurrent().getIsPrivate()) {
        fileName = Preferences.getSavingDir() + conf.getID() + "J" + cdid + "inHouse" + alt;
    } else {
        fileName = Preferences.getSavingDir() + conf.getID() + "J" + cdid + alt.replaceAll("-", "");
    }

    try {
        FileWriter fw = new FileWriter(sparkIdx.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(lastTransactionId);
        bw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    String pnmlPath = Preferences.getSavingDir() + conf.getID() + "J" + cdid + alt.replaceAll("-", "")
            + ".pnml";
    String outputPath = new File(fileName + ".jsimg").getAbsolutePath();
    String indexPath = sparkIdx.getAbsolutePath();

    String command = String.format("java -cp %sbin:%slib/* PNML_Pre_Processor gspn %s %s %s",
            Preferences.getJmTPath(), Preferences.getJmTPath(), pnmlPath, outputPath, indexPath);

    System.out.println("Calling PNML_Pre_Processor");
    System.out.println(command);

    Process proc;

    try {
        proc = Runtime.getRuntime().exec(command);
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }

    try {
        proc.waitFor();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}

From source file:com.isa.utiles.Utiles.java

public static void downloadFile(String linkDescarga, String rutaDestino)
        throws MalformedURLException, IOException {

    URL urlFile = new URL(linkDescarga);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    InputStream in = new BufferedInputStream(urlFile.openStream());

    byte[] buf = new byte[1024];
    int n = 0;/*  www  .  j av  a 2  s  . c  om*/
    while (-1 != (n = in.read(buf))) {
        out.write(buf, 0, n);
    }
    out.close();
    in.close();

    /*
    byte[] response = out.toByteArray();
    FileOutputStream fos = new FileOutputStream(rutaDestino);
    fos.write(response);
    fos.close();
    */

    File file = new File(rutaDestino);
    file.setWritable(true);
    file.setReadable(true);

    BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
    bw.write(out.toString());
    bw.close();
}

From source file:com.kylinolap.query.test.KylinTestBase.java

protected static void putTextTofile(File file, String sql) throws IOException {
    BufferedWriter writer = new BufferedWriter(new FileWriter(file));
    writer.write(sql, 0, sql.length());//from   w w  w. java 2  s.  co m
    writer.close();
}