Home Segments Index Top Previous Next

474: Mainline

Using nextLine and atEnd, you can arrange to read all the lines in a file. Note that the following definition of eatOnlyVegetables uses both methods, but does nothing with the lines other than to display them in the transcript:

Food method definition • class 
eatOnlyVegetables: aFile 
 |inputStream string| 
 inputStream := File pathName: aFile. 
 [inputStream atEnd] 
  whileFalse: 
   [Transcript show: (string := inputStream nextLine) printString; cr]. 
 inputStream close 
Workspace
Food eatOnlyVegetables: 'c:\test\foods.dta' 
Transcript 
'V Broccoli  3  16   9   0.5c            ' 
'D Butter   37   0   0   1.0tsp          avoid' 
'V Carrots   1  33   4   0.5c            good for rabbits' 
'V Corn      1  23   6   0.5c' 
'V Cabbage   2  14   3   0.5c' 
'D Milk     54  45  32   1.0c            whole milk' 
'' 
'' 

The first string happens to contain extra spaces after the last meaningful character. Also, there are two blank strings containing no characters.