Example usage for java.io Writer close

List of usage examples for java.io Writer close

Introduction

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

Prototype

public abstract void close() throws IOException;

Source Link

Document

Closes the stream, flushing it first.

Usage

From source file:com.iksgmbh.sql.pojomemodb.utils.FileUtil.java

public static void appendToFile(final File file, final String text) throws IOException {
    final String oldFileContent;
    final String newFileContent;

    if (file.exists()) {
        oldFileContent = getFileContent(file);
    } else {//from   ww w .ja  va 2  s  .co  m
        oldFileContent = "";
    }

    if (oldFileContent.length() > 0) {
        newFileContent = oldFileContent + getSystemLineSeparator() + text;
    } else {
        newFileContent = text;
    }

    Writer writer = null;
    try {
        writer = IOEncodingHelper.STANDARD.getBufferedWriter(file);
        writer.write(newFileContent);
    } finally {
        if (writer != null) {
            writer.close();
        }
    }
}

From source file:cascading.util.Util.java

public static void printGraph(String filename, SimpleDirectedGraph graph) {
    try {/*w ww  .  j  a va 2  s  .c om*/
        Writer writer = new FileWriter(filename);

        printGraph(writer, graph);

        writer.close();
    } catch (IOException exception) {
        exception.printStackTrace();
    }
}

From source file:dataGen.DataGen.java

/**
 * Saves the Data/*from   w  ww .  j  av a  2s . c om*/
 * @param values
 *       The values which should be saved.
 * @param fw
 *       The File-Writer including the File location
 * @param nf
 *       How should the data get formatted?
 * @throws IOException
 *       If Stream to a File couldn't be written/closed 
 */
public static void saveData(Array2DRowRealMatrix values, Writer fw, NumberFormat nf) throws IOException {
    for (int i = 0; i < values.getRowDimension(); i++) {
        String row = i + 1 + " ";
        //String row = "";

        for (int j = 0; j < values.getColumnDimension(); j++) {
            row = row + nf.format(values.getEntry(i, j)) + " ";
        }
        fw.write(row);
        fw.append(System.getProperty("line.separator"));
    }
    fw.close();
}

From source file:com.gm.machine.util.CommonUtils.java

private synchronized static void writeHtml(String htmlFileName, String content, String encoding)
        throws Exception {
    Writer fw = null;

    try {/*from w ww  .  j  av a 2s.  co  m*/
        fw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFileName), encoding));
        fw.write(content);
        fw.flush();

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        // ?
        if (fw != null) {
            fw.close();
        }

    }

}

From source file:citibob.licensor.MakeLauncher.java

/**
 * //  w w w  . j  ava 2 s  .co  m
 * @param version
 * @param configDir
 * @param outJar
 * @param spassword
 * @throws java.lang.Exception
 */
public static void makeLauncher(String version, File configDir, File outJar, String spassword)
        throws Exception {
    File oaDir = ClassPathUtils.getMavenProjectRoot();
    File oalaunchDir = new File(oaDir, "../oalaunch");
    File oasslDir = new File(oaDir, "../oassl");
    File keyDir = new File(oasslDir, "keys/client");
    File tmpDir = new File(".", "tmp");
    FileUtils.deleteDirectory(tmpDir);
    tmpDir.mkdirs();

    // Find the oalaunch JAR file
    File oalaunchJar = null;
    File[] files = new File(oalaunchDir, "target").listFiles();
    for (File f : files) {
        if (f.getName().startsWith("oalaunch") && f.getName().endsWith(".jar")) {
            oalaunchJar = f;
            break;
        }
    }

    // Unjar the oalaunch.jar file into the temporary directory
    exec(tmpDir, "jar", "xvf", oalaunchJar.getAbsolutePath());

    File tmpOalaunchDir = new File(tmpDir, "oalaunch");
    File tmpConfigDir = new File(tmpOalaunchDir, "config");

    // Read app.properties
    Properties props = new Properties();
    InputStream in = new FileInputStream(new File(configDir, "app.properties"));
    props.load(in);
    in.close();

    // Re-do the config dir
    FileUtils.deleteDirectory(tmpConfigDir);
    FileFilter ff = new FileFilter() {
        public boolean accept(File f) {
            if (f.getName().startsWith("."))
                return false;
            if (f.getName().endsWith("~"))
                return false;
            return true;
        }
    };
    FileUtils.copyDirectory(configDir, tmpConfigDir, ff);

    // Set up to encrypt
    char[] password = null;
    PBECrypt pbe = new PBECrypt();
    if (spassword != null)
        password = spassword.toCharArray();

    // Encrypt .properties files if needed
    if (password != null) {
        for (File fin : configDir.listFiles()) {
            if (fin.getName().endsWith(".properties") || fin.getName().endsWith(".jks")) {
                File fout = new File(tmpConfigDir, fin.getName());
                pbe.encrypt(fin, fout, password);
            }
        }
    }

    // Copy the appropriate key and certificate files
    String dbUserName = props.getProperty("db.user", null);
    File[] jksFiles = new File[] { new File(keyDir, dbUserName + "-store.jks"),
            new File(keyDir, dbUserName + "-trust.jks") };
    for (File fin : jksFiles) {
        if (!fin.exists()) {
            System.out.println("Missing jks file: " + fin.getName());
            continue;
        }
        File fout = new File(tmpConfigDir, fin.getName());
        if (password != null) {
            System.out.println("Encrypting " + fin.getName());
            pbe.encrypt(fin, fout, password);
        } else {
            System.out.println("Copying " + fin.getName());
            FileUtils.copyFile(fin, fout);
        }
    }

    // Use a downloaded JNLP file, not a static one.
    new File(tmpOalaunchDir, "offstagearts.jnlp").delete();

    // Open properties file, which we will write to...
    File oalaunchProperties = new File(tmpDir, "oalaunch/oalaunch.properties");
    Writer propertiesOut = new FileWriter(oalaunchProperties);
    propertiesOut.write(
            "jnlp.template.url = " + "http://offstagearts.org/releases/offstagearts/offstagearts_oalaunch-"
                    + version + ".jnlp.template\n");
    String configName = outJar.getName();
    int dot = configName.lastIndexOf(".jar");
    if (dot >= 0)
        configName = configName.substring(0, dot);
    propertiesOut.write("config.name = " + configName + "\n");
    propertiesOut.close();

    // Jar it back up
    exec(tmpDir, "jar", "cvfm", outJar.getAbsolutePath(), "META-INF/MANIFEST.MF", ".");

    //   // Sign it
    //   exec(null, "jarsigner", "-storepass", "keyst0re", outJar.getAbsolutePath(),
    //         "offstagearts");

    // Remove the tmp directory
    FileUtils.deleteDirectory(tmpDir);
}

From source file:IOUtil.java

/**
 * Unconditionally close an <code>Writer</code>.
 * Equivalent to {@link Writer#close()}, except any exceptions will be ignored.
 *
 * @param output A (possibly null) Writer
 *///from w  w  w  .j a  v  a 2 s . c  om
public static void shutdownWriter(final Writer output) {
    if (null == output) {
        return;
    }

    try {
        output.close();
    } catch (final IOException ioe) {
    }
}

From source file:edu.brandeis.cs.planner.utils.WsdlClient.java

public static void copy(InputStream is, Writer writer) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    String line;/*from w w  w  .j  av  a  2 s  .c o m*/
    while ((line = br.readLine()) != null)
        writer.write(line);
    br.close();
    is.close();
    writer.close();
}

From source file:com.oeg.oops.VocabUtils.java

/**
 * Method to save a document on a path//from   w  w w  .j av  a2  s  .c o m
 * @param path
 * @param textToWrite
 */
public static void saveDocument(String path, String textToWrite) {
    File f = new File(path);
    Writer out = null;
    try {
        f.createNewFile();
        out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), "UTF-8"));
        out.write(textToWrite);
        out.close();
    } catch (IOException e) {
        System.err.println("Error while creating the file " + e.getMessage() + "\n" + f.getAbsolutePath());
    }

}

From source file:com.zotoh.core.io.StreamUte.java

/**
 * @param w//from   w ww .j  a v a 2 s  . c  o  m
 * @return null
 */
public static Writer close(Writer w) {
    try {
        if (w != null)
            w.close();
    } catch (Throwable t) {
    }
    return null;
}

From source file:de.akquinet.engineering.vaadinator.mojo.CodeGeneratorMojo.java

private static void runVelocity(BeanDescription desc, Map<String, Object> commonMap, String pckg,
        String modelPckg, String presenterPckg, String viewPckg, String profileName, String templateName,
        File outFile, boolean mandatory) throws IOException {
    Velocity.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
    Velocity.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
    Template template;/*  w w  w.j  av  a  2  s.co m*/
    // Issue #6: for optional templates check whether it's there
    boolean runTemplate;
    if (mandatory) {
        runTemplate = true;
    } else {
        Enumeration<URL> templateResEnum = Velocity.class.getClassLoader()
                .getResources(TEMPLATE_PACKAGE.substring(1) + templateName);
        runTemplate = templateResEnum.hasMoreElements();
    }
    if (!runTemplate) {
        return;
    }
    template = Velocity.getTemplate(TEMPLATE_PACKAGE + templateName);
    VelocityContext context = new VelocityContext();
    context.put("bean", desc);
    context.put("common", commonMap);
    context.put("package", pckg);
    context.put("modelPackage", modelPckg);
    context.put("presenterPackage", presenterPckg);
    context.put("viewPackage", viewPckg);
    context.put("profileName", profileName);
    context.put("unicodeUtil", UnicodeUtil.SINGLETON);
    Writer writer = new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8");
    template.merge(context, writer);
    writer.close();
}