Created
August 22, 2017 20:54
-
-
Save mr-aleks/70dd35fe6f28716780ba114e3873b9cc to your computer and use it in GitHub Desktop.
Чтения-Запись файла
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try | |
{ | |
using (FileStream FS = new FileStream(path, FileMode.CreateNew)) | |
{ | |
Console.WriteLine("Файл " + Path.GetFileName(path) + " создал с помощью FileStream"); | |
Console.WriteLine("Введите текст для последующей записи в файл"); | |
StringReader SR = new StringReader(Console.ReadLine()); | |
StreamWriter SW = new StreamWriter(FS); | |
SW.Write(SR.ReadToEnd()); | |
SW.Flush(); | |
//string line; | |
//while ((line = sr.ReadLine()) != null) | |
//{ | |
// Console.WriteLine(line); | |
//} | |
} | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e.Message); | |
if (File.Exists(path)) | |
{ | |
File.Delete(path); | |
Console.WriteLine("Файл " + Path.GetFileName(path) + " удален"); | |
} | |
} | |
using (FileStream FS = new FileStream(path, FileMode.Create)) | |
{ | |
using (StreamWriter file = new StreamWriter(FS)) | |
{ | |
while (true) | |
{ | |
string newsLine; | |
if (Console.ReadLine() != "end") | |
{ | |
newsLine = Console.ReadLine(); | |
file.WriteLine(newsLine); | |
} | |
else | |
{ | |
Console.WriteLine("Выход из записи"); | |
break; | |
} | |
} | |
} | |
} | |
using (StreamWriter sw = File.CreateText(path)) | |
{ | |
while (true) | |
{ | |
if (Console.ReadLine() != "end") | |
{ | |
sw.WriteLine(Console.ReadLine()); | |
} | |
else | |
{ | |
Console.WriteLine("Выход из записи"); | |
break; | |
} | |
} | |
} | |
WORK | |
using (FileStream FS = new FileStream(path, FileMode.Create)) | |
{ | |
string newsLine; | |
using (StreamWriter file = new StreamWriter(FS, Encoding.Default, 512, false)) | |
{ | |
do | |
{ | |
newsLine = Console.ReadLine(); | |
file.WriteLine(newsLine); | |
} | |
while (newsLine != ""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment