As mentioned in Java API, LineNumberReader class keeps track of line number and return line number in addition to the data read. In this post I will mainly cover setLineNumber method in the class. The api document doesn’t clearly mention its purpose. The setLineNumber can be used for two purposes
1) Reset the line number count back to 0
2) Set the line number count to any integer other than default 0
As shown in the below code
package IO;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
public class LineReaderDemo {
public static void main(String[] args) {
File file = new File(".project");
try(FileReader fr = new FileReader(file);LineNumberReader lr = new LineNumberReader(fr);) {
String data = lr.readLine();
while(data != null) {
System.out.println(lr.getLineNumber() + data);
data = lr.readLine();
}
} catch(FileNotFoundException excep) {
excep.printStackTrace();
} catch(IOException excep) {
excep.printStackTrace();
}
System.out.println();
//Reset the line number count back to 0
try(FileReader fr = new FileReader(file);LineNumberReader lr = new LineNumberReader(fr);) {
String data = lr.readLine();
int i = 1;
while(data != null) {
System.out.println(lr.getLineNumber() + data);
if(i == 10) {
lr.setLineNumber(0);
}
data = lr.readLine();
i++;
}
} catch(FileNotFoundException excep) {
excep.printStackTrace();
} catch(IOException excep) {
excep.printStackTrace();
}
System.out.println();
//Set the line number count to any integer other than default 0
try(FileReader fr = new FileReader(file);LineNumberReader lr = new LineNumberReader(fr);) {
lr.setLineNumber(10);
String data = lr.readLine();
while(data != null) {
System.out.println(lr.getLineNumber() + data);
data = lr.readLine();
}
} catch(FileNotFoundException excep) {
excep.printStackTrace();
} catch(IOException excep) {
excep.printStackTrace();
}
}
}
I have read .project xml file created by the xml. The contents of xml are as shown below
<?xml version=”1.0″ encoding=”UTF-8″?>
<projectDescription>
<name>JavaConcepts</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Output
1<?xml version=”1.0″ encoding=”UTF-8″?>
2<projectDescription>
3 <name>JavaConcepts</name>
4 <comment></comment>
5 <projects>
6 </projects>
7 <buildSpec>
8 <buildCommand>
9 <name>org.eclipse.jdt.core.javabuilder</name>
10 <arguments>
11 </arguments>
12 </buildCommand>
13 </buildSpec>
14 <natures>
15 <nature>org.eclipse.jdt.core.javanature</nature>
16 </natures>
17</projectDescription>
1<?xml version=”1.0″ encoding=”UTF-8″?>
2<projectDescription>
3 <name>JavaConcepts</name>
4 <comment></comment>
5 <projects>
6 </projects>
7 <buildSpec>
8 <buildCommand>
9 <name>org.eclipse.jdt.core.javabuilder</name>
10 <arguments>
1 </arguments>
2 </buildCommand>
3 </buildSpec>
4 <natures>
5 <nature>org.eclipse.jdt.core.javanature</nature>
6 </natures>
7</projectDescription>
11<?xml version=”1.0″ encoding=”UTF-8″?>
12<projectDescription>
13 <name>JavaConcepts</name>
14 <comment></comment>
15 <projects>
16 </projects>
17 <buildSpec>
18 <buildCommand>
19 <name>org.eclipse.jdt.core.javabuilder</name>
20 <arguments>
21 </arguments>
22 </buildCommand>
23 </buildSpec>
24 <natures>
25 <nature>org.eclipse.jdt.core.javanature</nature>
26 </natures>
27</projectDescription>