Example usage for java.io LineNumberReader setLineNumber

List of usage examples for java.io LineNumberReader setLineNumber

Introduction

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

Prototype

public void setLineNumber(int lineNumber) 

Source Link

Document

Set the current line number.

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);

    // set current line number
    lnr.setLineNumber(100);
    System.out.println("setLineNumber(100) is invoked");

    // get current line number
    int i = lnr.getLineNumber();
    System.out.print("Current line number: " + i);
    lnr.close();/*w w  w.j ava2  s . c o  m*/
}

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);

    // sets the current line number
    lnr.setLineNumber(100);

    // get the current line number
    int i = lnr.getLineNumber();

    // print the current line number
    System.out.print("Current line number: " + i);
    lnr.close();//from  w w w .j  a v a  2s. c  om
}

From source file:SumLine.java

static void sumLines(String filename) throws IOException {
    LineNumberReader lnr = new LineNumberReader(new FileReader(filename));
    lnr.setLineNumber(1);
    StreamTokenizer stok = new StreamTokenizer(lnr);
    stok.parseNumbers();/*from  w  ww .ja v  a  2  s  . com*/
    stok.eolIsSignificant(true);
    stok.nextToken();
    while (stok.ttype != StreamTokenizer.TT_EOF) {
        int lineno = lnr.getLineNumber();
        double sum = 0;
        while (stok.ttype != StreamTokenizer.TT_EOL) {
            if (stok.ttype == StreamTokenizer.TT_NUMBER)
                sum += stok.nval;
            stok.nextToken();
        }
        System.out.println("Sum of line " + lineno + " is " + sum);
        stok.nextToken();
    }
}

From source file:com.jkoolcloud.tnt4j.streams.configure.state.FileStreamStateHandlerTest.java

@Test
public void findStreamingFile() throws Exception {
    FileStreamStateHandler rwd = new FileStreamStateHandler();

    File testFilesDir = new File(samplesDir, "/multiple-logs/");
    File[] testFiles = testFilesDir.listFiles((FilenameFilter) new WildcardFileFilter("orders*")); // NON-NLS
    FileAccessState newFAS = new FileAccessState();

    int count = 0;
    File fileToSearchFor = null;/*from   w  w w.ja v a 2s .  co  m*/
    int lineLastRead = 0;
    File fileWritten = null;
    for (File testFile : testFiles) {
        count++;
        FileReader in;
        LineNumberReader reader;

        Long fileCRC = rwd.getFileCrc(testFile);
        if (count == 2) {
            newFAS.currentFileCrc = fileCRC;
            fileToSearchFor = testFile;
        }

        in = new FileReader(testFile);
        reader = new LineNumberReader(in);
        reader.setLineNumber(0);
        String line = reader.readLine();
        int count2 = 0;
        while (line != null) {
            count2++;
            Checksum crcLine = new CRC32();
            final byte[] bytes4Line = line.getBytes();
            crcLine.update(bytes4Line, 0, bytes4Line.length);
            final long lineCRC = crcLine.getValue();
            final int lineNumber = reader.getLineNumber();
            System.out.println("for " + lineNumber + " line CRC is " + lineCRC); // NON-NLS
            if (count2 == 3) {
                newFAS.currentLineCrc = lineCRC;
                newFAS.currentLineNumber = lineNumber;
                newFAS.lastReadTime = System.currentTimeMillis();
                lineLastRead = lineNumber;
            }
            line = reader.readLine();
        }
        fileWritten = AbstractFileStreamStateHandler.writeState(newFAS, testFilesDir, "TestStream"); // NON-NLS
        Utils.close(reader);
    }

    final File findLastProcessed = rwd.findStreamingFile(newFAS, testFiles);
    assertEquals(fileToSearchFor, findLastProcessed);
    final int lineLastReadRecorded = rwd.checkLine(findLastProcessed, newFAS);
    assertEquals(lineLastRead, lineLastReadRecorded);
    fileWritten.delete();
}

From source file:org.jumpmind.metl.core.runtime.component.XmlReader.java

void processFiles(List<String> files, Message inputMessage, ISendMessageCallback callback,
        boolean unitOfWorkLastMessage) throws XmlPullParserException, IOException {
    IDirectory directory = getResourceReference();
    XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
    for (String file : files) {
        ArrayList<String> outboundPayload = new ArrayList<String>();
        log(LogLevel.INFO, "Reading %s", file);
        Map<String, Serializable> headers = new HashMap<>();
        headers.put("source.file.path", file);
        LineNumberReader lineNumberReader = null;
        InputStream parserIs = null;
        try {/*from w ww.  ja va2 s .  c o m*/
            String filePath = resolveParamsAndHeaders(file, inputMessage);
            parserIs = directory.getInputStream(filePath, mustExist);
            if (parserIs != null) {
                InputStreamReader reader = new InputStreamReader(directory.getInputStream(filePath, mustExist),
                        encoding);
                parser.setInput(parserIs, encoding);
                lineNumberReader = new LineNumberReader(reader);
                lineNumberReader.setLineNumber(1);
                int startCol = 0;
                int startLine = 1;
                int prevEndLine = 1;
                int prevEndCol = 0;
                int eventType = parser.getEventType();
                String line = null;
                while (eventType != XmlPullParser.END_DOCUMENT) {
                    switch (eventType) {
                    case XmlPullParser.START_TAG:
                        if (StringUtils.isBlank(readTag)) {
                            readTag = parser.getName();
                            info("Read tag was not set, defaulting to root tag: " + readTag);
                        }
                        if (parser.getName().equals(readTag)) {
                            startCol = prevEndCol;
                            startLine = prevEndLine;
                        }
                        prevEndCol = parser.getColumnNumber();
                        prevEndLine = parser.getLineNumber();
                        break;
                    case XmlPullParser.END_TAG:
                        prevEndCol = parser.getColumnNumber();
                        prevEndLine = parser.getLineNumber();
                        if (parser.getName().equals(readTag)) {
                            StringBuilder xml = new StringBuilder();

                            forward(startLine, lineNumberReader);

                            int linesToRead = parser.getLineNumber() - lineNumberReader.getLineNumber();
                            if (lineNumberReader.getLineNumber() > startLine) {
                                startCol = 0;
                            }
                            line = lineNumberReader.readLine();

                            while (linesToRead >= 0 && line != null) {
                                if (startCol > 0) {
                                    if (line.length() > startCol) {
                                        xml.append(line.substring(startCol)).append("\n");
                                    }
                                    startCol = 0;
                                } else if (linesToRead == 0) {
                                    if (line.length() > parser.getColumnNumber()) {
                                        xml.append(line.substring(0, parser.getColumnNumber()));
                                    } else {
                                        xml.append(line).append("\n");
                                    }
                                } else {
                                    xml.append(line).append("\n");
                                }

                                linesToRead--;
                                if (linesToRead >= 0) {
                                    line = lineNumberReader.readLine();
                                }
                            }
                            getComponentStatistics().incrementNumberEntitiesProcessed(threadNumber);
                            outboundPayload.add(xml.toString());
                            if (outboundPayload.size() == readTagsPerMessage) {
                                callback.sendTextMessage(headers, outboundPayload);
                                outboundPayload = new ArrayList<String>();
                            }
                            startCol = 0;
                        }
                        break;
                    }
                    eventType = parser.next();
                }
            } else {
                info("File %s didn't exist, but Must Exist setting was false.  Continuing", file);
            }
        } finally {
            closeQuietly(lineNumberReader);
            closeQuietly(parserIs);
        }

        if (outboundPayload.size() > 0) {
            callback.sendTextMessage(headers, outboundPayload);
        }
    }
}