# F Sharp Programming/Input and Output

Input and output, also called I/O, refers to any kind of communication between two hardware devices or between the user and the computer. This includes printing text out to the console, reading and writing files to disk, sending data over a socket, sending data to the printer, and a wide variety of other common tasks.
This page is not intended to provide an exhaustive look at .NET's I/O methods (readers seeking exhaustive references are encouraged to review the excellent documentation on the System.IO namespace on MSDN). This page will provide a cursory overview of some of the basic methods available to F# programmers for printing and working with files.

## Working with the Console
### With F#

By now, you're probably familiar with the printf, printfn, sprintf and its variants in the Printf module. However, just to describe these methods more formally, these methods are used for printf-style printing and formatting using % markers as placeholders:
Print methods take a format string and a series of arguments, for example:
Methods in the Printf module are type-safe. For example, attempting to use substitute an int placeholder with a string results in a compilation error:
According to the F# documentation, % placeholders consist of the following:
    %[flags][width][.precision][type]
[flags] (optional)
Valid flags are:
    0: add zeros instead of spaces to make up the required width
    '-': left justify the result within the width specified
    '+': add a '+' character if the number is positive (to match a '-' sign for negatives)
    ' ': add an extra space if the number is positive (to match a '-' sign for negatives)
[width] (optional)
The optional width is an integer indicating the minimal width of the result. For instance, %6d prints an integer, prefixing it with spaces to fill at least 6 characters. If width is '*', then an extra integer argument is taken to specify the corresponding width.
    any number
[.precision] (optional)
Represents the number of digits after a floating point number.
[type] (required)
The following placeholder types are interpreted as follows:
Programmers can print to the console using the printf method, however F# recommends reading console input using the System.Console.ReadLine method.

### With .NET

.NET includes its own notation for format specifiers. .NET format strings are untyped. Additionally, .NET's format strings are designed to be extensible, meaning that a programmer can implement their own custom format strings. Format placeholders have the following form:
    {index[, length][:formatString]}
For example, using the String.Format method in fsi:
See Number Format Strings, Date and Time Format Strings, and Enum Format Strings for a comprehensive reference on format specifiers for .NET.
Programmers can read and write to the console using the System.Console class:

## System.IO Namespace

The System.IO namespace contains a variety of useful classes for performing basic I/O.

### Files and Directories

The following classes are useful for interrogating the host filesystem:
* The System.IO.File class exposes several useful members for creating, appending, and deleting files.
* System.IO.Directory exposes methods for creating, moving, and deleting directories.
* System.IO.Path performs operations on strings which represent file paths.
* System.IO.FileSystemWatcher which allows users to listen to a directory for changes.

### Streams

A stream is a sequence of bytes. .NET provides some classes which allow programmers to work with streams including:
* System.IO.StreamReader which is used to read characters from a stream.
* System.IO.StreamWriter which is used to write characters to a stream.
* System.IO.MemoryStream which creates an in-memory stream of bytes.