Example usage for java.io LineNumberReader readLine

List of usage examples for java.io LineNumberReader readLine

Introduction

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

Prototype

public String readLine() throws IOException 

Source Link

Document

Read a line of text.

Usage

From source file:mitm.common.postfix.PostfixLogParser.java

private void parseLog(Reader log) throws IOException {
    LineNumberReader lineReader = new LineNumberReader(log);

    index = 0;//w ww  .  j  ava2  s. c  o  m
    stopParsing = false;

    activeItems = new HashMap<String, SortableLogItem>();

    String line;

    while (!stopParsing && (line = lineReader.readLine()) != null) {
        parseLine(line);
    }

    /*
     * If stopParsing was false we came to the end of the log. There can however still be items 
     * in activeItems. This happens when a message is still in one of the postfix queues 
     * (message is not yet delivered). We will therefore 'force finish' the items still active 
     * until stopParsing 
     */
    for (SortableLogItem logItem : activeItems.values()) {
        if (stopParsing) {
            break;
        }

        logItemComplete(logItem);
    }
}

From source file:org.apache.pdfbox.text.BidiTest.java

/**
 * Validate text extraction on a single file.
 *
 * @param inFile The PDF file to validate
 * @param outDir The directory to store the output in
 * @param bLogResult Whether to log the extracted text
 * @param bSort Whether or not the extracted text is sorted
 * @throws Exception when there is an exception
 *//*from w w  w . j a  v a2 s. co  m*/
public void doTestFile(File inFile, File outDir, boolean bLogResult, boolean bSort) throws IOException {
    if (bSort) {
        log.info("Preparing to parse " + inFile.getName() + " for sorted test");
    } else {
        log.info("Preparing to parse " + inFile.getName() + " for standard test");
    }

    if (!outDir.exists()) {
        if (!outDir.mkdirs()) {
            throw (new IOException("Error creating " + outDir.getAbsolutePath() + " directory"));
        }
    }

    PDDocument document = PDDocument.load(inFile);
    try {
        File outFile;
        File expectedFile;

        if (bSort) {
            outFile = new File(outDir, inFile.getName() + "-sorted.txt");
            expectedFile = new File(inFile.getParentFile(), inFile.getName() + "-sorted.txt");
        } else {
            outFile = new File(outDir, inFile.getName() + ".txt");
            expectedFile = new File(inFile.getParentFile(), inFile.getName() + ".txt");
        }

        OutputStream os = new FileOutputStream(outFile);
        try {
            Writer writer = new OutputStreamWriter(os, ENCODING);
            try {
                //Allows for sorted tests 
                stripper.setSortByPosition(bSort);
                stripper.writeText(document, writer);
            } finally {
                // close the written file before reading it again
                writer.close();
            }
        } finally {
            os.close();
        }

        if (bLogResult) {
            log.info("Text for " + inFile.getName() + ":");
            log.info(stripper.getText(document));
        }

        if (!expectedFile.exists()) {
            this.bFail = true;
            fail("FAILURE: Input verification file: " + expectedFile.getAbsolutePath() + " did not exist");
            return;
        }

        LineNumberReader expectedReader = new LineNumberReader(
                new InputStreamReader(new FileInputStream(expectedFile), ENCODING));
        LineNumberReader actualReader = new LineNumberReader(
                new InputStreamReader(new FileInputStream(outFile), ENCODING));

        while (true) {
            String expectedLine = expectedReader.readLine();
            while (expectedLine != null && expectedLine.trim().length() == 0) {
                expectedLine = expectedReader.readLine();
            }
            String actualLine = actualReader.readLine();
            while (actualLine != null && actualLine.trim().length() == 0) {
                actualLine = actualReader.readLine();
            }
            if (!stringsEqual(expectedLine, actualLine)) {
                this.bFail = true;
                fail("FAILURE: Line mismatch for file " + inFile.getName() + " (sort = " + bSort + ")"
                        + " at expected line: " + expectedReader.getLineNumber() + " at actual line: "
                        + actualReader.getLineNumber() + "\nexpected line was: \"" + expectedLine + "\""
                        + "\nactual line was:   \"" + actualLine + "\"" + "\n");

                //lets report all lines, even though this might produce some verbose logging
                //break;
            }

            if (expectedLine == null || actualLine == null) {
                break;
            }
        }
        expectedReader.close();
        actualReader.close();
    } finally {
        document.close();
    }
}

From source file:org.fuin.esmp.AbstractEventStoreMojo.java

/**
 * Returns the string as list./*w w  w. ja v  a 2  s  . com*/
 * 
 * @param str
 *            String to split into lines.
 * 
 * @return List of lines.
 * 
 * @throws MojoExecutionException
 *             Error splitting the string into lines.
 */
protected final List<String> asList(final String str) throws MojoExecutionException {
    try {
        final List<String> lines = new ArrayList<String>();
        final LineNumberReader reader = new LineNumberReader(new StringReader(str));
        String line;
        while ((line = reader.readLine()) != null) {
            lines.add(line);
        }
        return lines;
    } catch (final IOException ex) {
        throw new MojoExecutionException("Error creating string list", ex);
    }
}

From source file:com.l2jfree.gameserver.handler.admincommands.AdminTeleport.java

private void delbookmark(String Name) {
    File file = new File(Config.DATAPACK_ROOT, "data/html/admin/tele/bookmark.txt");
    LineNumberReader lnr = null;
    String bookmarks = "";

    try {/*www  .j a v  a  2s  . c  o  m*/
        String line = null;
        lnr = new LineNumberReader(new FileReader(file));
        while ((line = lnr.readLine()) != null) {
            StringTokenizer st = new StringTokenizer(line, ";");
            String nm = st.nextToken();
            if (!nm.equals(Name))
                bookmarks += line + "\n";
        }

        FileWriter save = new FileWriter(file);
        save.write(bookmarks);
        save.close();
    } catch (FileNotFoundException e) {
    } catch (IOException e1) {
        e1.printStackTrace();
    } finally {
        try {
            if (lnr != null)
                lnr.close();
        } catch (Exception e2) {
        }
    }
}

From source file:edu.stanford.muse.util.EmailUtils.java

private static Map<String, String> readDBpedia(double p, String typesFile) {
    if (dbpedia != null) {
        if (p == 1)
            return dbpedia;
        else/*from   w  w  w  .  j  a  v a 2 s  . co  m*/
            return new org.apache.commons.collections4.map.CaseInsensitiveMap<>(sample(dbpedia, p));
    }
    if (typesFile == null)
        typesFile = Config.DBPEDIA_INSTANCE_FILE;
    //dbpedia = new LinkedHashMap<>();
    //we want to be able to access elements in the map in a case-sensitive manner, this is a way to do that.
    dbpedia = new org.apache.commons.collections4.map.CaseInsensitiveMap<>();
    int d = 0, numPersons = 0, lines = 0;
    try {
        InputStream is = Config.getResourceAsStream(typesFile);
        if (is == null) {
            log.warn("DBpedia file resource could not be read!!");
            return dbpedia;
        }

        //true argument for BZip2CompressorInputStream so as to load the whole file content into memory
        LineNumberReader lnr = new LineNumberReader(
                new InputStreamReader(new BZip2CompressorInputStream(is, true), "UTF-8"));
        while (true) {
            String line = lnr.readLine();
            if (line == null)
                break;
            if (lines++ % 1000000 == 0)
                log.info("Processed " + lines + " lines of approx. 3.02M in " + typesFile);

            if (line.contains("GivenName"))
                continue;

            String[] words = line.split("\\s+");
            String r = words[0];

            /**
             * The types file contains lines like this:
             * National_Bureau_of_Asian_Research Organisation|Agent
             * National_Bureau_of_Asian_Research__1 PersonFunction
             * National_Bureau_of_Asian_Research__2 PersonFunction
             * Which leads to classifying "National_Bureau_of_Asian_Research" as PersonFunction and not Org.
             */
            if (r.contains("__")) {
                d++;
                continue;
            }
            //if it still contains this, is a bad title.
            if (r.equals("") || r.contains("__")) {
                d++;
                continue;
            }
            String type = words[1];
            //Royalty names, though tagged person are very weird, contains roman characters and suffixes like of_Poland e.t.c.
            if (type.equals("PersonFunction") || type.equals("Royalty|Person|Agent"))
                continue;
            //in places there are things like: Shaikh_Ibrahim,_Iraq
            if (type.endsWith("Settlement|PopulatedPlace|Place"))
                r = r.replaceAll(",_.*", "");

            //its very dangerous to remove things inside brackets as that may lead to terms like
            //University_(Metrorail_Station) MetroStation|Place e.t.c.
            //so keep them, or just skip this entry all together
            //We are not considering single word tokens any way, so its OK to remove things inside the brackets
            //removing stuff in brackets may cause trouble when blind matching entities
            //r = r.replaceAll("_\\(.*?\\)", "");
            String title = r.replaceAll("_", " ");

            String badSuffix = "|Agent";
            if (type.endsWith(badSuffix) && type.length() > badSuffix.length())
                type = type.substring(0, type.length() - badSuffix.length());
            if (type.endsWith("|Person"))
                numPersons++;
            type = type.intern(); // type strings are repeated very often, so intern

            if (type.equals("Road|RouteOfTransportation|Infrastructure|ArchitecturalStructure|Place")) {
                //System.err.print("Cleaned: "+title);
                title = cleanDBPediaRoad(title);
                //System.err.println(" to "+title);
            }
            dbpedia.put(title, type);
        }
        lnr.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    log.info("Read " + dbpedia.size() + " names from DBpedia, " + numPersons + " people name. dropped: " + d);

    return new org.apache.commons.collections4.map.CaseInsensitiveMap<>(sample(dbpedia, p));
}

From source file:org.gofleet.module.routing.RoutingMap.java

private int getNumberLines(File f) {
    LineNumberReader lineCounter;
    try {//from   w  w w.  j av a 2 s. com
        lineCounter = new LineNumberReader(new InputStreamReader(new FileInputStream(f.getPath())));

        while (lineCounter.readLine() != null)
            ;
        return lineCounter.getLineNumber();
    } catch (Exception done) {
        log.error(done, done);
        return -1;
    }

}

From source file:de.innovationgate.wgpublisher.design.fs.AbstractDesignFile.java

protected String readCode(DesignMetadata md) throws FileNotFoundException, IOException, WGDesignSyncException,
        InstantiationException, IllegalAccessException {

    // No, filecontainers have no code, but thanks for asking....
    if (getType() == WGDocument.TYPE_FILECONTAINER) {
        return null;
    }// w w  w  .j  a va 2 s .co  m

    FileObject codeFile = getCodeFile();
    if (!codeFile.exists()) {
        throw new WGDesignSyncException("Code of file '" + getCodeFile().getName().getPath()
                + "' could not be read because the file does not exist.");
    }

    LineNumberReader reader = new LineNumberReader(createReader(codeFile));
    StringWriter writer = new StringWriter();
    int headerLines = 0;
    try {
        String line;
        boolean lookForHeaders = true;
        boolean firstLine = true;

        while ((line = reader.readLine()) != null) {
            if (lookForHeaders == true && line.startsWith("##")) {
                processDesignHeader(line, md.getInfo());
                headerLines++;
            } else {
                lookForHeaders = false;

                if (!firstLine) {
                    writer.write("\n");
                } else {
                    firstLine = false;
                }

                writer.write(line);
            }
        }
    } finally {
        reader.close();
        codeFile.getContent().close();
    }
    writer.close();
    md.setHeaderLines(headerLines);
    String code = writer.toString();
    return code;
}

From source file:org.apache.pdfbox.util.TestTextStripper.java

/**
 * Validate text extraction on a single file.
 *
 * @param inFile The PDF file to validate
 * @param outDir The directory to store the output in
 * @param bLogResult Whether to log the extracted text
 * @param bSort Whether or not the extracted text is sorted
 * @throws Exception when there is an exception
 *///w w w.ja  v  a2  s .  com
public void doTestFile(File inFile, File outDir, boolean bLogResult, boolean bSort) throws Exception {
    if (bSort) {
        log.info("Preparing to parse " + inFile.getName() + " for sorted test");
    } else {
        log.info("Preparing to parse " + inFile.getName() + " for standard test");
    }

    if (!outDir.exists()) {
        if (!outDir.mkdirs()) {
            throw (new Exception("Error creating " + outDir.getAbsolutePath() + " directory"));
        }
    }

    PDDocument document = PDDocument.load(inFile);
    try {

        File outFile = null;
        File expectedFile = null;

        if (bSort) {
            outFile = new File(outDir, inFile.getName() + "-sorted.txt");
            expectedFile = new File(inFile.getParentFile(), inFile.getName() + "-sorted.txt");
        } else {
            outFile = new File(outDir, inFile.getName() + ".txt");
            expectedFile = new File(inFile.getParentFile(), inFile.getName() + ".txt");
        }

        OutputStream os = new FileOutputStream(outFile);
        try {
            os.write(0xFF);
            os.write(0xFE);

            Writer writer = new OutputStreamWriter(os, encoding);
            try {
                //Allows for sorted tests 
                stripper.setSortByPosition(bSort);
                stripper.writeText(document, writer);
            } finally {
                // close the written file before reading it again
                writer.close();
            }
        } finally {
            os.close();
        }

        if (bLogResult) {
            log.info("Text for " + inFile.getName() + ":");
            log.info(stripper.getText(document));
        }

        if (!expectedFile.exists()) {
            this.bFail = true;
            log.error("FAILURE: Input verification file: " + expectedFile.getAbsolutePath() + " did not exist");
            return;
        }

        LineNumberReader expectedReader = new LineNumberReader(
                new InputStreamReader(new FileInputStream(expectedFile), encoding));
        LineNumberReader actualReader = new LineNumberReader(
                new InputStreamReader(new FileInputStream(outFile), encoding));

        while (true) {
            String expectedLine = expectedReader.readLine();
            while (expectedLine != null && expectedLine.trim().length() == 0) {
                expectedLine = expectedReader.readLine();
            }
            String actualLine = actualReader.readLine();
            while (actualLine != null && actualLine.trim().length() == 0) {
                actualLine = actualReader.readLine();
            }
            if (!stringsEqual(expectedLine, actualLine)) {
                this.bFail = true;
                log.error("FAILURE: Line mismatch for file " + inFile.getName() + " ( sort = " + bSort + ")"
                        + " at expected line: " + expectedReader.getLineNumber() + " at actual line: "
                        + actualReader.getLineNumber());
                log.error("  expected line was: \"" + expectedLine + "\"");
                log.error("  actual line was:   \"" + actualLine + "\"" + "\n");

                //lets report all lines, even though this might produce some verbose logging
                //break;
            }

            if (expectedLine == null || actualLine == null) {
                break;
            }
        }
    } finally {
        document.close();
    }
}

From source file:chatbot.Chatbot.java

/** ************************************************************************************************
 * Read a file of stopwords into the variable
 * ArrayList<String> stopwords/* w w w .j  av a 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;
}

From source file:com.l2jfree.gameserver.handler.admincommands.AdminTeleport.java

private void bookmark(L2Player activeChar, String Name) {
    File file = new File(Config.DATAPACK_ROOT, "data/html/admin/tele/bookmark.txt");
    LineNumberReader lnr = null;
    String bookmarks = "";
    String table = "";
    try {//from www . ja  v  a2s  .com
        String line = null;
        lnr = new LineNumberReader(new FileReader(file));
        while ((line = lnr.readLine()) != null) {
            bookmarks += line + "\n";
            StringTokenizer st = new StringTokenizer(line, ";");
            String nm = st.nextToken();
            table += ("<a action=\"bypass -h admin_move_to " + st.nextToken() + " " + st.nextToken() + " "
                    + st.nextToken() + "\">" + nm + "</a>&nbsp;");
            table += ("<a action=\"bypass -h admin_delbookmark " + nm
                    + "\"><font color=\"FF0000\">[X]</font></a><br>");
        }
        if (Name == null) {
            NpcHtmlMessage adminReply = new NpcHtmlMessage(5);
            adminReply.setFile("data/html/admin/tele/bookmarks.htm");
            adminReply.replace("%bookmarks%", table);
            activeChar.sendPacket(adminReply);
        } else {
            FileWriter save = new FileWriter(file);
            bookmarks += Name + ";" + activeChar.getX() + ";" + activeChar.getY() + ";" + activeChar.getZ()
                    + "\n";
            save.write(bookmarks);
            save.close();
            bookmark(activeChar, null);
        }
    } catch (FileNotFoundException e) {
        activeChar.sendMessage("bookmark.txt not found");
    } catch (IOException e1) {
        e1.printStackTrace();
    } finally {
        try {
            if (lnr != null)
                lnr.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}