-----------------------------------------------------------------------------------
[Pharo Trick: #0008] - If appropriate use the memory file system 
-----------------------------------------------------------------------------------
Works in: Pharo 2.0, 3.0, ...
-----------------------------------------------------------------------------------

It may be possible that your application has to export some
data into an external file in a specific format. Pharo allows writing into
a file using Streams as any other Smalltalk.
 
But sometimes you want to check the result right from the Pharo image 
without having to look into the external file using external tools.

So the trick here is to use the memory file system instead 
of the real disk file system during development since also no 
cleanup on the hard disk is required afterwards:

|fs|
fs := FileSystem memory.
(fs / 'hello.txt') writeStreamDo: [:s| s nextPutAll: 'Exported data' ].
(fs / 'hello.txt') readStream contents.

The memory filesystem is also nice if you need to work with files
within a unit test. So when the test runs nothing remains left on the 
hard disk.

If you need a real copy of the file on the hard disk you can also copy 
from the memory file system to the disk file system using #copyTo:

|fs|
fs := FileSystem memory.
(fs / 'hello.txt') writeStreamDo: [:s| s nextPutAll: 'Memory first and then 
disk' ].
(fs / 'hello.txt') copyTo: (FileSystem disk / 'exactCopy.txt')


Reply via email to