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:com.bluexml.side.m2.repositoryBuilder.Builder.java

private static void writeShellScript(File script, List<File> poms) throws Exception {
    // create file
    if (!script.exists()) {
        script.createNewFile();/*from   w ww  .  j a  va  2  s . c o m*/
    }

    FileWriter fw = new FileWriter(script);

    // write header
    String header = "#!/bin/bash\n";
    header += "if [ $# -eq 1 ]; then" + "\n";
    header += "  MAVENREPO=$1" + "\n";
    header += "else" + "\n";
    header += "  echo \"Usage: patcher.sh MAVENREPO\"" + "\n";
    header += "  echo \"       with MAVENREPO = maven.repo.local absolute path\"" + "\n";
    header += "  exit -2" + "\n";
    header += "fi" + "\n";
    fw.write(header);

    for (File file : poms) {
        fw.write("mvn dependency:go-offline -P public -f " + file.getAbsolutePath()
                + " -Dmaven.repo.local=$MAVENREPO\n");
    }

    // save close
    fw.flush();
    fw.close();
}

From source file:au.org.ala.layers.grid.GridClassBuilder.java

static void exportSLD(String filename, String name, ArrayList<Integer> maxValues, ArrayList<String> labels) {
    StringBuffer sld = new StringBuffer();
    /* header *//*  w  ww .j  a v  a2 s  . c  om*/
    sld.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    sld.append(
            "<sld:StyledLayerDescriptor xmlns=\"http://www.opengis.net/sld\" xmlns:sld=\"http://www.opengis.net/sld\" xmlns:ogc=\"http://www.opengis.net/ogc\" xmlns:gml=\"http://www.opengis.net/gml\" version=\"1.0.0\">");
    sld.append("<sld:NamedLayer>");
    sld.append("<sld:Name>raster</sld:Name>");
    sld.append(" <sld:UserStyle>");
    sld.append("<sld:Name>raster</sld:Name>");
    sld.append("<sld:Title>A very simple color map</sld:Title>");
    sld.append("<sld:Abstract>A very basic color map</sld:Abstract>");
    sld.append("<sld:FeatureTypeStyle>");
    sld.append(" <sld:Name>name</sld:Name>");
    sld.append("<sld:FeatureTypeName>Feature</sld:FeatureTypeName>");
    sld.append(" <sld:Rule>");
    sld.append("   <sld:RasterSymbolizer>");
    sld.append(" <sld:Geometry>");
    sld.append(" <ogc:PropertyName>geom</ogc:PropertyName>");
    sld.append(" </sld:Geometry>");
    sld.append(" <sld:ChannelSelection>");
    sld.append(" <sld:GrayChannel>");
    sld.append("   <sld:SourceChannelName>1</sld:SourceChannelName>");
    sld.append(" </sld:GrayChannel>");
    sld.append(" </sld:ChannelSelection>");
    sld.append(" <sld:ColorMap type=\"intervals\">");

    //sort labels
    List<String> sortedLabels = new ArrayList<String>(labels);
    Collections.sort(sortedLabels);

    /* outputs */
    sld.append("\n<sld:ColorMapEntry color=\"#ffffff\" opacity=\"0\" quantity=\"1\"/>\n");
    for (int j = 0; j < sortedLabels.size(); j++) {
        int i = 0;
        while (i < labels.size()) {
            if (labels.get(i).equals(sortedLabels.get(j)))
                break;
            i++;
        }
        sld.append("<sld:ColorMapEntry color=\"#" + getHexColour(colours[i % colours.length]) + "\" quantity=\""
                + (maxValues.get(i) + 1) + ".0\" label=\"" + labels.get(i) + "\" opacity=\"1\"/>\r\n");
    }

    /* footer */
    sld.append(
            "</sld:ColorMap></sld:RasterSymbolizer></sld:Rule></sld:FeatureTypeStyle></sld:UserStyle></sld:NamedLayer></sld:StyledLayerDescriptor>");

    /* write */
    FileWriter fw = null;
    try {
        fw = new FileWriter(filename);
        fw.append(sld.toString());
        fw.flush();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        if (fw != null) {
            try {
                fw.close();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
    }
}

From source file:com.sldeditor.test.unit.tool.vector.VectorToolTest.java

/**
 * Stream 2 file./*from w  w w . j ava 2 s . c om*/
 *
 * @param in the in
 * @return the file
 * @throws IOException Signals that an I/O exception has occurred.
 */
private static File stream2file(InputStream in) throws IOException {
    final File tempFile = File.createTempFile(PREFIX, SUFFIX);
    try (FileOutputStream out = new FileOutputStream(tempFile)) {
        IOUtils.copy(in, out);
    }

    // Update the font for the operating system
    String newFont = getFontForOS();
    if (newFont.compareToIgnoreCase(DEFAULT_FONT) != 0) {
        BufferedReader br = new BufferedReader(new FileReader(tempFile));
        try {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line != null) {
                sb.append(line.replace(DEFAULT_FONT, newFont));
                sb.append("\n");
                line = br.readLine();
            }
            try {
                FileWriter fileWriter = new FileWriter(tempFile);
                fileWriter.write(sb.toString());
                fileWriter.flush();
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } finally {
            br.close();
        }
    }
    return tempFile;
}

From source file:jeplus.TRNSYSWinTools.java

/**
 * Copy a file from one location to another
 *
 * @param from The source file to be copied
 * @param to The target file to write// w  w  w .j a va  2 s  .  co  m
 * @return Successful or not
 */
public static boolean fileCopy(String from, String to) {
    boolean success = true;
    try {
        FileReader in = new FileReader(from);
        FileWriter out = new FileWriter(to);
        int c;
        while ((c = in.read()) != -1) {
            out.write(c);
        }
        in.close();
        out.close();
    } catch (Exception ee) {
        logger.error("Error copying " + from + " to " + to, ee);
        success = false;
    }
    return success;
}

From source file:foss.filemanager.core.Utils.java

public static void transform(File input, File output, Charset dstCharset) throws IOException {
    BufferedReader br = null;//ww  w  .ja  va 2s . co m
    FileWriter fileWriter = new FileWriter(output);
    try {
        String sCurrentLine;
        br = new BufferedReader(new FileReader(input));
        int i = 0;
        while ((sCurrentLine = br.readLine()) != null) {
            //Charset srcCharset = Charset.forName("UTF-8");
            InputStream is = new FileInputStream(input);
            Charset srcCharset = Charset.forName(guessEncoding(is));
            byte[] isoB = encode(sCurrentLine.getBytes(), srcCharset, dstCharset);
            fileWriter.write(new String(isoB, dstCharset));
            fileWriter.write("\n");
            System.out.println(i++);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            fileWriter.flush();
            fileWriter.close();
            if (br != null)
                br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

From source file:egovframework.rte.fdl.filehandling.EgovFileUtil.java

/**
 * <p>/*w  ww .  jav  a 2s  .  co m*/
 * ? ? ? .
 * </p>
 * @param file
 *        <code>File</code>
 * @param text
 *        <code>String</code>
 */
public static void writeFile(File file, String text) {
    FileWriter writer = null;

    try {
        writer = new FileWriter(file);
        writer.write(text);
    } catch (Exception e) {
        log.error("Error creating File: " + file.getName() + ":" + e);
        return;
    } finally {
        try {
            writer.close();
        } catch (Exception e) {

        }
    }
}

From source file:com.ieasy.basic.util.file.FileUtils.java

/**
 * //from  w  ww  .  ja  v  a 2 s .  c  o m
 * 
 * @param content
 * @param filePath
 */
public static void writeFile(String content, String filePath) {
    try {
        if (FileUtils.createFile(filePath)) {
            FileWriter fileWriter = new FileWriter(filePath, true);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write(content);
            bufferedWriter.close();
            fileWriter.close();
        } else {
            logger.debug("??");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:edu.kit.dama.util.SystemUtils.java

/**
 * Generate the chgrp script for Unix systems.
 *
 * @return The full path to the chgrp script.
 *
 * @throws IOException If the generation fails.
 *///w  w  w  .j  ava  2s  .c o m
private static String generateChgrpScript() throws IOException {
    String scriptFile;
    FileWriter fout = null;
    try {
        scriptFile = FilenameUtils.concat(System.getProperty("java.io.tmpdir"), "chgrpFolder.sh");
        fout = new FileWriter(scriptFile, false);
        StringBuilder b = new StringBuilder();
        b.append("#!/bin/sh\n");
        b.append("echo Changing ownership of $2 to $1\n");
        b.append("chgrp $1 $2 -R\n");
        LOGGER.debug("Writing script data to '{}'", scriptFile);
        fout.write(b.toString());
        fout.flush();
    } finally {
        if (fout != null) {
            try {
                fout.close();
            } catch (IOException ioe) {//ignore
            }
        }
    }
    return scriptFile;
}

From source file:edu.kit.dama.util.SystemUtils.java

/**
 * Generate the open script for Unix systems.
 *
 * @return The full path to the open script.
 *
 * @throws IOException If the generation fails.
 *//*ww w  .  j  a  v a  2s.  co m*/
private static String generateOpenScript() throws IOException {
    String scriptFile;
    FileWriter fout = null;
    try {
        scriptFile = FilenameUtils.concat(System.getProperty("java.io.tmpdir"), "stickFolder.sh");
        fout = new FileWriter(scriptFile, false);
        StringBuilder b = new StringBuilder();
        b.append("#!/bin/sh\n");
        b.append("echo Changing access of $1 to 2770\n");
        b.append("chmod 2770 $1 -R\n");
        LOGGER.debug("Writing script data to '{}'", scriptFile);
        fout.write(b.toString());
        fout.flush();
    } finally {
        if (fout != null) {
            try {
                fout.close();
            } catch (IOException ioe) {//ignore
            }
        }
    }
    return scriptFile;
}

From source file:io.github.casnix.mcdropshop.util.configsys.Shops.java

public static boolean checkShopsFile() {
    File shopsFile = new File("./plugins/mcDropShop/Shops.json");

    if (!shopsFile.exists())
        return false;

    try {//from   w ww .  jav  a  2 s  .  c  o  m
        String configTable = new String(Files.readAllBytes(Paths.get("./plugins/mcDropShop/Shops.json")));

        JSONParser parser = new JSONParser();

        Object obj = parser.parse(configTable);

        JSONObject jsonObj = (JSONObject) obj;

        String listVersion = (String) jsonObj.get("listVersion");

        if (listVersion == null || !listVersion.equals("0.2.0")) {
            // Old version
            jsonObj.put("listVersion", "0.2.0");

            // Update Shops.json
            FileWriter shopsJSON = new FileWriter("./plugins/mcDropShop/Shops.json");

            shopsJSON.write(jsonObj.toJSONString());

            shopsJSON.flush();
            shopsJSON.close();
        }

        return true;
    } catch (Exception e) {
        Bukkit.getLogger().severe("mcDropShop failed on setup check in Shops.json");

        e.printStackTrace();

        return false;
    }
}