Example usage for java.io LineNumberReader LineNumberReader

List of usage examples for java.io LineNumberReader LineNumberReader

Introduction

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

Prototype

public LineNumberReader(Reader in) 

Source Link

Document

Create a new line-numbering reader, using the default input-buffer size.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    LineNumberReader r = new LineNumberReader(new FileReader("yourFile.txt"));

    String line = null;//from  w  ww  . j av  a  2 s .  com

    while ((line = r.readLine()) != null) {
        System.out.println(r.getLineNumber() + ": " + line);
    }
    r.close();
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    LineNumberReader lineCounter = new LineNumberReader(new InputStreamReader(System.in));

    String nextLine = null;/*w ww  .j  a  va 2 s .co m*/
    System.out.println("Type any text and press return. Type 'exit' to quit the program.");
    try {
        while ((nextLine = lineCounter.readLine()).indexOf("exit") == -1) {
            if (nextLine == null)
                break;
            System.out.print(lineCounter.getLineNumber());
            System.out.print(": ");
            System.out.println(nextLine);
        }
    } catch (Exception done) {
        done.printStackTrace();
    }
}

From source file:LineViewer.java

public static void main(String[] args) throws Exception {
    LineNumberReader lnr = null;//from  ww w .  j  ava  2  s. c  o m

    FileReader fr = new FileReader(args[0]);
    lnr = new LineNumberReader(fr);

    String s;
    while ((s = lnr.readLine()) != null)
        System.out.println(lnr.getLineNumber() + ": " + s);
}

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);/*from w w w.  j a  v a2 s.  co m*/
    System.out.println("setLineNumber(100) is invoked");

    // get current line number
    int i = lnr.getLineNumber();
    System.out.print("Current line number: " + i);
    lnr.close();
}

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);/*from   w ww. ja v  a 2s  .  c o  m*/

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

    // print the current line number
    System.out.print("Current line number: " + i);
    lnr.close();
}

From source file:LineNumberIOApp.java

public static void main(String args[]) throws IOException {
    FileReader inFile = new FileReader("LineNumberIOApp.java");
    LineNumberReader inLines = new LineNumberReader(inFile);
    String inputLine;/*ww w. j  av  a 2s  . co  m*/
    while ((inputLine = inLines.readLine()) != null) {
        System.out.println(inLines.getLineNumber() + ". " + inputLine);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File file = new File("data.csv");
    FileReader fr = new FileReader(file);
    LineNumberReader lnr = new LineNumberReader(fr);
    // lnr.setLineNumber(400);
    String line = "";
    while ((line = lnr.readLine()) != null) {
        System.out.println("Line Number " + lnr.getLineNumber() + ": " + line);
    }//from   www.j  av a  2s .co m
    fr.close();
    lnr.close();
}

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 2s.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);/* w  w w  .ja  va 2 s. co 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:Main.java

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

    int i;//from  w  w w  . j a  v a  2  s. co  m

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

    while ((i = lnr.read()) != -1) {
        // converts int to char
        char c = (char) i;

        // prints character
        System.out.println(c);
    }
    lnr.close();
}