开发者

How to open a txt File in Squeak4.1

开发者 https://www.devze.com 2023-02-10 07:05 出处:网络
Friend, How can open a txt file in Squeak4.1 ,the code Shall be like this: at: #f put: (FileStreamopen: \'/root/test\'mode: FileStream rea开发者_JS百科d) !

Friend, How can open a txt file in Squeak4.1 ,the code Shall be like this:

at: #f put: (FileStream  open: '/root/test'  mode: FileStream rea开发者_JS百科d) !
f do: [ :c | Transcript nextPut: c ] !
f close !

can any body give some hints on how to open the file and do the + - * / equation ? thanks first :)


I'd use one of these methods...

fileContents := FileStream 
                   readOnlyFileNamed: '/root/test' 
                   do: [:f | f contents ].

Using the block form above automatically closes the file, you can't forget. Or..

fileContents := (FileStream readOnlyFileNamed: '/root/test') 
                    contentsOfEntireFile.

#contentsOfEntireFile also automatically closes the file, you don't need to do it again.

In a language with blocks, it just makes no sense to manually close the stream when higher order methods are available that ensure you don't have to do so.


This should work:

|file fileContents|
file := FileStream fileNamed: '/root/test'.
fileContents := file contentsOfEntireFile.
file close.


|f|
f:=StandardFileStream fileNamed: 'myFile.txt'.
Transcript show: f upToEnd.
f close.

I use StandardFileStream for raw input without UTF-8 detection and read upToEnd because reading single characters is not considered apropriate.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号