dcsimg
To read some specific texts from a text file
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Albert_Leng
Posted On:   Thursday, February 7, 2002 01:56 PM

Hi there. I'm trying to make an application which reads from text file and use the data to load some images. What i'm trying to do is to read some lines like line 2 to line 4 or line5 to line 9... I tried to do using the following code but it doesnt work. It means the program just keep reading the same line (line 1) of the text file. Can anyone figure out how and tell me? Thanks a lot! try{ FileReader fR1 = new FileReader(file1); BufferedReader bR1 = new BufferedReader(fR1); line1 = bR1.readLine(); while(line1 != null) { if(0 < i&& i < 4){ tokenizer1 = new StringTokenizer(line1); endLeftName[i-1] = tokenizer1.nextToken(); Syst   More>>

Hi there.
I'm trying to make an application which reads from text file and use the data to load some images. What i'm trying to do is to read some lines like line 2 to line 4 or line5 to line 9... I tried to do using the following code but it doesnt work. It means the program just keep reading the same line (line 1) of the text file. Can anyone figure out how and tell me? Thanks a lot!

			
try{
FileReader fR1 = new FileReader(file1);
BufferedReader bR1 = new BufferedReader(fR1);

line1 = bR1.readLine();


while(line1 != null)
{

if(0 < i&& i < 4){
tokenizer1 = new StringTokenizer(line1);
endLeftName[i-1] = tokenizer1.nextToken();
System.out.println(i-1);
endLeft[i-1] = Toolkit.getDefaultToolkit().getImage(endLeftName[i-1]);//line 60
System.out.println(endLeftName[i-1]);
endLeftFName[i-1] = tokenizer1.nextToken();
endLeftF[i-1] = Toolkit.getDefaultToolkit().getImage(endLeftFName[i-1]);
endRightName[i-1] = tokenizer1.nextToken();
endRight[i-1] = Toolkit.getDefaultToolkit().getImage(endRightName[i-1]);
endRightFName[i-1] =tokenizer1.nextToken();
endRightF[i-1] = Toolkit.getDefaultToolkit().getImage(endRightFName[i-1]);
}
else if(4 < i && i < 10){
shortMidName[i-5] = tokenizer1.nextToken();
shortMid[i-5] = Toolkit.getDefaultToolkit().getImage(shortMidName[i-5]);
shortMidFName[i-5] = tokenizer1.nextToken();
shortMidF[i-5] = Toolkit.getDefaultToolkit().getImage(shortMidFName[i-5]);
}
line1 = bR1.readLine();
i++;

}

The other question is how can i parse a text file and when it meets a specific string, it will read the following lines? For example, let's say i have a text file like the following:
			
...
name:
Albert Daisy Kenny Alvin Bryan
...

That means when the application meets "name:", it will read the next lines...    <<Less

Re: To read some specific texts from a text file

Posted By:   David_Saintloth  
Posted On:   Thursday, February 7, 2002 04:25 PM

Ewww...


Rather than try to debug your code, I can provide you a link to code that is 99.5% what you need to do. You'll just have to modify the lines that will make the "fileRead()" method that I've written do what you want. (It shouldn't be difficult, let me know if you need more help.) Anyway HERE is a link to some threads that provide the previously mentioned method as well as others that can be modified to do exactly what you want. It's more code than what you have above, but 1) it works and 2) it is extremely modular and that's what OOP is all about!


Hope that helps you out.


Regards,

Dsl

About | Sitemap | Contact