Java File reader - Reading file line by line and compare some content of one line with rest of the lines
I would like to read file line by line and compare them.
for example
file.tex ChapterInput[x,y,z] sectionInput[x,y,z] sectionInput[x,y] sectionInput[z,k,m] ChapterInput[k,d,l] sectionInput[k,d,h] sectionInput[l] sectionInput[l,r]
if i read line AAA and wanted to check whether ChapterInput[x,y,z] contains all the letters(in [x,y,z]) in lines sectionInput[x,y,z],sectionInput[x,y],sectionInput[x,k,m] if not i wanted to include missing letter (ex: k,m is missing in ChapterInput) so file will be re written as follows
file.tex
ChapterInput[x,y,z,k,m] sectionInput[x,y,z] sectionInput[x,y] sectionInput[z,k,m] ChapterInput[k,d,l,h,r] sectionInput[k,d,h] sectionInput[l] sectionInput[l,r]
import java.util.Scanner; import java.io.; import java.util.;//StringTokenizer; import java.util.Stack; import java.util.regex.*; import java.io.LineNumberReader;
class sample {
public static void main (String args[])
{
String line;
String nextData = null;
int lineCount=0;
try{
File file=new File("waste.tex");
Scanner scanFile = new Scanner(file);
boolean flag = true;
while(scanFile.hasNextLine())
{
lineCount++;
line= scanFile.nextLine();
StringTokenizer tok = new StringTokenizer(line);
while(tok.hasMoreElements())
{
nextData = tok.nextToken();
if(nextData.contains("chapterInput"))
{
System.out.println("Line : "+lineCount+ ": "+ nextData);
Pattern p1 = Pattern.compile(\\[(.*?)\\]);
Matcher m1 = p1.matcher(nextData);
while(m1.find())
{ System.out.println(m1.group(1)); }
String nxt_line = scanFile.nextLine();
Scanner scanFile_1 = new Scanner(file);
while((scanFile_1.hasNextLine()) && (flag=true) )
{
System.out.println("");
if(nxt_line.contains("sectionInput"))
{
System.out.println("Line : "+lineCount+ ": "+nxt_line);
Pattern p2= Pattern.compile(\\[(.*?)\\]);
Matcher m2=p2.matcher(nxt_line);
while (m2.find())
{System.out.println(m2.group(1));}
}
else
{flag=false;}
}
}
}
}
}
catch(Exception e){e.printStackTrace();}
} }
But i cannot Access lines in between chapterInputs.
Thanks in Advance
Uma
I would like to read file line by line and compare them.
for example
file.tex ChapterInput[x,y,z] sectionInput[x,y,z] sectionInput[x,y] sectionInput[z,k,m] ChapterInput[k,d,l] sectionInput[k,d,h] sectionInput[l] sectionInput[l,r]
if i read line AAA and wanted to check whether ChapterInput[x,y,z] contains all the letters(in [x,y,z]) in lines sectionInput[x,y,z],sectionInput[x,y],sectionInput[x,k,m] if not i wanted to include missing letter (ex: k,m is missing in ChapterInput) so file will be re written as follows
file.tex
ChapterInput[x,y,z,k,m] sectionInput[x,y,z] sectionInput[x,y] sectionInput[z,k,m] ChapterInput[k,d,l,h,r] sectionInput[k,d,h] sectionInput[l] sectionInput[l,r]
import java.util.Scanner; import java.io.; import java.util.;//StringTokenizer; import java.util.Stack; import java.util.regex.*; import java.io.LineNumberReader;
class sample {
public static void main (String args[])
{
String line;
String nextData = null;
int lineCount=0;
try{
File file=new File("waste.tex");
Scanner scanFile = new Scanner(file);
boolean flag = true;
while(scanFile.hasNextLine())
{
lineCount++;
line= scanFile.nextLine();
StringTokenizer tok = new StringTokenizer(line);
while(tok.hasMoreElements())
{
nextData = tok.nextToken();
if(nextData.contains("chapterInput"))
{
System.out.println("Line : "+lineCount+ ": "+ nextData);
Pattern p1 = Pattern.compile(\\[(.*?)\\]);
Matcher m1 = p1.matcher(nextData);
while(m1.find())
{ System.out.println(m1.group(1)); }
String nxt_line = scanFile.nextLine();
Scanner scanFile_1 = new Scanner(file);
while((scanFile_1.hasNextLine()) && (flag=true) )
{
System.out.println("");
if(nxt_line.contains("sectionInput"))
{
System.out.println("Line : "+lineCount+ ": "+nxt_line);
Pattern p2= Pattern.compile(\\[(.*?)\\]);
Matcher m2=p2.matcher(nxt_line);
while (m2.find())
{System.out.println(m2.group(1));}
}
else
{flag=false;}
}
}
}
}
}
catch(Exception e){e.printStackTrace();}
} }
But i cannot Access lines in between chapterInputs.
Thanks in Advance
Uma
No comments:
Post a Comment