Reputation: 83
I have a text file that contains
"[PartA]
1
2
3
[PartB]
4
5
6
[PartC]
7
8
9"
what I have done so far is to read [PartA] only here's my code:
try
{
BufferedReader fw = new BufferedReader(new FileReader(new File(filename)));
while(!((content=fw.readLine()).equals("[PartB]")))
{
System.out.println(content);
}
}
catch(Exception e)
{
}
so how can I read only PartB or only PartC?
Upvotes: 0
Views: 3261
Reputation: 714
try
{
BufferedReader fw = new BufferedReader(new FileReader(new File(filename)));
while(!fw.readLine()).equals("[PartB]"){} //search for PartB
while(!(content=fw.readLine())).equals("[PartC]")){ //read till PartC
System.out.println(content);
}
}
catch(Exception e)
{
}
Upvotes: 0
Reputation: 441
You could say
boolean partB = false;
content = fw.readLine();
while(content != null) {
if(content.equals("[PartA]")) {
partB = false;
} else if (content.equals("[PartB]")) {
partB = true;
} else if (content.equals("[PartC]")) {
partB = false;
}
if (partB) {
System.out.println(content);
}
content = fw.readLine();
}
and follow that same logic for PartC
Upvotes: 3