Example usage for java.io LineNumberReader reset

List of usage examples for java.io LineNumberReader reset

Introduction

In this page you can find the example usage for java.io LineNumberReader reset.

Prototype

public void reset() throws IOException 

Source Link

Document

Reset the stream to the most recent mark.

Usage

From source file:Main.java

public static void main(String[] args) throws IOException {

    FileReader fr = new FileReader("C:/test.txt");
    LineNumberReader lnr = new LineNumberReader(fr);

    // reads and prints data from reader
    System.out.println((char) lnr.read());
    System.out.println((char) lnr.read());

    // mark invoked at this position
    lnr.mark(0);//from  w w  w .j a v a  2 s. c  o m
    System.out.println("mark() invoked");
    System.out.println((char) lnr.read());
    System.out.println((char) lnr.read());

    // if this reader supports mark() an reset()
    if (lnr.markSupported()) {
        // reset() repositioned the stream to the mark
        lnr.reset();
        System.out.println("reset() invoked");
        System.out.println((char) lnr.read());
        System.out.println((char) lnr.read());
    }
    lnr.close();
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    // create new reader
    FileReader fr = new FileReader("C:/test.txt");
    LineNumberReader lnr = new LineNumberReader(fr);

    // reads and prints data from reader
    System.out.println((char) lnr.read());
    System.out.println((char) lnr.read());

    // mark invoked at this position
    lnr.mark(0);/*from w  w w.j a  v a2  s  . c o m*/
    System.out.println("mark() invoked");
    System.out.println((char) lnr.read());
    System.out.println((char) lnr.read());

    // if this reader supports mark() an reset()
    if (lnr.markSupported()) {
        // reset() repositioned the reader to the mark
        lnr.reset();
        System.out.println("reset() invoked");
        System.out.println((char) lnr.read());
        System.out.println((char) lnr.read());
    }
    lnr.close();
}

From source file:org.beanfuse.struts2.action.EntityDrivenAction.java

/**
 * /*from w ww  .  j a v a2  s .c  om*/
 * 
 * @param upload
 * @param clazz
 * @return
 */
protected EntityImporter buildEntityImporter(String upload, Class clazz) {
    try {
        File[] files = (File[]) ActionContext.getContext().getParameters().get(upload);
        if (files == null || files.length < 1) {
            logger.error("cannot get {} file.", upload);
        }
        String fileName = get(upload + "FileName");
        InputStream is = new FileInputStream(files[0]);
        if (fileName.endsWith(".xls")) {
            HSSFWorkbook wb = new HSSFWorkbook(is);
            if (wb.getNumberOfSheets() < 1 || wb.getSheetAt(0).getLastRowNum() == 0) {
                return null;
            }
            EntityImporter importer = (clazz == null) ? new DefaultEntityImporter()
                    : new DefaultEntityImporter(clazz);
            importer.setReader(new ExcelItemReader(wb, 1));
            put("importer", importer);
            return importer;
        } else {
            LineNumberReader reader = new LineNumberReader(new InputStreamReader(is));
            if (null == reader.readLine())
                return null;
            reader.reset();
            EntityImporter importer = (clazz == null) ? new DefaultEntityImporter()
                    : new DefaultEntityImporter(clazz);
            importer.setReader(new CSVReader(reader));
            return importer;
        }
    } catch (Exception e) {
        logger.error("error", e);
        return null;
    }
}

From source file:org.beangle.struts2.action.EntityDrivenAction.java

/**
 * /* w ww  .ja v a2s  . c  o m*/
 * 
 * @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:org.codehaus.mojo.taglist.FileAnalyser.java

/**
 * Scans a file to look for task tags.//w  w w. j  a v a2  s  . c o m
 * 
 * @param file the file to scan.
 */
public void scanFile(File file) {
    LineNumberReader reader = null;

    try {
        reader = new LineNumberReader(getReader(file));

        String currentLine = reader.readLine();
        while (currentLine != null) {
            int index = -1;
            Iterator iter = tagClasses.iterator();
            // look for a tag on this line
            while (iter.hasNext()) {
                TagClass tagClass = (TagClass) iter.next();
                index = tagClass.tagMatchContains(currentLine, locale);
                if (index != TagClass.NO_MATCH) {
                    // there's a tag on this line
                    String commentType = null;
                    commentType = extractCommentType(currentLine, index);

                    if (commentType == null) {
                        // this is not a valid comment tag: skip other tag classes and
                        // go to the next line
                        break;
                    }

                    int tagLength = tagClass.getLastTagMatchStringLength();
                    int commentStartIndex = reader.getLineNumber();
                    StringBuffer comment = new StringBuffer();

                    String firstLine = StringUtils.strip(currentLine.substring(index + tagLength));
                    firstLine = StringUtils.removeEnd(firstLine, "*/"); //MTAGLIST-35
                    if (firstLine.length() == 0 || ":".equals(firstLine)) {
                        // this is not a valid comment tag: nothing is written there
                        if (emptyCommentsOn) {
                            comment.append("--");
                            comment.append(noCommentString);
                            comment.append("--");
                        } else {
                            continue;
                        }
                    } else {
                        // this tag has a comment
                        if (firstLine.charAt(0) == ':') {
                            comment.append(firstLine.substring(1).trim());
                        } else {
                            comment.append(firstLine);
                        }

                        if (multipleLineCommentsOn) {
                            // Mark the current position, set the read forward limit to
                            // a large number that should not be met.
                            reader.mark(MAX_COMMENT_CHARACTERS);

                            // next line
                            String futureLine = reader.readLine();

                            // we're looking for multiple line comments
                            while (futureLine != null && futureLine.trim().startsWith(commentType)
                                    && futureLine.indexOf(tagClass.getLastTagMatchString()) < 0) {
                                String currentComment = futureLine
                                        .substring(futureLine.indexOf(commentType) + commentType.length())
                                        .trim();
                                if (currentComment.startsWith("@") || "".equals(currentComment)
                                        || "/".equals(currentComment)) {
                                    // the comment is finished
                                    break;
                                }
                                // try to look if the next line is not a new tag
                                boolean newTagFound = false;
                                Iterator moreTCiter = tagClasses.iterator();
                                while (moreTCiter.hasNext()) {
                                    TagClass tc = (TagClass) moreTCiter.next();
                                    if (tc.tagMatchStartsWith(currentComment, locale)) {
                                        newTagFound = true;
                                        break;
                                    }
                                }
                                if (newTagFound) {
                                    // this is a new comment: stop here the current comment
                                    break;
                                }
                                // nothing was found: this means the comment is going on this line
                                comment.append(" ");
                                comment.append(currentComment);
                                futureLine = reader.readLine();
                            }

                            // Reset the reader to the marked position before the multi
                            // line check was performed.
                            reader.reset();
                        }
                    }
                    TagReport tagReport = tagClass.getTagReport();
                    FileReport fileReport = tagReport.getFileReport(file, encoding);
                    fileReport.addComment(comment.toString(), commentStartIndex);
                }
            }
            currentLine = reader.readLine();
        }
    } catch (IOException e) {
        log.error("Error while scanning the file " + file.getPath(), e);
    } finally {
        IOUtil.close(reader);
    }
}

From source file:org.seqdoop.hadoop_bam.TestVCFOutputFormat.java

private void skipHeader(LineNumberReader reader) throws IOException {
    String line = reader.readLine();
    while (line.startsWith("#")) {
        reader.mark(1000);/*from w w  w  .j a  v  a 2s . c  om*/
        line = reader.readLine();
    }
    reader.reset();
}