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:eu.udig.catalog.jgrass.utils.JGrassCatalogUtilities.java

public static boolean createMapset(String locationPath, String mapset, CoordinateReferenceSystem crs,
        JGrassRegion window) {//from   w  w  w.j  av a2 s .c  o  m
    String path = locationPath + File.separator + mapset;

    /* Create mapset directory */
    if (!(new File(path).mkdirs()))
        return false;

    if (mapset.equals(JGrassConstants.PERMANENT_MAPSET)) {
        /* Create blank DEFAULT_WIND and WIND files */
        try {
            if (window != null) {
                JGrassRegion.writeWINDToMapset(path, window);
                JGrassRegion.writeDEFAULTWINDToLocation(locationPath, window);
            } else {
                // create blank windows
                BufferedWriter out = new BufferedWriter(
                        new FileWriter(path + File.separator + JGrassConstants.DEFAULT_WIND));
                out.write(JGrassRegion.BLACKBOARD_KEY);
                out.close();

                out = new BufferedWriter(new FileWriter(path + File.separator + JGrassConstants.WIND));
                out.write(JGrassRegion.BLANK_REGION);
                out.close();
            }

            /* Create projection files */
            if (crs != null) {
                // FIXME create GRASS proj files

                BufferedWriter prjOut = new BufferedWriter(
                        new FileWriter(path + File.separator + JGrassConstants.PROJ_WKT));
                prjOut.write(crs.toWKT());
                prjOut.close();

            }
        } catch (IOException e) {
            JGrassPlugin.log(
                    "JGrassPlugin problem: eu.hydrologis.udig.catalog.utils#JGrassCatalogUtilities#createMapset",
                    e);
            e.printStackTrace();
            return false;
        }

    } else {
        /* Copy WIND file from PERMANENT mapset of this location */
        try {
            BufferedReader in = new BufferedReader(new FileReader(locationPath + File.separator
                    + JGrassConstants.PERMANENT_MAPSET + File.separator + JGrassConstants.DEFAULT_WIND));
            BufferedWriter out = new BufferedWriter(
                    new FileWriter(path + File.separator + JGrassConstants.WIND));
            String line;
            while ((line = in.readLine()) != null) {
                out.write(line);
                out.write("\n");
            }
            out.close();
            in.close();
        } catch (IOException e) {
            JGrassPlugin.log(
                    "JGrassPlugin problem: eu.hydrologis.udig.catalog.utils#JGrassCatalogUtilities#createMapset",
                    e);
            e.printStackTrace();
            return false;
        }
    }

    /* Create point/site directories */
    if (!(new File(path + File.separator + JGrassConstants.SITE_LISTS).mkdirs()))
        return false;

    /* Create raster directories */
    if (!(new File(path + File.separator + JGrassConstants.FCELL).mkdirs()))
        return false;
    if (!(new File(path + File.separator + JGrassConstants.CELL).mkdirs()))
        return false;
    if (!(new File(path + File.separator + JGrassConstants.CELLHD).mkdirs()))
        return false;
    if (!(new File(path + File.separator + JGrassConstants.CATS).mkdirs()))
        return false;
    if (!(new File(path + File.separator + JGrassConstants.COLR).mkdirs()))
        return false;
    if (!(new File(path + File.separator + JGrassConstants.CELL_MISC).mkdirs()))
        return false;

    /* Create vector directories */
    if (!(new File(path + File.separator + JGrassConstants.DIG).mkdirs()))
        return false;
    if (!(new File(path + File.separator + JGrassConstants.DIG_ATTS).mkdirs()))
        return false;
    if (!(new File(path + File.separator + JGrassConstants.DIG_CATS).mkdirs()))
        return false;

    if (!createJGrassFolders(path))
        return false;

    return true;
}

From source file:edu.isi.pfindr.learn.util.PairsFileIO.java

public static void generatePairsFromStringAndFileContentWithNoClass(String userInput, String inputFilePath,
        String outputFilePath) {/*from   ww  w.ja v  a2  s . c o m*/

    List<String> phenotypeList = new ArrayList<String>();

    try {
        phenotypeList = FileUtils.readLines(new File(inputFilePath));
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    String[] phenotype2;
    StringBuffer outputBuffer = new StringBuffer();

    BufferedWriter bw = null;
    try {
        bw = new BufferedWriter(new FileWriter(outputFilePath));
        for (int j = 0; j < phenotypeList.size(); j++) {
            phenotype2 = phenotypeList.get(j).split("\t");

            outputBuffer.append(String.format("%s\t%s\t%d\n", userInput, phenotype2[3], 0));
            bw.append(outputBuffer.toString());
            outputBuffer.setLength(0);
        }
    } catch (IOException io) {
        try {
            if (bw != null) {
                bw.close();
                bw = null;
            }
            io.printStackTrace();
        } catch (IOException e) {
            System.out.println("Problem occured while closing output stream  " + bw);
            e.printStackTrace();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (bw != null) {
                bw.close();
                bw = null;
            }
        } catch (IOException e) {
            System.out.println("Problem occured while closing output stream  " + bw);
            e.printStackTrace();
        }
    }
}

From source file:mattmc.mankini.commands.CommandQuote.java

private void addQuote(String content, MessageEvent<PircBotX> event) {
    try {// w w w .  j  ava  2s . com
        if (!file.exists()) {
            System.out.println(file.createNewFile());
        }

        FileWriter fw;

        fw = new FileWriter(file, true);

        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(content);
        bw.newLine();
        bw.close();

        MessageSending.sendNormalMessage("Quote Added!", event);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:hsa.awp.common.dao.template.TemplateFileSystemDao.java

@Override
public void saveTemplate(String content, TemplateDetail templateDetail) {

    try {//from w w  w .  ja v  a  2  s  .  c  om
        File file = new File(generatePath(templateDetail));
        BufferedWriter out = new BufferedWriter(new FileWriter(file));
        out.write(content);
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:de.akquinet.dustjs.DustEngine.java

public void compile(File input, File output) {
    try {//from w ww  .  j  a v  a 2 s  .c  om
        String content = compile(input);
        if (!output.exists()) {
            output.createNewFile();
        }
        BufferedWriter bw = new BufferedWriter(new FileWriter(output));
        bw.write(content);
        bw.close();
    } catch (IOException e) {
        logger.error(e.getMessage());
    }
}

From source file:nl.tue.gale.ae.JSONServlet.java

private void reply(String s, HttpServletResponse resp) throws IOException {
    resp.setBufferSize(8192);// w ww  .  j  ava 2s  .  c o  m
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(resp.getOutputStream()));
    bw.write(s);
    bw.close();
}

From source file:org.openmrs.logic.rule.definition.JavaLanguageHandler.java

/**
* @see CompilableLanguageHandler#prepareSource(RuleDefinition,String)
*///from w  w  w.  j  a  v a 2  s. c o  m
public void prepareSource(RuleDefinition logicRule, String className) {

    AdministrationService as = Context.getAdministrationService();
    String javaDirectory = as.getGlobalProperty(LogicConstants.RULE_DEFAULT_SOURCE_FOLDER);
    File sourceDirectory = OpenmrsUtil.getDirectoryInApplicationDataDirectory(javaDirectory);

    String path = className.replace('.', File.separatorChar);

    File javaFile = new File(sourceDirectory, path + JAVA_EXTENSION);
    if (!javaFile.getParentFile().exists())
        javaFile.getParentFile().mkdirs();

    Date modifiedDate = logicRule.getDateChanged();
    if (modifiedDate == null) {
        modifiedDate = logicRule.getDateCreated();
    }

    // only compile when the java file is not exist or the concept derived is updated after the source file last modified
    if (!javaFile.exists() || modifiedDate.after(new Date(javaFile.lastModified()))) {
        String content = logicRule.getRuleContent();
        content = content.replace("{CLASSNAME}", className.substring(className.lastIndexOf('.') + 1));
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(javaFile));
            writer.write(content);
            writer.close();
        } catch (IOException e) {
            log.error("Failed saving java rule file ...", e);
        }
    }
}

From source file:es.uja.photofirma.android.Logger.java

/**
 * Almacena un archivo *.log en una URL especificada de todo el proceso recolectado hasta el momento de la ejecucion de la orden logWrite() y el ltimo logStop() o logStart()
 * @param logUrl Indica la url donde se debe almacenar el archivo *.log generado
 */// w  w w.  j  a  va2  s. c o m
public void logWrite(String logUrl) {
    File logFile = new File(logUrl);

    String sjson = logJson.toString();

    try {
        BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
        buf.write(sjson);
        buf.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.maxl.java.aips2xml.Aips2Xml.java

static void writeToFile(String string_to_write, String dir_name, String file_name) {
    try {/*from  ww  w  .  j  a va 2  s .c  o m*/
        File wdir = new File(dir_name);
        if (!wdir.exists())
            wdir.mkdirs();
        File wfile = new File(dir_name + file_name);
        if (!wfile.exists())
            wfile.createNewFile();
        // FileWriter fw = new FileWriter(wfile.getAbsoluteFile());
        CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
        encoder.onMalformedInput(CodingErrorAction.REPORT);
        encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(wfile.getAbsoluteFile()), encoder);
        BufferedWriter bw = new BufferedWriter(osw);
        bw.write(string_to_write);
        bw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.mansoor.uncommon.configuration.JsonConfiguration.java

/**
 * Save the json configuration to the given file.
 *
 * @param file file where the config will be sasved
 * @param json json String./*from w ww  . j  a v a2  s . c o m*/
 */
private void saveFile(final File file, final String json) {
    try {
        final BufferedWriter out = new BufferedWriter(new FileWriter(file));
        out.write(json);
        out.close();
    } catch (Exception e) {
        throw new IllegalStateException("unable to save file", e);
    }
}