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 String xmlFileToString(String path) {
    FileReader reader = null;
    StringBuilder sb = new StringBuilder();
    char[] buf = new char[1024];
    try {/*from  w w  w. j  a va  2  s.  c  om*/
        reader = new FileReader(path);
        int temp = 0;
        while ((temp = reader.read(buf)) != -1) {
            sb.append(String.valueOf(buf, 0, temp));
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

From source file:com.linkedin.databus2.client.util.CheckpointSerializerMain.java

private static Properties loadProperties(String fileName) throws IOException {
    Properties result = new Properties();
    FileReader freader = new FileReader(fileName);
    try {/*from  w  ww  .  j  a v  a  2 s. c  om*/
        result.load(freader);
    } finally {
        freader.close();
    }

    return result;
}

From source file:fi.helsinki.cs.iot.kahvihub.conf.ConfigurationFileParser.java

public static HubConfig parseConfigurationFile(String filename)
        throws ConfigurationParsingException, IOException {
    FileReader fileReader = new FileReader(filename);
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    StringBuilder stringBuilder = new StringBuilder();
    String line = null;/*from  ww w.  j  a  v  a  2s .  c o  m*/
    while ((line = bufferedReader.readLine()) != null) {
        stringBuilder.append(line);
    }
    bufferedReader.close();
    fileReader.close();

    JSONObject root = null;
    try {
        root = new JSONObject(stringBuilder.toString());
    } catch (JSONException e) {
        throw new ConfigurationParsingException("The configuration file is not a proper JSON object");
    }

    String name = getStringProperty(root, "name");
    int port = getIntProperty(root, "port");
    String host = getStringProperty(root, "host");
    String libdir = getStringProperty(root, "libdir");
    String logdir = getStringProperty(root, "logdir");
    String dbdir = getStringProperty(root, "dbdir");
    String dbname = getStringProperty(root, "dbname");
    int dbversion = getIntProperty(root, "dbversion");
    boolean debugMode = getBooleanProperty(root, "debug");

    return new HubConfig(name, host, port, libdir, logdir, dbdir, dbname, dbversion, debugMode);
}

From source file:net.ytbolg.mcxa.ForgeCheck.java

static String ReadFile(String path) throws FileNotFoundException, IOException {

    File file = new File(path);
    FileReader r = new FileReader(file);
    char c[] = new char[(int) file.length()];
    r.read(c);//from w  ww .j a  v  a2 s.  c  o  m

    r.close();
    return String.valueOf(c);
}

From source file:com.flipkart.flux.examples.WorkflowExecutionDemo.java

/**
 * Does necessary actions to run an example/user's workflow.
 * @param moduleName name of user module in which workflow code is present
 * @param workflowClassFQN fully qualified name of main class which triggers user workflow execution at client side.
 *                         @see com.flipkart.flux.examples.concurrent.RunEmailMarketingWorkflow for example.
 * @param configFileName "flux_config.yml" which contains workflow related configuration.
 *                       @see flux/examples/src/main/resources/flux_config.yml for example.
 * @throws Exception//from   w w w  . j  a  v  a  2  s .  com
 */
private static void runExample(String moduleName, String workflowClassFQN, String configFileName,
        String mavenPath) throws Exception {

    //copy dependencies to module's target directory
    executeCommand(mavenPath + " -pl " + moduleName
            + " -q package dependency:copy-dependencies -DincludeScope=runtime -DskipTests");

    //get deployment path from configuration.yml
    FileReader reader = new FileReader(
            WorkflowExecutionDemo.class.getResource("/packaged/configuration.yml").getFile());
    String deploymentUnitsPath = (String) ((Map) new Yaml().load(reader)).get("deploymentUnitsPath");
    if (!deploymentUnitsPath.endsWith("/")) {
        deploymentUnitsPath = deploymentUnitsPath + "/";
    }
    reader.close();

    //create deployment structure
    String deploymentUnitName = "DU1/1";
    String mainDirPath = deploymentUnitsPath + deploymentUnitName + "/main";
    String libDirPath = deploymentUnitsPath + deploymentUnitName + "/lib";
    executeCommand("mkdir -p " + mainDirPath);
    executeCommand("mkdir -p " + libDirPath);

    //copy dependencies to deployment unit
    FileUtils.copyFile(
            new File(moduleName + "/target/")
                    .listFiles((FilenameFilter) new WildcardFileFilter(moduleName + "*.jar"))[0],
            new File(mainDirPath + "/" + moduleName + ".jar"));
    FileUtils.copyDirectory(new File(moduleName + "/target/dependency"), new File(libDirPath));
    FileUtils.copyFile(new File(moduleName + "/src/main/resources/" + configFileName),
            new File(deploymentUnitsPath + deploymentUnitName + "/flux_config.yml"));

    //start flux runtime
    FluxInitializer.main(new String[] {});

    //Invoke workflow in separate process, the below system out prints this process's output in blue color
    System.out.println((char) 27 + "[34m" + executeCommand(
            "java -cp " + moduleName + "/target/*:" + moduleName + "/target/dependency/* " + workflowClassFQN)
            + (char) 27 + "[0m");
}

From source file:Main.java

/**
 * Read the contents of a file and place them in
 * a string object./*from w w w  . ja va  2 s.  co m*/
 *
 * @param file path to file.
 * @return String contents of the file.
 */
public static String fileContentsToString(String file) {
    String contents = "";

    File f = null;
    try {
        f = new File(file);

        if (f.exists()) {
            FileReader fr = null;
            try {
                fr = new FileReader(f);
                char[] template = new char[(int) f.length()];
                fr.read(template);
                contents = new String(template);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fr != null) {
                    fr.close();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return contents;
}

From source file:index.IncrementIndex.java

public static String getStoreId(String path) {
    String storeId = "";
    try {//  ww w .j a  v  a 2  s  .c  o  m
        File file = new File(path);
        if (!file.exists()) {
            file.createNewFile();
        }
        FileReader fr = new FileReader(path);
        BufferedReader br = new BufferedReader(fr);
        storeId = br.readLine();
        if (storeId == null || storeId == "")
            storeId = "0";
        br.close();
        fr.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return storeId;
}

From source file:com.thinkit.operationsys.util.FileUtil.java

public static void copyFile(String src, String dest) {
    System.out.println("copy");
    File f1 = new File(src);
    File f2 = new File(dest);
    //int b=0;//w  ww  .j  a v  a  2 s .c  om
    String line = "";
    try {
        FileReader reader = new FileReader(f1);
        FileWriter writer = new FileWriter(f2);
        BufferedReader br = new BufferedReader(reader);
        BufferedWriter bw = new BufferedWriter(writer);
        while ((line = br.readLine()) != null) {
            System.out.println(line);
            bw.write(line);
            bw.newLine();
            bw.flush();
        }
        reader.close();
        writer.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:com.docudile.app.services.utils.FileHandler.java

public static Map<String, List<String>> readAllFiles(String folder) throws IOException {
    ArrayList<String> filenames = getFilePaths(folder);
    Map<String, List<String>> files = new HashMap<>();

    for (String filename : filenames) {
        FileReader fr = new FileReader(folder + "/" + filename);
        BufferedReader br = new BufferedReader(fr);
        ArrayList<String> lines = new ArrayList<String>();
        String temp;/* w ww .java 2 s .c  o m*/

        while ((temp = br.readLine()) != null) {
            if (StringUtils.isNotEmpty(temp)) {
                lines.add(temp);
            }
        }
        files.put(filename.split("\\.")[0], lines);
        br.close();
        fr.close();
    }
    return files;
}

From source file:com.endofhope.neurasthenia.bayeux.BayeuxMessage.java

private static String getString(String location) throws IOException {
    StringBuilder sb = new StringBuilder();
    FileReader fr = new FileReader(location);
    int oneInt = -1;
    while (-1 != (oneInt = fr.read())) {
        sb.append((char) oneInt);
    }// ww  w  . j av  a  2  s  .  co m
    fr.close();
    return sb.toString();
}