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.netflix.config.DynamicFileConfigurationTest.java

static File createConfigFile(String prefix) throws Exception {
    configFile = File.createTempFile(prefix, ".properties");
    BufferedWriter writer = new BufferedWriter(
            new OutputStreamWriter(new FileOutputStream(configFile), "UTF-8"));
    writer.write("dprops1=123456789");
    writer.newLine();/*from  ww  w  .j  a  v a 2s .  c om*/
    writer.write("dprops2=79.98");
    writer.newLine();
    writer.close();
    System.err.println(configFile.getPath() + " created");
    return configFile;
}

From source file:iics.Connection.java

static void create_dir() {

    try {/*ww w. j  av  a 2 s . c  o m*/
        File directory = new File(dir);
        if (directory.exists()) {
            create_file();

        } else {
            System.out.println("Directory not exists, creating now");

            success = directory.mkdir();
            if (success) {
                create_file();
            } else {
                System.out.printf("Failed to create new directory: %s%n", dir);
                JOptionPane.showMessageDialog(null,
                        "                 An error occured!! \n Contact your system admin for help.", null,
                        JOptionPane.WARNING_MESSAGE);
                close_loda();
            }
        }
        fw = new FileWriter(f.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(content);
        bw.close();
    } catch (IOException ex) {
        //   Logger.getLogger(Extract.class.getName()).log(Level.SEVERE, null, ex);
        JOptionPane.showMessageDialog(null,
                "                 An error occured!! \n Contact your system admin for help.", null,
                JOptionPane.WARNING_MESSAGE);
        close_loda();
    } finally {
        try {
            fw.close();
        } catch (IOException ex) {
            //   Logger.getLogger(Extract.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null,
                    "                 An error occured!! \n Contact your system admin for help.", null,
                    JOptionPane.WARNING_MESSAGE);
            close_loda();
        }
    }
}

From source file:mase.MaseEvolve.java

public static Map<String, String> readParams(String[] args) throws Exception {
    // Reads command line parameters to a map
    // Checks the parent order
    Map<String, String> argsPars = new LinkedHashMap<>();
    int parentOrder = 0;
    for (int i = 0; i < args.length; i++) {
        if (args[i].equals("-p")) {
            String par = args[i + 1];
            String[] kv = par.split("=");
            if (kv[0].startsWith("parent")) { // Parent parameter
                if (kv[0].equals("parent")) { // Parent with no number
                    argsPars.put("parent." + parentOrder, kv[1]);
                    parentOrder++;/*from  w  w w  . j a v a 2 s .  c  o  m*/
                } else { // Numbered parent
                    String[] split = kv[0].split("\\.");
                    int num = Integer.parseInt(split[1]);
                    if (num != parentOrder) {
                        throw new Exception("Parent out of order: " + par);
                    }
                    argsPars.put("parent." + num, kv[1]);
                    parentOrder++;
                }
            } else { // Not a parent parameter
                argsPars.put(kv[0], kv[1]);
            }
        } else if (args[i].equals("-file")) {
            argsPars.put("parent.0", args[i + 1]);
        }
    }

    // Write command line parameters to a temp file -- the root file
    File tempFile = File.createTempFile("masetemp", ".params", new File("."));
    BufferedWriter bw = new BufferedWriter(new FileWriter(tempFile));
    for (Entry<String, String> e : argsPars.entrySet()) {
        bw.write(e.getKey() + " = " + e.getValue());
        bw.newLine();
    }
    bw.close();

    // Load all the parameter files
    Map<String, String> pars = new LinkedHashMap<>();
    loadParams(pars, tempFile);
    tempFile.delete();
    return pars;
}

From source file:iics.Connection.java

static void create_file() {
    try {/*  w  w w  . j av  a2s . c  o  m*/
        if (f.exists()) {
            content = "<html><head>"
                    + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"> "
                    + "</head><body onload='document.form1.submit()'>\n"
                    + "<form id='form1' name='form1' method='post' action='" + "http://" + path + "/IICS/"
                    + "/'>\n" + "<input type='type' name='s' value='" + IV.toString() + "' />\n"
                    + "<input type='type' name='s1' value='" + encryptionKey.toString() + "' />\n"
                    + "<input type='type' name='b' value='" + Base64Enc.encode((IV.toString() + "~~~"
                            + dt.getLHost().get(1) + "~~~" + det + "~~~" + encryptionKey.toString()).trim())
                    + "' />\n";

            content = content + "</form>\n" + "</body></html>";
            fw = new FileWriter(f.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();

        } else {

            System.out.println("No such file exists, creating now");
            success = f.createNewFile();
            if (success) {
                System.out.printf("Successfully created new file: %s%n", f);
            } else {
                //  System.out.printf("Failed to create new file: %s%n", f);
                JOptionPane.showMessageDialog(null,
                        "                 An error occured!! \n Contact your system admin for help.", null,
                        JOptionPane.WARNING_MESSAGE);
                close_loda();
            }

        }
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null,
                "                 An error occured!! \n Contact your system admin for help.", null,
                JOptionPane.WARNING_MESSAGE);
        close_loda();
    }
}

From source file:com.wso2telco.util.FileUtil.java

/**
 * File write.// w w w.  j a va 2 s .  c  o  m
 *
 * @param filePath the file path
 * @param data the data
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static void fileWrite(String filePath, String data) throws IOException {
    BufferedWriter out = null;
    try {
        out = new BufferedWriter(new FileWriter(filePath));
        out.write(data);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        out.close();
    }

}

From source file:org.openmrs.module.report.util.FileUtils.java

public static void WriteStringToFile(String str, String file) {

    try {/*  ww  w.j a  v a2  s  .  co  m*/
        BufferedWriter output = new BufferedWriter(new FileWriter(file));
        output.write(str);
        output.flush();
        output.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void write(File file, String data) {
    BufferedWriter out = null;
    try {/*  w w w  . ja  v  a2s  . co  m*/
        out = new BufferedWriter(new FileWriter(file), 1024);
        out.write(data);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (out != null) {
            try {
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:Main.java

/**
 * Trims down obj files, so that they may be parsed faster later on.
 * Remove uneccessary whitespaces, comments etc.
 * @param in stream to be trimmed/*from   w  ww .  j  a v a2s  . c  om*/
 * @param out the resulting trimmed stream
 */
public static void trim(BufferedReader in, BufferedWriter out) throws IOException {
    String line;
    out.write("#trimmed\n");
    for (line = in.readLine(); line != null; line = in.readLine()) {
        line = getCanonicalLine(line);
        if (line.length() > 0) {
            out.write(line.trim());
            out.write('\n');
        }
    }
    in.close();
    out.close();
}

From source file:io.github.data4all.util.upload.GpxTrackUtil.java

/**
 * Method for reading a track from memory. Return a string representation of
 * a saved track./*from ww w .  jav a2s  .  co m*/
 * 
 * @param context
 *            the context
 * @param trackXml
 *            the xml file of the gpx track
 * @param fileName
 *            the filename of the gpx track
 * @return a file object containing the gpx tracks
 */
private static File createGpxFile(Context context, String trackXml, String fileName) {
    final File file = new File(context.getFilesDir(), fileName);
    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter(new FileWriter(file));
        writer.write(trackXml);
        writer.close();
    } catch (IOException e) {
        Log.e(GpxTrackUtil.class.getSimpleName(), "IOException: ", e);
    }
    return file;
}

From source file:edu.indiana.d2i.htrc.util.Utilities.java

public static void Clean2Unclean(String input, String output) throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader(input));
    BufferedWriter writer = new BufferedWriter(new FileWriter(output));

    Pairtree pairtree = new Pairtree();

    String line = null;//  w w w.  jav  a2 s .  c  o m
    while ((line = reader.readLine()) != null) {
        writer.write(pairtree.uncleanId(line) + "\n");
    }

    writer.close();
    reader.close();
}