Some standard directories
FileLocator home. "User's home directory (~)"
FileLocator root. "File system's root (/)"
FileLocator C. "Windows C:\"
Accessing the children of a directory
| home |
home := FileLocator home.
home pathString
> '/home/cassou/'
home children
> an Array({home}/.bashrc {home}/Music ...)
Use message /
to build a path
(home / 'Music') directories
> an Array({home}/Music/Anouar_Brahem ...)
Use parent
to go up
(home / 'Music') parent pathString
> '/home/cassou/'
| music |
music := FileLocator home / 'Music'.
(music / 'Pink_Floyd') isDirectory.
> false
(music / 'Pink_Floyd') ensureCreateDirectory.
(music / 'Pink_Floyd') isDirectory.
> true
(music / 'Pink_Floyd') delete.
(music / 'Pink_Floyd') isDirectory
> false
(pinkFloyd / 'Meddle') allChildrenMatching: '*.ogg'.
is equivalent to
(pinkFloyd / 'Meddle') allChildren
select: [ :each | each basename endsWith: 'ogg' ]
| pharo |
pharo := 'pharo5.image' asFileReference.
pharo isFile.
> true
pharo basename
> 'pharo5.image'
pharo extension
> 'image'
pharo size
> 23744868
pharo parent pathString
> '/home/cassou/Pharo/images/pharo5'
| hello stream |
hello := 'hello.txt' asFileReference.
hello exists
> false
stream := hello writeStream.
stream nextPutAll: 'Hello World'.
stream close.
| hello stream |
hello := 'hello.txt' asFileReference.
hello exists
> true
stream := hello readStream.
stream next.
> $H
stream upToEnd.
> 'ello World'
stream close
| hello |
hello := 'hello.txt' asFileReference.
Writing
hello writeStreamDo: [ :stream |
stream nextPutAll: 'Hello World'].
Reading
hello readStreamDo: [ :stream | stream contents ]
> 'Hello World'
/