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:edu.clemson.lph.utils.CSVParserWrapper.java

private static void stripNewLines(File fIn, File fOut) {
    FileReader rIn = null;
    FileWriter wOut = null;/*from w w w  .j  a  v a 2  s.  c  o m*/
    try {
        rIn = new FileReader(fIn);
        wOut = new FileWriter(fOut);
        int iQuoteCount = 0;
        char cLast = '\0';
        char cThis = '\0';
        int iThis = rIn.read();
        while (iThis >= 0) {
            cThis = (char) iThis;
            if (cThis == '\"') {
                iQuoteCount++;
            }
            if (cThis == '\n' || cThis == '\r') {
                if ((iQuoteCount % 2) > 0) {
                    //               System.err.println("Removed new line after " + iQuoteCount + " quotes");
                    //               new Exception("Removed new line after " + iQuoteCount + " quotes").printStackTrace();
                    //               System.exit(1);
                    cThis = ' ';
                } else {
                    iQuoteCount = 0;
                }
            }
            wOut.append(cThis);
            cLast = cThis;
            iThis = rIn.read();
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        logger.error(e);
    } finally {
        try {
            if (rIn != null)
                rIn.close();
            if (wOut != null) {
                wOut.flush();
                wOut.close();
            }
        } catch (IOException e) {
            logger.error(e);
        }
    }
}

From source file:com.znsx.cms.service.impl.LicenseManagerImpl.java

/**
 * ??//www .jav  a 2 s  . co  m
 * 
 * @return ?
 * @throws BusinessException
 * @author huangbuji
 *         <p />
 *         Create at 2013 ?7:32:39
 */
private String getPublicKey() throws BusinessException {
    try {
        String path = LicenceUtil.class.getResource("").getPath();
        File publicFile = new File(path + "/public.ky");
        FileReader fr = new FileReader(publicFile);
        BufferedReader br = new BufferedReader(fr);
        StringBuffer sb = new StringBuffer();
        String temp = null;
        while ((temp = br.readLine()) != null) {
            sb.append(temp);
        }
        br.close();
        fr.close();
        return sb.toString();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        throw new BusinessException(ErrorCode.ERROR, "public.ky not found !");
    } catch (IOException e) {
        e.printStackTrace();
        throw new BusinessException(ErrorCode.ERROR, "public.ky read IOException !");
    }
}

From source file:cl.cla.web.firma.servlet.ServletDetalleDocumentoPdf.java

private String generarBase64(String nombreArchivo) {
    if (nombreArchivo.isEmpty()) {
        Properties prop = new Properties();

        try {/*from  ww  w  . ja  v  a2  s  .  c o m*/
            prop.load(getServletContext().getResourceAsStream("/WEB-INF/properties/servlet.properties"));
            System.out.println(prop.getProperty("pdfVacio"));
            return prop.getProperty("pdfVacio");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    } else {
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader(nombreArchivo);
            br = new BufferedReader(fr);
            br = new BufferedReader(new FileReader(nombreArchivo));
            return br.readLine();
        } catch (IOException ex) {
            Logger.getLogger(ServletDetalleDocumentoPdf.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if (fr != null) {
                    fr.close();
                }
                if (br != null) {
                    br.close();
                }
            } catch (IOException ex) {
                Logger.getLogger(ServletDetalleDocumentoPdf.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    return "";
}

From source file:com.apkcategorychecker.writer.WriterCSV.java

private void removeBlankLines(String _csvPath) {
    try {/*from www.j  ava  2s.c  om*/
        FileReader fr = new FileReader(_csvPath);
        BufferedReader br = new BufferedReader(fr);
        FileWriter fw = new FileWriter(_csvPath + ".temp");
        String line;

        while ((line = br.readLine()) != null) {
            //line = line.trim(); // remove leading and trailing whitespace
            if (line.length() != 0) // don't write out blank lines
            {
                fw.write(line, 0, line.length());
                fw.append("\r");
            }
        }
        fr.close();
        fw.close();
        File old = new File(_csvPath);
        old.delete();
        File clean = new File(_csvPath + ".temp");
        clean.renameTo(old);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:hu.petabyte.redflags.engine.tedintf.MaxNumberDeterminer.java

private void loadMaxNumCache() {
    try {/*from  w ww  .ja va 2 s . co  m*/
        FileReader r = new FileReader(CACHE_FILENAME);
        Properties p = new Properties();
        p.load(r);
        for (Entry<Object, Object> e : p.entrySet()) {
            if (e.getKey().toString().matches("\\d+") && e.getValue().toString().matches("\\d+")) {
                int y = Integer.valueOf(e.getKey().toString());
                int n = Integer.valueOf(e.getValue().toString());
                maxNumCache.put(y, n);
            }
        }
        r.close();
        LOG.debug("Max number cache loaded: {}", maxNumCache);
    } catch (IOException e) {
        LOG.warn("Failed to load max number cache");
        LOG.trace("Failed to load max number cache", e);
    }
}

From source file:com.digitalgeneralists.assurance.UnitTestUtils.java

public String readTestFileContents(File file) throws FileNotFoundException, IOException {
    String contents = null;// w w w .  j  av  a 2s.com

    FileReader fr = new FileReader(file.getAbsoluteFile());
    BufferedReader br = new BufferedReader(fr);
    String currentLine = null;
    while ((currentLine = br.readLine()) != null) {
        contents += currentLine;
    }
    br.close();
    fr.close();

    return contents;
}

From source file:it.geosolutions.geobatch.task.TaskExecutor.java

/**
 * /*  ww  w .jav  a 2  s . co  m*/
 * @param defaultScriptPath
 * @param tagName
 * @return
 * @throws IOException
 */
private static String getScriptArguments(final String defaultScriptPath, final String tagName)
        throws IOException {
    String value = null;

    // Create FileReader Object
    FileReader inputFileReader = new FileReader(defaultScriptPath);

    try {
        // Create Buffered/PrintWriter Objects
        BufferedReader inputStream = new BufferedReader(inputFileReader);

        String inLine = null;

        while ((inLine = inputStream.readLine()) != null) {
            // Handle KeyWords

            if (inLine.trim().startsWith("<" + tagName + ">")) {
                if (inLine.trim().endsWith("</" + tagName + ">")) {
                    int beginIndex = inLine.indexOf("<" + tagName + ">") + ("<" + tagName + ">").length();
                    int endIndex = inLine.length() - ("</" + tagName + ">").length();
                    value = inLine.substring(beginIndex, endIndex);
                } else {
                    while ((inLine = inputStream.readLine()) != null) {
                        if (!inLine.trim().endsWith("</" + tagName + ">"))
                            value = inLine;
                        else
                            break;
                    }
                }
            }
        }

    } catch (IOException e) {
    } finally {
        inputFileReader.close();
    }

    return value;
}

From source file:org.jts.docGenerator.PathInserter.java

/**
 *
 * @param hrefs String representations of hrefs for which paths need to be
 * inserted. it is assumed that file names in href are placed in the
 * destination root directory.// w w  w. j  ava2s  . c  om
 *
 */
void insertPaths(List<String> hrefs) {

    // get all HTML files at or under dest dir
    Collection<File> htmlFiles = FileUtils.listFiles(dest, FileFilterUtils.suffixFileFilter(".html"),
            TrueFileFilter.INSTANCE);

    FileReader reader = null;
    FileWriter writer = null;

    for (File htmlFile : htmlFiles) {

        try {
            reader = new FileReader(htmlFile);
        } catch (FileNotFoundException fnfe) {
            fnfe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        char[] buf = new char[(int) htmlFile.length()];

        try {
            reader.read(buf);
            reader.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        StringBuilder builder = new StringBuilder();
        builder = builder.append(buf);

        // insert paths to specified hrefs
        for (int jj = 0; jj < hrefs.size(); jj++) {
            String path = getPath(htmlFile);
            String href = (String) hrefs.get(jj);
            int offset = 0;

            // find all incidences of href and insert path modification
            while ((offset = builder.indexOf(href, offset)) != -1) {
                builder = builder.insert(offset, path);
                offset += path.length() + href.length();
            }
        }

        try {
            writer = new FileWriter(htmlFile);
            writer.write(builder.toString().toCharArray());
            writer.close();
        } catch (FileNotFoundException fnfe) {
            fnfe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

From source file:org.apache.mahout.knn.lsh.LocalitySensitiveHashTest.java

private List<Vector> readInputFile(String fileName) throws Exception {
    List<Vector> inputList = Lists.newArrayList();
    FileReader fileReader = new FileReader(new File(fileName));
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    String line;/*w w  w  .j av a 2  s  . c o  m*/
    String[] values = bufferedReader.readLine().split(",");
    double[] doubleValues = new double[values.length - 1];
    while ((line = bufferedReader.readLine()) != null) {
        values = line.split(",");
        for (int i = 0; i < doubleValues.length; i++) {
            doubleValues[i] = Double.parseDouble(values[i + 1]);
        }
        inputList.add(new DenseVector(doubleValues));
    }
    fileReader.close();
    return inputList;
}

From source file:net.jforum.util.JDBCLoader.java

/**
 * The sql file to load, relative to the classpath
 * @param sqlfile//  ww  w  . j av  a2 s . c o m
 */
public void run(String sqlfile) {
    BufferedReader reader = null;
    FileReader fileReader = null;

    try {
        fileReader = new FileReader(this.getClass().getResource(sqlfile).getFile());
        reader = new BufferedReader(fileReader);

        String line = null;

        while ((line = reader.readLine()) != null) {
            if (!StringUtils.isEmpty(line)) {
                logger.debug("JDBCLoader: [Running] " + line);
                this.runStatement(line);
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (fileReader != null) {
            try {
                fileReader.close();
            } catch (Exception e) {
            }
        }

        if (reader != null) {
            try {
                reader.close();
            } catch (Exception e) {
            }
        }
    }
}