![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4
![]() |
Scanning data from a file
How can i scan the entire data from a file with undefined number of lines?
Right now, i'm using BefferdReader identifier = new BufferedReader (new FileReader(filename.format)); but that scans 1 line at a time. i want to scan the entire file. Last edited by Dark Flare Knight; Jun 13th, 2005 at 3:30 PM. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
You want to read in the whole file or what?
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4
![]() |
yes. i want to read the whole file
|
|
|
|
|
|
#4 |
|
Professional Programmer
|
String s="";
String currentLine;
try{
BufferedReader identifier = new BufferedReader (new FileReader("asd.txt"));
for(;;){
if((currentLine = identifier.readLine())==null)break;
s=s+currentLine+"\n";
}
System.out.println(s);
}catch (IOException e) {
System.err.println("Error: " + e);
}
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#5 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Why the break statement?
while ((currentLine = identifier.readLine()) != null) {
s = s + currentLine + "\n";
}Last edited by Ooble; Jun 14th, 2005 at 10:27 AM. |
|
|
|
|
|
#6 | |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4
![]() |
Quote:
__________________
"A stupid man's report of what a clever man says can never be accurate, because he unconciously translates what he hears into something he can understand." - B. Russell http://web.bryant.edu/~srk2 |
|
|
|
|
|
|
#7 |
|
Professional Programmer
|
Isn't that the same thing ? What are the benefits of while against for(;; ) ?
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#8 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
for(;; ) is faster as it doesn't have to compare anything.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#9 |
|
Programmer
|
But surely you have to compare something to drop out of the loop anyway.
__________________
kirkl_uk |
|
|
|
|
|
#10 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
I believe it's generally accepted that if you can avoid the use of the break keyword, your solution will be more elegant. Your solution could have been written for ( ; (currentLine = identifier.readLine()) != null; ) - it's the same as mine.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|