Example usage for java.io FileWriter close

List of usage examples for java.io FileWriter close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Usage

From source file:Main.java

public static String getMotherboardSN() {
    String result = "";
    try {/*w w w .  ja  v  a2 s.c o  m*/
        File file = File.createTempFile("realhowto", ".vbs");
        file.deleteOnExit();
        FileWriter fw = new java.io.FileWriter(file);

        String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
                + "Set colItems = objWMIService.ExecQuery _ \n" + "   (\"Select * from Win32_BaseBoard\") \n"
                + "For Each objItem in colItems \n" + "    Wscript.Echo objItem.SerialNumber \n"
                + "    exit for  ' do the first cpu only! \n" + "Next \n";

        fw.write(vbs);
        fw.close();
        Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while ((line = input.readLine()) != null) {
            result += line;
        }
        input.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result.trim();
}

From source file:gov.nih.nci.caintegrator.application.query.GenomicDataFileWriter.java

/**
 * Writes a GenomicDataQueryResult to the given file.
 * @param result genomic query result to write in csv format.
 * @param csvFile to write file to./*from w  ww.  java  2s.c o m*/
 * @return csv file.
 */
public static File writeAsCsv(GenomicDataQueryResult result, File csvFile) {
    try {
        FileWriter writer = new FileWriter(csvFile);
        if (ResultsOrientationEnum.SUBJECTS_AS_COLUMNS.equals(result.getQuery().getOrientation())) {
            writeStandardOrientation(result, writer);
        } else {
            writeSubjectsAsRowsOrientation(result, writer);
        }
        writer.flush();
        writer.close();
    } catch (IOException e) {
        throw new IllegalArgumentException("Couldn't write file at the path " + csvFile.getAbsolutePath(), e);
    }
    return csvFile;
}

From source file:com.serotonin.bacnet4j.util.sero.StreamUtils.java

public static void writeFile(File file, String content) throws IOException {
    FileWriter out = null;
    try {/*from  www .  ja  v a 2 s.  co m*/
        out = new FileWriter(file);
        out.write(content);
    } finally {
        if (out != null)
            out.close();
    }
}

From source file:com.abid_mujtaba.fetchheaders.models.Account.java

private static void createEmptyAccountsJsonFile(File file) {
    try {//from  www. j a  v  a2s . c o  m
        // We start by creating the JSON tree for an empty accounts.json file.
        JSONObject root = new JSONObject();
        JSONArray accounts = new JSONArray();

        root.put("accounts", accounts);

        // Save JSON to accounts.json
        FileWriter fw = new FileWriter(file); // Write root JSON object to file info.json
        fw.write(root.toString());
        fw.flush();
        fw.close();
    } catch (JSONException e) {
        Log.e(Resources.LOGTAG, "Exception raised while manipulate JSON objects.", e);
    } catch (IOException e) {
        Log.e(Resources.LOGTAG, "Exception raised while saving content to json file.", e);
    }
}

From source file:loadTest.loadTestLib.LUtil.java

public static void setUpResultFile() throws IOException {
    FileWriter writer = new FileWriter("loadTestResults.csv");
    writer.write("NodeCount,FileCount,FileSize,ResultTime\n");
    writer.flush();/* ww  w.  j a v  a2s .c o m*/
    writer.close();
}

From source file:Main.java

public static Boolean canWrite(File f) {
    if (f.isDirectory()) {
        FileWriter w = null;
        String testFilename = f.getPath() + "/.EASYRPG_WRITE_TEST";
        try {/*from www  . ja  v a  2  s .co  m*/
            w = new FileWriter(testFilename);
            // Permissions are checked on open, but it is Android, better be save
            w.write("Android >.<");
        } catch (IOException e) {
            return false;
        } finally {
            try {
                if (w != null) {
                    w.close();
                }
            } catch (IOException e) {
            }
        }

        File testFile = new File(testFilename);
        if (testFile.exists()) {
            // Does not throw
            testFile.delete();
        }
    } else {
        boolean deleteAfter = f.exists();
        try {
            FileWriter w = new FileWriter(f, true);
            w.close();
        } catch (IOException e) {
            return false;
        }

        if (deleteAfter) {
            f.delete();
        }
    }

    return true;
}

From source file:com.ibm.watson.app.qaclassifier.tools.GenerateTrainingAndPopulationData.java

private static void writeGSON(String src, File output) throws IOException {
    FileWriter writer = null;
    try {/*  w  ww  .j  a  v a2  s.co m*/
        writer = new FileWriter(output);
        writer.write(src);
        writer.flush();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static boolean writeFile(String filePath, List<String> contentList, boolean append) {
    if (contentList == null || contentList.size() < 1) {
        return false;
    }/*  www  .  ja  v  a  2 s . co m*/

    FileWriter fileWriter = null;
    try {
        makeDirs(filePath);
        fileWriter = new FileWriter(filePath, append);
        int i = 0;
        for (String line : contentList) {
            if (i++ > 0) {
                fileWriter.write("\r\n");
            }
            fileWriter.write(line);
        }
        fileWriter.close();
        return true;
    } catch (IOException e) {
        throw new RuntimeException("IOException occurred. ", e);
    } finally {
        if (fileWriter != null) {
            try {
                fileWriter.close();
            } catch (IOException e) {
                throw new RuntimeException("IOException occurred. ", e);
            }
        }
    }
}

From source file:eu.europa.ec.markt.dss.validation102853.ValidationResourceManager.java

/**
 * This method saves the data in the output stream to a file.
 *
 * @param diagnosticDataFileName/* w w w .j av a 2  s .  co  m*/
 * @param outputStream
 * @throws IOException
 */
protected static void saveToFile(final String diagnosticDataFileName, final OutputStream outputStream)
        throws IOException {

    FileWriter file = null;
    try {

        file = new FileWriter(diagnosticDataFileName);
        file.write(outputStream.toString());
    } finally {

        if (file != null) {

            file.close();
        }
    }
}

From source file:com.github.rvesse.airline.parser.aliases.TestAliases.java

public static void prepareConfig(File f, String... lines) throws IOException {
    FileWriter writer = new FileWriter(f);
    for (String line : lines) {
        writer.append(line);/*from  ww w  .  ja  va2s .  c  om*/
        writer.append('\n');
    }
    writer.close();
}