File IO

Sometimes it is necessary to write data to a file or to read data from a file. An
expression can be stored in a file using the redirection arrows, >>. For example,

In[70]:=

  { 2, 3, 5, 7, 11, 13, 17, 19,23}>> primefile

will create a file called primefile where the list will be stored. This can be read into
any expression using the ReadList command.

In[71]:=

  primes= ReadList[ "primefile", Expression]

Out[71]

Notice that this has placed an extra set of braces around the input. The extra
set of braces can be removed using the Flatten command, which removes all the
braces (except the outermost ones) from a list.

In[72]:=

  primes= Flatten[ primes]

Out[72]

To add to a file we can use the PutAppend command or its short form, >>>.

In[73]:=

  { 29, 31, 37, 41, 43, 47} >>> primefile

In[74]:=

  primes= Flatten[ ReadList[ "primefile", Expression]]

Out[74]

Up to Tutorial