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:de.viaboxx.nlstools.tasks.CopyBundlesTask.java

/**
 * @param controlFile/* w  w  w  .j a v  a2 s .c  o m*/
 * @return key = plain name.xml, value = xml-bundle file
 * @throws IOException
 */
static Map<String, File> readControlFileMapping(File controlFile) throws IOException {
    FileReader reader = new FileReader(controlFile);
    List<String> controlFileContent = IOUtils.readLines(reader);
    reader.close();

    Map<String, File> controlFileMapping = new HashMap(controlFileContent.size());
    for (String line : controlFileContent) {
        line = line.trim();
        if (line.length() > 0) {
            int i = line.lastIndexOf('/');
            String name = line.substring(i + 1);
            controlFileMapping.put(name, new File(controlFile.getParent(), line));
        }
    }
    return controlFileMapping;
}

From source file:Main.java

public static String readToString(File file) throws IOException {
    if (file == null || !file.exists()) {
        return null;
    }// w w  w.ja v  a  2s  . c o m
    FileReader reader = new FileReader(file);
    StringBuilder out = new StringBuilder();
    char[] buffer = new char[1024 * 4];
    int numRead = 0;
    while ((numRead = reader.read(buffer)) > -1) {
        out.append(String.valueOf(buffer, 0, numRead));
    }
    reader.close();
    return out.toString();
}

From source file:org.glite.slcs.pki.CertificateRequest.java

/**
 * //from  w ww.  j  av a  2s  .  c o  m
 * @param file
 * @return
 * @throws IOException
 * @throws GeneralSecurityException
 */
static public CertificateRequest loadPEM(File file) throws IOException, GeneralSecurityException {
    FileReader reader = new FileReader(file);
    CertificateRequest csr = readPEM(reader);
    try {
        reader.close();
    } catch (IOException e) {
        LOG.warn(e);
    }
    return csr;
}

From source file:Main.java

public static double[] read_array(String filename) throws Exception {
    FileReader f = new FileReader(filename);
    BufferedReader inp = new BufferedReader(f);

    String str = inp.readLine();// w  w  w . j  av a2  s. c  o  m
    int nl = 0;
    while (str != null) {
        nl++;
        str = inp.readLine();
    }

    inp.close();
    f.close();

    double[] res = new double[nl];
    f = new FileReader(filename);
    inp = new BufferedReader(f);
    for (int i = 0; i < nl; i++) {
        str = inp.readLine();
        res[i] = Double.valueOf(str).doubleValue();
    }

    inp.close();
    f.close();

    return res;
}

From source file:com.amalto.workbench.utils.ResourcesUtil.java

public static void postResourcesFromFile(String typeName, String pathName, String uri) throws Exception {
    File file = new File(pathName);
    FileReader fr = new FileReader(file);
    BufferedReader br = new BufferedReader(fr);
    postResourcesFromContentString(br.readLine(), uri, typeName);
    br.close();//from  www  . java  2  s. c o  m
    fr.close();

}

From source file:org.apache.flink.streaming.util.TestDataUtil.java

public static void downloadIfNotExists(String fileName) {

    File file = new File(testDataDir + fileName);
    File checkFile = new File(testChekSumDir + fileName + ".md5");
    String checkSumDesired = new String();
    String checkSumActaul = new String();

    File testDataDirectory = new File(testDataDir);
    testDataDirectory.mkdirs();//from ww  w  .  ja va2 s .co  m

    try {
        FileReader fileReader = new FileReader(checkFile);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        checkSumDesired = bufferedReader.readLine();
        bufferedReader.close();
        fileReader.close();
    } catch (FileNotFoundException e) {
        throw new RuntimeException("File not found: " + file.getAbsolutePath(), e);
    } catch (IOException e) {
        throw new RuntimeException("Cannot read file: " + file.getAbsolutePath(), e);
    }

    if (file.exists()) {
        if (LOG.isInfoEnabled()) {
            LOG.info(fileName + " already exists.");
        }

        try {
            checkSumActaul = DigestUtils.md5Hex(FileUtils.readFileToByteArray(file));
        } catch (IOException e) {
            throw new RuntimeException("Cannot read file to byte array: " + file.getAbsolutePath(), e);
        }
        if (!checkSumActaul.equals(checkSumDesired)) {
            if (LOG.isInfoEnabled()) {
                LOG.info("Checksum is incorrect.");
                LOG.info("Downloading file.");
            }
            download(fileName);
        }
    } else {
        if (LOG.isInfoEnabled()) {
            LOG.info("File does not exist.");
            LOG.info("Downloading file.");
        }
        download(fileName);
    }

}

From source file:Main.java

public static String getStringFromFile(String file) throws IOException {
    char[] data = null;
    FileReader fr = null;
    try {//from  w w  w  .  jav  a  2  s. c o m
        File f = new File(file);
        data = new char[(int) f.length()];
        fr = new FileReader(f);
        fr.read(data);
    } catch (IOException e) {
        throw e;
    } finally {
        if (fr != null) {
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return new String(data);
}

From source file:Main.java

public static char[] getChars(File file) throws IOException {
    String str;//from   w w w.  ja v a  2  s  . c o m

    FileReader reader = new FileReader(file);
    BufferedReader buffer = new BufferedReader(reader);
    String line;
    StringBuilder result = new StringBuilder();
    while ((line = buffer.readLine()) != null) {
        result.append(line).append("\n");
    }
    str = result.toString();
    buffer.close();
    reader.close();

    return str.toCharArray();
}

From source file:com.npower.unicom.sync.RequestHeader.java

/**
 * //from  ww  w  . j  av  a 2  s.  c o m
 * @param file
 * @return
 * @throws IOException
 */
public static RequestHeader parseHeaderInfo(File file) throws IOException {
    // 
    FileReader in = new FileReader(file);
    BufferedReader reader = new BufferedReader(in);
    RequestHeader header = parseHeaderInfo(reader);
    in.close();
    // 
    header.setFile(file);
    return header;
}

From source file:org.kuali.kfs.gl.batch.BatchSortUtil.java

private static void mergeFiles(File tempSortDir, int numFiles, String outputFileName,
        Comparator<String> comparator) {
    try {//from ww  w.  jav  a2 s  . c  o m
        ArrayList<FileReader> mergefr = new ArrayList<FileReader>(numFiles);
        ArrayList<BufferedReader> mergefbr = new ArrayList<BufferedReader>(numFiles);
        // temp buffer for writing - contains the minimum record from each file
        ArrayList<String> fileRows = new ArrayList<String>(numFiles);

        BufferedWriter bw = new BufferedWriter(new FileWriter(outputFileName));
        //LOG.info("Successfully opened output file " + outputFileName);

        boolean someFileStillHasRows = false;

        // Iterate over all the files, getting the first line in each file
        for (int i = 0; i < numFiles; i++) {
            // open a file reader for each file
            mergefr.add(new FileReader(new File(tempSortDir, "chunk_" + i)));
            mergefbr.add(new BufferedReader(mergefr.get(i)));

            // get the first row
            String line = mergefbr.get(i).readLine();
            if (line != null) {
                fileRows.add(line);
                someFileStillHasRows = true;
            } else {
                fileRows.add(null);
            }
        }

        while (someFileStillHasRows) {
            String min = null;
            int minIndex = 0; // index of the file with the minimum record

            // init for later compare - assume the first file has the minimum
            String line = fileRows.get(0);
            if (line != null) {
                min = line;
                minIndex = 0;
            } else {
                min = null;
                minIndex = -1;
            }

            // determine the minimum record of the top lines of each file
            // check which one is min
            for (int i = 1; i < fileRows.size(); i++) {
                line = fileRows.get(i);
                if (line != null) {
                    if (min != null) {
                        if (comparator.compare(line, min) < 0) {
                            minIndex = i;
                            min = line;
                        }
                    } else {
                        min = line;
                        minIndex = i;
                    }
                }
            }

            if (minIndex < 0) {
                someFileStillHasRows = false;
            } else {
                // write to the sorted file
                bw.append(fileRows.get(minIndex)).append('\n');

                // get another row from the file that had the min
                line = mergefbr.get(minIndex).readLine();
                if (line != null) {
                    fileRows.set(minIndex, line);
                } else { // file is out of rows, set to null so it is ignored
                    fileRows.set(minIndex, null);
                }
            }
            // check if one still has rows
            for (int i = 0; i < fileRows.size(); i++) {
                someFileStillHasRows = false;
                if (fileRows.get(i) != null) {
                    if (minIndex < 0) {
                        throw new RuntimeException(
                                "minIndex < 0 and row found in chunk file " + i + " : " + fileRows.get(i));
                    }
                    someFileStillHasRows = true;
                    break;
                }
            }

            // check the actual files one more time
            if (!someFileStillHasRows) {
                //write the last one not covered above
                for (int i = 0; i < fileRows.size(); i++) {
                    if (fileRows.get(i) == null) {
                        line = mergefbr.get(i).readLine();
                        if (line != null) {
                            someFileStillHasRows = true;
                            fileRows.set(i, line);
                        }
                    }
                }
            }
        }

        // close all the files
        bw.close();
        //LOG.info("Successfully closed output file " + outputFileName);

        for (BufferedReader br : mergefbr) {
            br.close();
        }
        for (FileReader fr : mergefr) {
            fr.close();
        }
    } catch (Exception ex) {
        LOG.error("Exception merging the sorted files", ex);
        throw new RuntimeException("Exception merging the sorted files", ex);
    }
}