List of usage examples for java.io LineNumberReader LineNumberReader
public LineNumberReader(Reader in)
From source file:org.opencms.util.CmsRfsFileViewer.java
/** * Internally sets the member <code>m_windowPos</code> to the last available * window of <code>m_windowSize</code> windows to let further calls to * <code>{@link #readFilePortion()}</code> display the end of the file. <p> * //from w ww. j ava 2 s . co m * This method is triggered when a new file is chosen * (<code>{@link #setFilePath(String)}</code>) because the amount of lines changes. * This method is also triggered when a different window size is chosen * (<code>{@link #setWindowSize(int)}</code>) because the amount of lines to display change. * * @return the amount of lines in the file to view */ private int scrollToFileEnd() { int lines = 0; if (OpenCms.getRunLevel() < OpenCms.RUNLEVEL_3_SHELL_ACCESS) { // no scrolling if system not yet fully initialized } else { LineNumberReader reader = null; // shift the window position to the end of the file: this is expensive but OK for ocs logfiles as they // are ltd. to 2 MB try { reader = new LineNumberReader( new BufferedReader(new InputStreamReader(new FileInputStream(m_filePath)))); while (reader.readLine() != null) { lines++; } reader.close(); // if 11.75 windows are available, we don't want to end on window nr. 10 int availWindows = (int) Math.ceil((double) lines / (double) m_windowSize); // we start with window 0 m_windowPos = availWindows - 1; } catch (IOException ioex) { LOG.error("Unable to scroll file " + m_filePath + " to end. Ensure that it exists. "); } finally { if (reader != null) { try { reader.close(); } catch (Throwable f) { LOG.info("Unable to close reader of file " + m_filePath, f); } } } } return lines; }
From source file:org.opensha.commons.util.FileUtils.java
/** * Prints a Text file/*from w w w . j a va2s.com*/ * @param pjob PrintJob created using getToolkit().getPrintJob(JFrame,String,Properties); * @param pg Graphics * @param textToPrint String */ public static void print(PrintJob pjob, Graphics pg, String textToPrint) { int margin = 60; int pageNum = 1; int linesForThisPage = 0; int linesForThisJob = 0; // Note: String is immutable so won't change while printing. if (!(pg instanceof PrintGraphics)) { throw new IllegalArgumentException("Graphics context not PrintGraphics"); } StringReader sr = new StringReader(textToPrint); LineNumberReader lnr = new LineNumberReader(sr); String nextLine; int pageHeight = pjob.getPageDimension().height - margin; Font helv = new Font("Monaco", Font.PLAIN, 12); //have to set the font to get any output pg.setFont(helv); FontMetrics fm = pg.getFontMetrics(helv); int fontHeight = fm.getHeight(); int fontDescent = fm.getDescent(); int curHeight = margin; try { do { nextLine = lnr.readLine(); if (nextLine != null) { if ((curHeight + fontHeight) > pageHeight) { // New Page if (linesForThisPage == 0) break; pageNum++; linesForThisPage = 0; pg.dispose(); pg = pjob.getGraphics(); if (pg != null) { pg.setFont(helv); } curHeight = 0; } curHeight += fontHeight; if (pg != null) { pg.drawString(nextLine, margin, curHeight - fontDescent); linesForThisPage++; linesForThisJob++; } } } while (nextLine != null); } catch (EOFException eof) { // Fine, ignore } catch (Throwable t) { // Anything else t.printStackTrace(); } }
From source file:com.termmed.utils.FileHelper.java
/** * Copy to.//from www . j ava 2 s . c o m * * @param inputFile the input file * @param outputFile the output file * @throws IOException Signals that an I/O exception has occurred. */ public static void copyTo(File inputFile, File outputFile) throws IOException { FileInputStream fis = new FileInputStream(inputFile); InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); LineNumberReader reader = new LineNumberReader(isr); FileOutputStream fos = new FileOutputStream(outputFile); OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8"); BufferedWriter bw = new BufferedWriter(osw); String lineRead = ""; while ((lineRead = reader.readLine()) != null) { bw.append(lineRead); bw.append("\r\n"); } reader.close(); bw.close(); }
From source file:Librarian.java
/** * Loads the library from a file/*from w ww . ja v a2s . co m*/ * * @param filename * the filename * @throws IOException */ public void load(String filename) throws IOException { BufferedReader in = new BufferedReader(new LineNumberReader(new FileReader(filename))); String line; while ((line = in.readLine()) != null) { StringTokenizer st = new StringTokenizer(line, SEP); Book book = null; if (st.hasMoreTokens()) book = new Book(st.nextToken()); if (st.hasMoreTokens()) book.checkOut(st.nextToken()); if (book != null) add(book); } in.close(); this.filename = filename; dirty = false; }
From source file:ca.nrc.cadc.sc2pkg.PackageIntTest.java
private Content getEntry(TarArchiveInputStream tar) throws IOException, NoSuchAlgorithmException { Content ret = new Content(); TarArchiveEntry entry = tar.getNextTarEntry(); ret.name = entry.getName();//from w w w .ja va 2 s. com if (ret.name.endsWith("README")) { byte[] buf = new byte[(int) entry.getSize()]; tar.read(buf); ByteArrayInputStream bis = new ByteArrayInputStream(buf); LineNumberReader r = new LineNumberReader(new InputStreamReader(bis)); String line = r.readLine(); while (line != null) { String[] tokens = line.split(" "); // status [md5 filename url] String status = tokens[0]; if ("OK".equals(status)) { String fname = tokens[1]; String md5 = tokens[2]; ret.md5map.put(fname, md5); } else { throw new RuntimeException("tar content failure: " + line); } line = r.readLine(); } } else { MessageDigest md5 = MessageDigest.getInstance("MD5"); byte[] buf = new byte[8192]; int n = tar.read(buf); while (n > 0) { md5.update(buf, 0, n); n = tar.read(buf); } byte[] md5sum = md5.digest(); ret.contentMD5 = HexUtil.toHex(md5sum); } return ret; }
From source file:com.cisco.dvbu.ps.common.util.CommonUtils.java
/** * Find Lines Containing passed in match String from the passed in file * @param filePath file Path//from w ww . j a va 2 s . com * @param findMe find String * @return List of Matching Strings */ public static List<String> getLinesWithStringsInFile(String filePath, String findMe) { LineNumberReader lineReader = null; List<String> passwordLineList = null; try { lineReader = new LineNumberReader(new FileReader(filePath)); String line = null; passwordLineList = new ArrayList<String>(); while ((line = lineReader.readLine()) != null) { if (line.contains(findMe)) { passwordLineList.add(line); } } } catch (FileNotFoundException ex) { logger.error(ex.getMessage()); } catch (IOException ex) { logger.error(ex.getMessage()); } finally { try { if (lineReader != null) lineReader.close(); } catch (IOException ex) { logger.error(ex.getMessage()); } } return passwordLineList; }
From source file:org.apache.click.util.ErrorReport.java
/** * Return Java Source LineNumberReader for the given filename, or null if * not found.//w w w .jav a 2s .co m * * @param filename the name of the Java source file, e.g. /examples/Page.java * @return LineNumberReader for the given source filename, or null if * not found * @throws FileNotFoundException if file could not be found */ protected LineNumberReader getJavaSourceReader(String filename) throws FileNotFoundException { if (pageClass == null) { return null; } // Look for source file on classpath InputStream is = pageClass.getResourceAsStream(filename); if (is != null) { return new LineNumberReader(new InputStreamReader(is)); } // Else search for source file under WEB-INF String rootPath = servletContext.getRealPath("/"); String webInfPath = rootPath + File.separator + "WEB-INF"; File sourceFile = null; File webInfDir = new File(webInfPath); if (webInfDir.isDirectory() && webInfDir.canRead()) { File[] dirList = webInfDir.listFiles(); if (dirList != null) { for (File file : dirList) { if (file.isDirectory() && file.canRead()) { String sourcePath = file.toString() + filename; sourceFile = new File(sourcePath); if (sourceFile.isFile() && sourceFile.canRead()) { FileInputStream fis = new FileInputStream(sourceFile); return new LineNumberReader(new InputStreamReader(fis)); } } } } } return null; }
From source file:org.beangle.struts2.action.EntityDrivenAction.java
/** * //from www . j a v a 2s.c om * * @param upload * @param clazz * @return */ protected EntityImporter buildEntityImporter(String upload, Class<?> clazz) { try { File file = get(upload, File.class); if (null == file) { logger.error("cannot get upload file {}.", upload); return null; } String fileName = get(upload + "FileName"); InputStream is = new FileInputStream(file); if (fileName.endsWith(".xls")) { EntityImporter importer = (clazz == null) ? new DefaultEntityImporter() : new DefaultEntityImporter(clazz); importer.setReader(new ExcelItemReader(is, 1)); put("importer", importer); return importer; } else { LineNumberReader reader = new LineNumberReader(new InputStreamReader(is)); if (null == reader.readLine()) { reader.close(); return null; } reader.reset(); EntityImporter importer = (clazz == null) ? new DefaultEntityImporter() : new DefaultEntityImporter(clazz); importer.setReader(new CsvItemReader(reader)); return importer; } } catch (MyException e) { logger.error("saveAndForwad failure", e); return null; } catch (Exception e) { logger.error("error", e); return null; } }
From source file:com.sds.acube.ndisc.mts.xserver.util.XNDiscUtils.java
/** * XNDisc ? ?//from www . j av a2 s . c o m */ private static void readVersionFromFile() { XNDisc_PublishingVersion = "<unknown>"; XNDisc_PublishingDate = "<unknown>"; InputStreamReader isr = null; LineNumberReader lnr = null; try { isr = new InputStreamReader( XNDiscUtils.class.getResourceAsStream("/com/sds/acube/ndisc/mts/xserver/version.txt")); if (isr != null) { lnr = new LineNumberReader(isr); String line = null; do { line = lnr.readLine(); if (line != null) { if (line.startsWith("Publishing-Version=")) { XNDisc_PublishingVersion = line.substring("Publishing-Version=".length(), line.length()) .trim(); } else if (line.startsWith("Publishing-Date=")) { XNDisc_PublishingDate = line.substring("Publishing-Date=".length(), line.length()) .trim(); } } } while (line != null); lnr.close(); } } catch (IOException ioe) { XNDisc_PublishingVersion = "<unknown>"; XNDisc_PublishingDate = "<unknown>"; } finally { try { if (lnr != null) { lnr.close(); } if (isr != null) { isr.close(); } } catch (IOException ioe) { } } }
From source file:chatbot.Chatbot.java
/** ************************************************************************************************ * Read a file of stopwords into the variable * ArrayList<String> stopwords//from www . ja va 2 s .co m */ private void readStopWords(String stopwordsFilename) throws IOException { String filename = ""; if (asResource) { URL stopWordsFile = Resources.getResource("resources/stopwords.txt"); filename = stopWordsFile.getPath(); } else filename = stopwordsFilename; FileReader r = new FileReader(filename); LineNumberReader lr = new LineNumberReader(r); String line; while ((line = lr.readLine()) != null) stopwords.add(line.intern()); return; }