This is documentation for Mathematica 6, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)

Introduction to Reading and Writing Mathematica Files

You can use files on your computer system to store definitions and results from Mathematica. The most general approach is to store everything as plain text that is appropriate for input to Mathematica. With this approach, a version of Mathematica 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.
<<nameread in a Mathematica input file
expr>>nameoutput expr to a file as plain text
expr>>>nameappend expr to a file
FilePrint["name"]display the contents of a plain text file

Reading and writing files.

This expands (x+y)3, and outputs the result to a file called tmp.
Click for copyable input
Here are the contents of tmp. They can be used directly as input for Mathematica.
This reads in tmp, evaluating the Mathematica input it contains.
Click for copyable input
If you are familiar with command-line operating systems, you will recognize the Mathematica redirection operators >>, >>> and << as being analogous to the command-line operators >, >> and <.
The redirection operators >> and >>> are convenient for storing results you get from Mathematica. The function Save["name", f, g, ...] allows you to save definitions for variables and functions.
Save["name",f,g,...]save definitions for variables or functions in a file

Saving definitions in plain text files.

Here is a definition for a function f.
Click for copyable input
This gives c the value 17.
Click for copyable input
This saves the definition of f in the file ftmp.
Click for copyable input
Mathematica automatically saves both the actual definition of f, and the definition of c on which it depends.
This clears the definitions of f and c.
Click for copyable input
You can reinstate the definitions you saved simply by reading in the file ftmp.
Click for copyable input
file.mMathematica expression file in plain text format
file.nbMathematica notebook file
file.mxMathematica definitions in DumpSave format

Typical names of Mathematica files.

If you use a notebook interface to Mathematica, then the Mathematica front end allows you to save complete notebooks, including not only Mathematica input and output, but also text, graphics and other material.
It is conventional to give Mathematica notebook files names that end in .nb, and most versions of Mathematica enforce this convention.
When you open a notebook in the Mathematica front end, Mathematica will immediately display the contents of the notebook, but it will not normally send any of these contents to the kernel for evaluation until you explicitly request this to be done.
Within a Mathematica notebook, however, you can use the Cell menu in the front end to identify certain cells as initialization cells, and if you do this, then the contents of these cells will automatically be evaluated whenever you open the notebook.
The I in the cell bracket indicates that the second cell is an initialization cell that will be evaluated whenever the notebook is opened.
It is sometimes convenient to maintain Mathematica material both in a notebook which contains explanatory text, and in a package which contains only raw Mathematica definitions. You can do this by putting the Mathematica definitions into initialization cells in the notebook. Every time you save the notebook, the front end will then allow you to save an associated .m file which contains only the raw Mathematica definitions.