# Reading and Writing Wolfram System Files

## Storing Wolfram Language Expressions in External Files

You can use files on your computer system to store definitions and results from the Wolfram Language. The most general approach is to store everything as plain text that is appropriate for input to the Wolfram Language. With this approach, a version of the Wolfram Language running on one computer system produces files that can be read by a version running on any computer system. In addition, such files can be manipulated by other standard programs, such as text editors.

<<file orGet["file"] | read in a file of Wolfram Language input, and return the last expression in the file |

FilePrint["file"] | display the contents of a file |

expr>>file orPut[expr,"file"] | write an expression to a file |

expr>>>file orPutAppend[expr,"file"] | append an expression to a file |

In[1]:= |

In[3]:= |

Out[3]= |

In[2]:= |

Out[2]= |

In[19]:= |

Out[19]= |

When you read in a file with <<file, the Wolfram Language returns the last expression it evaluates in the file. You can avoid getting any visible result from reading a file by ending the last expression in the file with a semicolon, or by explicitly adding Null after that expression.

If the Wolfram Language encounters a syntax error while reading a file, it reports the error, skips the remainder of the file, then returns $Failed. If the syntax error occurs in the middle of a package that uses BeginPackage and other context manipulation functions, then the Wolfram Language tries to restore the context to what it was before the package was read.

## Saving Multiple Wolfram Language Expressions

The Wolfram Language input files can contain any number of expressions. Each expression, however, must start on a new line. The expressions may continue for as many lines as necessary. Just as in a standard interactive Wolfram Language session, the expressions are processed as soon as they are complete. Note that in a file, unlike an interactive session, you can insert a blank line at any point without effect.

When you use , the Wolfram Language appends each new expression you give to the end of your file. If you use , however, then the Wolfram Language instead wipes out anything that was in the file before, and then puts expr into the file.

In[4]:= |

In[6]:= |

If you are familiar with command‐line operating systems, you will recognize the Wolfram Language redirection operators , , and as being analogous to the command‐line operators , , and .

## Saving Wolfram Language Expressions in Different Formats

When you use either or to write expressions to files, the expressions are usually given in Wolfram Language input format, so that you can read them back into the Wolfram Language. Sometimes, however, you may want to save expressions in other formats. You can do this by explicitly wrapping a format directive such as OutputForm around the expression you write out.

In[8]:= |

## Saving Definitions of Wolfram Language Objects

One of the most common reasons for using files is to save definitions of Wolfram Language objects, to be able to read them in again in a subsequent Wolfram Language session. The operators and allow you to save Wolfram Language expressions in files. You can use the function Save to save complete definitions of Wolfram Language objects in a form suitable for execution in subsequent Wolfram Language sessions.

Save["file",symbol] | save the complete definitions for a symbol in a file |

Save["file","form"] | save definitions for symbols whose names match the string pattern form |

Save["file","context`"] | save definitions for all symbols in the specified context |

Save["file",{object_{1},object_{2},…}] | save definitions for several objects |

Saving definitions in plain text files.

In[51]:= |

Out[51]= |

In[52]:= |

In[54]:= |

In[55]:= |

In[57]:= |

In[58]:= |

Out[58]= |

The function Save makes use of the output forms Definition and FullDefinition, which print as definitions of Wolfram Language symbols. In some cases, you may find it convenient to use these output forms directly.

In[59]:= |

Out[59]= |

In[60]:= |

Out[60]= |

When you define a new object in the Wolfram Language, your definition will often depend on other objects that you defined before. If you are going to be able to reconstruct the definition of your new object in a subsequent Wolfram Language session, it is important that you store not only its own definition, but also the definitions of other objects on which it depends. The function Save looks through the definitions of the objects you ask it to save, and automatically also saves all definitions of other objects on which it can see that these depend. However, in order to avoid saving a large amount of unnecessary material, Save never includes definitions for symbols that have the attribute Protected. It assumes that the definitions for these symbols are also built in. Nevertheless, with such definitions taken care of, it should always be the case that reading the output generated by Save back into a new Wolfram Language session will set up the definitions of your objects exactly as you had them before.

## Saving Wolfram Language Definitions in Encoded Form

When you create files for input to the Wolfram Language, you usually want them to contain only "plain text", which can be read or modified directly. Sometimes, however, you may want the contents of a file to be "encoded" so that they cannot be read or modified directly as plain text, but can be loaded into the Wolfram Language. You can create encoded files using the Wolfram Language function Encode.

Encode["source","dest"] | write an encoded version of the file source to the file dest |

<<dest | read in an encoded file |

Encode["source","dest","key"] | encode with the specified key |

Get["dest","key"] | read in a file that was encoded with a key |

Encode["source","dest",MachineID->"ID"] | create an encoded file that can only be read on a machine with a particular ID |

Creating and reading encoded files.

In[61]:= |

In[62]:= |

In[64]:= |

Out[64]= |

DumpSave["file.mx",symbol] | save definitions for a symbol in internal Wolfram Language format |

DumpSave["file.mx","context`"] | save definitions for all symbols in a context |

DumpSave["file.mx",{object_{1},object_{2},…}] | save definitions for several symbols or contexts |

DumpSave["package`",objects] | save definitions in a file with a specially chosen name |

Saving definitions in internal Wolfram System format.

If you have to read in very large or complicated definitions, you will often find it more efficient to store these definitions in internal Wolfram System format, rather than as text. You can do this using DumpSave.

In[22]:= |

Out[22]= |

In[23]:= |

recognizes when a file contains definitions in internal Wolfram System format, and operates accordingly. One subtlety is that the internal Wolfram System format differs from one computer system to another. As a result, files created on one computer cannot typically be read on another.

If you use DumpSave["package`",…] then the Wolfram Language will write out definitions to a file with a name like , where system identifies your type of computer system.

In[24]:= |

Out[24]= |

In[25]:= |