Imports System.IO
Imports System.Text
Module Module1
Sub Main()
Dim fileName As String = "test.txt"
Dim textToAdd As String = "Example text in file"
Using writer As StreamWriter = New StreamWriter(fileName, True, Encoding.UTF8)
writer.Write(textToAdd)
End Using
End Sub
End Module
此构造函数使用编码
Encoding
参数初始化 属性。 有关附加信息,请参见
Encoding
。
path
可以是文件名,包括 UNC 共享中通用命名约定 () 文件。
path
不需要是磁盘上存储的文件;它可以是支持通过流访问的系统的任何部分。
使用特定文化设置编译一组字符,并使用不同的文化设置检索这些相同的字符时,这些字符可能无法解释,并可能导致引发异常。
有关常见 I/O 任务的列表,请参阅
常见 I/O 任务
。
public:
StreamWriter(System::String ^ path, System::Text::Encoding ^ encoding, System::IO::FileStreamOptions ^ options);
public StreamWriter (string path, System.Text.Encoding encoding, System.IO.FileStreamOptions options);
new System.IO.StreamWriter : string * System.Text.Encoding * System.IO.FileStreamOptions -> System.IO.StreamWriter
Public Sub New (path As String, encoding As Encoding, options As FileStreamOptions)
public:
StreamWriter(System::IO::Stream ^ stream, System::Text::Encoding ^ encoding, int bufferSize, bool leaveOpen);
public StreamWriter (System.IO.Stream stream, System.Text.Encoding encoding, int bufferSize, bool leaveOpen);
public StreamWriter (System.IO.Stream stream, System.Text.Encoding? encoding = default, int bufferSize = -1, bool leaveOpen = false);
new System.IO.StreamWriter : System.IO.Stream * System.Text.Encoding * int * bool -> System.IO.StreamWriter
Public Sub New (stream As Stream, encoding As Encoding, bufferSize As Integer, leaveOpen As Boolean)
Public Sub New (stream As Stream, Optional encoding As Encoding = Nothing, Optional bufferSize As Integer = -1, Optional leaveOpen As Boolean = false)
string fileName = "test.txt";
string textToAdd = "Example text in file";
FileStream fs = null;
fs = new FileStream(fileName, FileMode.CreateNew);
using (StreamWriter writer = new StreamWriter(fs, Encoding.UTF8, 512, false))
writer.Write(textToAdd);
finally
if (fs != null)
fs.Dispose();
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
Dim fileName As String = "test.txt"
Dim textToAdd As String = "Example text in file"
Dim fs As FileStream = Nothing
fs = New FileStream(fileName, FileMode.CreateNew)
Using writer As StreamWriter = New StreamWriter(fs, Encoding.Default, 512, False)
writer.Write(textToAdd)
End Using
Finally
If Not fs Is Nothing Then
fs.Dispose()
End If
End Try
End Sub
End Module
除非您设置
leaveOpen
参数
true
,则
StreamWriter
对象调用
Dispose()
上提供
Stream
对象时
StreamWriter.Dispose
调用。
此构造函数使用 参数初始化 属性,然后使用
Encoding
encoding
BaseStream
参数初始化
stream
属性。 流的位置未重置。 有关其他信息,请参阅
Encoding
属性。
使用特定文化设置编译一组字符,并使用不同的文化设置检索这些相同的字符时,这些字符可能无法解释,并可能导致引发异常。
public:
StreamWriter(System::String ^ path, bool append, System::Text::Encoding ^ encoding, int bufferSize);
public StreamWriter (string path, bool append, System.Text.Encoding encoding, int bufferSize);
new System.IO.StreamWriter : string * bool * System.Text.Encoding * int -> System.IO.StreamWriter
Public Sub New (path As String, append As Boolean, encoding As Encoding, bufferSize As Integer)
string textToAdd = "Example text in file";
using (StreamWriter writer = new StreamWriter(fileName, true, Encoding.UTF8, 512))
writer.Write(textToAdd);
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
Dim fileName As String = "test.txt"
Dim textToAdd As String = "Example text in file"
Using writer As StreamWriter = New StreamWriter(fileName, True, Encoding.UTF8, 512)
writer.Write(textToAdd)
End Using
End Sub
End Module
此构造函数使用编码
Encoding
参数初始化 属性。 有关附加信息,请参见
Encoding
。
path
可以是文件名,包括 UNC 共享中通用命名约定 () 文件。
path
不需要是磁盘上存储的文件;它可以是支持通过流访问的系统的任何部分。
使用特定文化设置编译一组字符,并使用不同的文化设置检索这些相同的字符时,这些字符可能无法解释,并可能导致引发异常。
有关常见 I/O 任务的列表,请参阅
常见 I/O 任务
。