Example usage for java.io FileReader close

List of usage examples for java.io FileReader close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Usage

From source file:Main.java

public static void closeStaxStreamReader(XMLStreamReader xsr, FileReader fr) {
    try {//from   ww w . java 2  s  .c o m
        if (xsr != null) {
            xsr.close();
        }
        if (fr != null) {
            fr.close();
        }
    } catch (XMLStreamException ex) {
        //
    } catch (IOException ex) {
        //
    }
}

From source file:Main.java

/**
 * Convenient call to load a properties file from the provided file
 *///from w ww. java  2 s.com
public static Properties loadProperties(File file) throws IOException {
    if (file == null)
        return null;

    FileReader reader = new FileReader(file);
    try {
        return loadProperties(reader);
    } finally {
        reader.close();
    }
}

From source file:Main.java

public static String readFile(String filename) {
    String content = null;/*from  w  w w . ja  v a  2s  . c  o  m*/
    File file = new File(filename); //for ex foo.txt
    try {
        FileReader reader = new FileReader(file);
        char[] chars = new char[(int) file.length()];
        reader.read(chars);
        content = new String(chars);
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return content;
}

From source file:net.firejack.platform.core.utils.MiscUtils.java

/**
 * @param properties/*from   www. j  a  va  2s  .c  om*/
 * @param append
 * @throws java.io.IOException
 */
public static void setProperties(File properties, Map<String, String> append) throws IOException {
    Properties props = new Properties();
    if (properties.exists()) {
        FileReader reader = new FileReader(properties);
        props.load(reader);
        reader.close();
    } else {
        FileUtils.forceMkdir(properties.getParentFile());
    }

    if (properties.exists() || properties.createNewFile()) {
        props.putAll(append);
        FileWriter writer = new FileWriter(properties);
        props.store(writer, null);
        writer.flush();
        writer.close();
    }
}

From source file:net.firejack.platform.core.utils.MiscUtils.java

/**
 * @param properties/*  w  w  w . j ava 2 s.  c  o  m*/
 * @param checkFileExists
 * @param property
 * @param value
 * @throws java.io.IOException
 */
public static void setProperties(File properties, boolean checkFileExists, String property, String value)
        throws IOException {
    if (checkFileExists && !properties.exists()) {
        logger.error("Properties file [" + properties.getAbsolutePath() + "] does not exist.");
        throw new FileNotFoundException("Properties file does not found.");
        //            IOHelper.delete(dbProperties);
    }
    Properties props = new Properties();
    if (properties.exists()) {
        FileReader reader = new FileReader(properties);
        props.load(reader);
        reader.close();
    }
    props.put(property, value);
    FileWriter writer = new FileWriter(properties);
    props.store(writer, null);
    writer.flush();
    writer.close();
}

From source file:net.feed_the_beast.launcher.json.JsonFactory.java

public static Version loadVersion(File json) throws JsonSyntaxException, JsonIOException, IOException {
    FileReader reader = new FileReader(json);
    Version v = GSON.fromJson(reader, Version.class);
    reader.close();
    return v;//  w w w .j  ava2s.  co  m
}

From source file:net.feed_the_beast.launcher.json.JsonFactory.java

public static AssetIndex loadAssetIndex(File json) throws JsonSyntaxException, JsonIOException, IOException {
    FileReader reader = new FileReader(json);
    AssetIndex a = GSON.fromJson(reader, AssetIndex.class);
    reader.close();
    return a;//w  ww . ja  v  a  2s  . c  o m
}

From source file:JSONUtil.java

/**
 * @param string/*from  www  .j  a  v a2 s  . c  o m*/
 * @return
 * @throws IOException
 * @throws JSONException
 */
public static JSONObject loadJson(String file) throws IOException, JSONException {
    File f = new File(file);
    FileReader fr = new FileReader(f);
    JSONTokener j = new JSONTokener(fr);
    JSONObject in = new JSONObject(j);
    fr.close();
    return in;
}

From source file:com.norconex.jefmon.server.JEFMonServer.java

private static Properties getSetupProperties() throws IOException {
    File setupFile = new File(CONFIG_PROPERTIES);
    if (!setupFile.exists()) {
        System.err.println("Missing file " + CONFIG_PROPERTIES);
        System.exit(-1);/*w w w .j a  va  2 s  . com*/
    }
    Properties props = new Properties();
    FileReader reader = new FileReader(setupFile);
    props.load(reader);
    reader.close();
    return props;
}

From source file:Main.java

/**
 * Load a text file contents as a <code>String<code>.
 * This method does not perform enconding conversions
 *
 * @param file The input file/*w  ww. ja v  a  2 s .c  om*/
 * @return The file contents as a <code>String</code>
 * @exception IOException IO Error
 */
public static String deserializeString(File file) throws IOException {
    int len;
    char[] chr = new char[4096];
    final StringBuffer buffer = new StringBuffer();
    final FileReader reader = new FileReader(file);
    try {
        while ((len = reader.read(chr)) > 0) {
            buffer.append(chr, 0, len);
        }
    } finally {
        reader.close();
    }
    return buffer.toString();
}