学习记录
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.IO;
namespace SrcFile
{
public class FileReaderWriter
{
public void ReaderFile()
{
try
{
using (StreamReader str = new StreamReader("F:\\code\\C#\\ConsoleApplication3\\src\\test.txt"))
{
string line;
while ((line = str.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch (Exception e)
{
Console.WriteLine("The file could not read:");
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
public void WriteFile()
{
string[] names = new string[]{"aaaa aafff1", "sdfsfsdfff1"};
using (StreamWriter sw = new StreamWriter("F:\\code\\C#\\ConsoleApplication3\\src\\test.txt", true))
{
foreach (string s in names)
{
sw.WriteLine(s);
}
}
string line = "";
using (StreamReader sr = new StreamReader("F:\\code\\C#\\ConsoleApplication3\\src\\test.txt"))
{
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
Console.ReadKey();
}
}
}
}
|