本文整理汇总了C#中System.IO.StreamWriter类的典型用法代码示例。如果您正苦于以下问题:C# StreamWriter类的具体用法?C# StreamWriter怎么用?C# StreamWriter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
StreamWriter类属于System.IO命名空间,在下文中一共展示了StreamWriter类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace StreamReadWrite
{
class Program
{
static void Main(string[] args)
{
// Get the directories currently on the C drive.
DirectoryInfo[] cDirs = new DirectoryInfo(@"c:\").GetDirectories();
// Write each directory name to a file.
using (StreamWriter sw = new StreamWriter("CDriveDirs.txt"))
{
foreach (DirectoryInfo dir in cDirs)
{
sw.WriteLine(dir.Name);
}
}
// Read and show each line from the file.
string line = "";
using (StreamReader sr = new StreamReader("CDriveDirs.txt"))
{
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
}
}
示例2: new StreamWriter(String filePath)
//引入命名空间
using System;
using System.IO;
public class MyStreamWriterReader {
static void Main(string[] args) {
StreamWriter writer = new StreamWriter("reminders.txt");
writer.WriteLine("...");
writer.WriteLine("Don't forget these numbers:");
for (int i = 0; i < 10; i++)
writer.Write(i + " ");
writer.Write(writer.NewLine);
writer.Close();
StreamReader sr = new StreamReader("reminders.txt");
string input = null;
while ((input = sr.ReadLine()) != null) {
Console.WriteLine(input);
}
}
}
示例3: new StreamWriter(fs, Encoding.UTF8)
//引入命名空间
using System;
using System.IO;
using System.Text;
class MainClass {
static void Main() {
using (FileStream fs = new FileStream("test.txt", FileMode.Create)) {
using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8)) {
w.WriteLine(124.23M);
w.WriteLine("Test string");
w.WriteLine('!');
}
}
using (FileStream fs = new FileStream("test.txt", FileMode.Open)) {
using (StreamReader r = new StreamReader(fs, Encoding.UTF8)) {
Console.WriteLine(Decimal.Parse(r.ReadLine()));
Console.WriteLine(r.ReadLine());
Console.WriteLine(Char.Parse(r.ReadLine()));
}
}
}
}
示例4: new StreamWriter
//引入命名空间
using System;
using System.IO;
using System.Text;
class MainClass {
static void Main() {
using (FileStream fs = new FileStream("test.txt", FileMode.Create)) {
using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8)) {
w.WriteLine(124.23M);
w.WriteLine("Test string");
w.WriteLine('!');
}
}
Console.WriteLine("Press Enter to read the information.");
Console.ReadLine();
using (FileStream fs = new FileStream("test.txt", FileMode.Open)) {
using (StreamReader r = new StreamReader(fs, Encoding.UTF8)) {
Console.WriteLine(Decimal.Parse(r.ReadLine()));
Console.WriteLine(r.ReadLine());
Console.WriteLine(Char.Parse(r.ReadLine()));
}
}
}
}