({{, (RefLink[OpenWrite, paclet:ref/OpenWrite])["file"]
opens a file to write output to it, and returns an (RefLink[OutputStream, paclet:ref/OutputStream]) object. }, {, (RefLink[OpenWrite, paclet:ref/OpenWrite])[]
opens a new file in the default area for temporary files on your computer system.}})

OpenersNotesSection(Spacer1)Details and Options

(RefLink[OpenWrite, paclet:ref/OpenWrite]) deletes any existing contents in a file, and prepares to write output starting at the beginning of the file.

The following options can be given:

On computer systems that support pipes, (RefLink[OpenWrite, paclet:ref/OpenWrite])["!command"] runs the external program specified by command, and opens a pipe to send input to it.

If (RefLink[OpenWrite, paclet:ref/OpenWrite]) does not succeed in opening a particular file or pipe, it generates a message and returns (RefLink[$Failed, paclet:ref/$Failed]).

(RefLink[OpenWrite, paclet:ref/OpenWrite]) resolves file names according to the procedure described in "Files and Streams".

(RefLink[OpenWrite, paclet:ref/OpenWrite]) returns (RefLink[OutputStream, paclet:ref/OutputStream])["name",n], where name is the full name of a file or command, and n is a serial number that is unique across all streams opened in the current Wolfram System session.

(RefLink[SetOptions, paclet:ref/SetOptions]) can be used to change the properties of an output stream after it is already open.

Functions like (RefLink[Put, paclet:ref/Put]) and (RefLink[Write, paclet:ref/Write]) automatically open the files or pipes they need, if they are not already open.

With the default setting (RefLink[BinaryFormat, paclet:ref/BinaryFormat])->(RefLink[False, paclet:ref/False]), the setting for (RefLink[CharacterEncoding, paclet:ref/CharacterEncoding]) is taken to be "ASCII".

(RefLink[OpenWrite, paclet:ref/OpenWrite])[] always creates a new file that does not already exist, in the directory given by (RefLink[$TemporaryDirectory, paclet:ref/$TemporaryDirectory]).

With the (RefLink[Method, paclet:ref/Method]) option, the stream is opened using the given input stream method. This overrides the default way that (RefLink[OpenWrite, paclet:ref/OpenWrite]) resolves file names.

(RefLink[OpenWrite, paclet:ref/OpenWrite])[(RefLink[File, paclet:ref/File])["file"]] is also supported.

OpenersPrimaryExamplesSection(Spacer1)Examples  (2)

OpenersExampleSection0(Spacer1)Basic Examples  (1)

Create and open a new stream for writing:

fname=FileNameJoin[{$TemporaryDirectory,"testfile"}];

s=OpenWrite[fname]

Write an expression to the stream:

Write[s,Expand[(x+y)^10]]

Close the stream:

Close[s]

Print the resulting file:

FilePrint[FileNameJoin[{$TemporaryDirectory,"testfile"}]]

OpenersExampleSection1(Spacer1)Scope  (1)

Write an expression to the file specified by the (RefLink[File, paclet:ref/File]) object:

file =File[CreateFile[]]

str=OpenWrite[file];

Write[str, x^2 - y^2];

Close[str];

FilePrint[file]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[OpenAppend, paclet:ref/OpenAppend])  ▪  (RefLink[OpenRead, paclet:ref/OpenRead])  ▪  (RefLink[Close, paclet:ref/Close])  ▪  (RefLink[Put, paclet:ref/Put])  ▪  (RefLink[Streams, paclet:ref/Streams])  ▪  (RefLink[LinkCreate, paclet:ref/LinkCreate])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Streams and Low-Level Input and Output, paclet:tutorial/StreamsAndLowLevelInputAndOutput])}, {▪(RefLinkPlain[Stream Methods, paclet:tutorial/StreamMethods])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Files, paclet:guide/Files])}, {▪(RefLinkPlain[File Operations, paclet:guide/FileOperations])}, {▪(RefLinkPlain[Basic Input & Output in Programs, paclet:guide/BasicInputAndOutputInPrograms])}, {▪(RefLinkPlain[Low-Level File Operations, paclet:guide/LowLevelFileOperations])}})}})