File 为什么这里没有读取文件的第一行

File 为什么这里没有读取文件的第一行,file,smalltalk,gnu-smalltalk,File,Smalltalk,Gnu Smalltalk,我正在尝试以下代码来读取和打印目录中所有扩展名为.st的文件的第一行: (Directory working: '.') allFilesMatching: '*.st' do: [ :ff | line := (ff nextLine). "Read first line only." line displayNl. ff close. ] 但是,它不工作,并出现以下错误: $ gst firstline3.st "Global garbage collect

我正在尝试以下代码来读取和打印目录中所有扩展名为.st的文件的第一行:

(Directory working: '.')
allFilesMatching: '*.st' do: [ :ff |
    line := (ff nextLine).    "Read first line only."
    line displayNl.
    ff close. ]
但是,它不工作,并出现以下错误:

$ gst firstline3.st 
"Global garbage collection... done"
Object: nil error: did not understand #oldFileNamed:
MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
UndefinedObject(Object)>>doesNotUnderstand: #oldFileNamed: (SysExcept.st:1448)
optimized [] in UndefinedObject>>executeStatements (firstline3.st:3)
[] in Kernel.RecursiveFileWrapper(FilePath)>>filesMatching:do: (FilePath.st:903)
[] in Kernel.RecursiveFileWrapper>>namesDo:prefixLength: (VFS.st:378)
[] in File>>namesDo: (File.st:589)
BlockClosure>>ensure: (BlkClosure.st:268)
File>>namesDo: (File.st:586)
Kernel.RecursiveFileWrapper>>namesDo:prefixLength: (VFS.st:373)
Kernel.RecursiveFileWrapper>>namesDo: (VFS.st:396)
Kernel.RecursiveFileWrapper(FilePath)>>filesMatching:do: (FilePath.st:902)
File(FilePath)>>allFilesMatching:do: (FilePath.st:775)
Directory class>>allFilesMatching:do: (Directory.st:225)
UndefinedObject>>executeStatements (firstline3.st:2)
问题在哪里?谢谢你的帮助

编辑: @tucan回答中的以下代码无效:

(Directory working: '.') allFilesMatching: '*.st' do: [ :ff |
   file := FileStream open: ff mode: FileStream read.
   file nextLine printNl.       "I want to print it right away"
   file close
].
错误是:

$ gst firstline3.st 
"Global garbage collection... done"
Object: RecursiveFileWrapper new "<0x7f6decddd780>" error: did not understand #indexOfSubCollection:
MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
Kernel.RecursiveFileWrapper(Object)>>doesNotUnderstand: #indexOfSubCollection: (SysExcept.st:1448)
FileStream class(FileDescriptor class)>>open:mode:ifFail: (FileDescr.st:131)
FileStream class(FileDescriptor class)>>open:mode: (FileDescr.st:111)
optimized [] in UndefinedObject>>executeStatements (firstline3.st:2)
[] in Kernel.RecursiveFileWrapper(FilePath)>>filesMatching:do: (FilePath.st:903)
[] in Kernel.RecursiveFileWrapper>>namesDo:prefixLength: (VFS.st:378)
[] in File>>namesDo: (File.st:589)
BlockClosure>>ensure: (BlkClosure.st:268)
File>>namesDo: (File.st:586)
Kernel.RecursiveFileWrapper>>namesDo:prefixLength: (VFS.st:373)
Kernel.RecursiveFileWrapper>>namesDo: (VFS.st:396)
Kernel.RecursiveFileWrapper(FilePath)>>filesMatching:do: (FilePath.st:902)
File(FilePath)>>allFilesMatching:do: (FilePath.st:775)
Directory class>>allFilesMatching:do: (Directory.st:225)
UndefinedObject>>executeStatements (firstline3.st:1)
$gst firstline3.st
“全局垃圾收集…完成”
对象:RecursiveFileWrapper new“”错误:无法理解#indexOfSubCollection:
MessageNotUnderstanding(异常)>>信号(exHandling.st:254)
Kernel.RecursiveFileWrapper(Object)>>doesNotUnderstand:#indexOfSubCollection:(SysExcept.st:1448)
FileStream类(FileDescriptor类)>>打开:模式:ifFail:(FileDescr.st:131)
FileStream类(FileDescriptor类)>>打开:模式:(FileDescr.st:111)
在UndefinedObject>>executeStatements中优化了[](firstline3.st:2)
[]在Kernel.RecursiveFileWrapper(FilePath)>>文件匹配:do:(FilePath.st:903)
[]在Kernel.RecursiveFileWrapper>>名称中do:prefixLength:(VFS.st:378)
[]在文件中>>namesDo:(File.st:589)
区块关闭>>确保:(BlkClosure.st:268)
文件>>名称DO:(File.st:586)
Kernel.RecursiveFileWrapper>>名称do:prefixLength:(VFS.st:373)
Kernel.RecursiveFileWrapper>>namesDo:(VFS.st:396)
RecursiveFileWrapper(FilePath)>>文件匹配:do:(FilePath.st:902)
文件(FilePath)>>所有文件匹配:do:(FilePath.st:775)
目录类>>所有文件匹配:do:(Directory.st:225)
未定义对象>>执行语句(firstline3.st:1)

嗯。已解决:
FileStream open:(ff name)
有效。

我看不到您的代码中有任何地方在打开文件。要打开文件,需要在块中执行类似的操作

(Directory working: '.') allFilesMatching: '*.st' do: [ :ff |
   file := FileStream open: (ff name) mode: FileStream read.
   file nextLine. "How you want to print it here or somewhere else?"
   file close
].

@rnso啊,是的,没有时间在GNU Smalltalk env中运行它。这是一个不受约束的文件(我在前面的线程中已经讨论过)。你已经自己解决了,干得不错。