0% found this document useful (0 votes)
44 views5 pages

New For Int Byte: + Bài 1: Filestream Filestream Filemode Fileaccess

The document contains code snippets demonstrating how to read and write files in C# using FileStream and StreamReader/StreamWriter. The first code block writes bytes to a file and then reads them back. The second code block prompts the user for input, writes it to a file, and then reads the file if selected. The third section discusses FileMode options. The remaining code defines a FileText class to encapsulate writing and reading data from a file. Main methods are provided to test writing and then reading data from a file using the FileText class.

Uploaded by

Đức Khôi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views5 pages

New For Int Byte: + Bài 1: Filestream Filestream Filemode Fileaccess

The document contains code snippets demonstrating how to read and write files in C# using FileStream and StreamReader/StreamWriter. The first code block writes bytes to a file and then reads them back. The second code block prompts the user for input, writes it to a file, and then reads the file if selected. The third section discusses FileMode options. The remaining code defines a FileText class to encapsulate writing and reading data from a file. Main methods are provided to test writing and then reading data from a file using the FileText class.

Uploaded by

Đức Khôi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

+ BÀi 1:

FileStream FF = new FileStream(@"files\test.dat", FileMode.OpenOrCreate,


FileAccess.ReadWrite);
for (int i = 1; i < 20; i++)
{
FF.WriteByte((byte)i);
}

Console.WriteLine("write ok");
FF.Position = 0;
for (int i = 1; i < 20; i++)
{
Console.Write(FF.ReadByte() + "\t");
}
FF.Close();
Console.ReadKey();

Bài 2:

string filepath = "1.txt";


Console.WriteLine("Nhap");
string input = Console.ReadLine();

using (StreamWriter sw = new StreamWriter("1.txt"))


{
sw.WriteLine(input);
}

Console.WriteLine("Your name was saved. Do you want to read it ? Y/N");


string choice = Console.ReadLine().ToUpper();

if ("Y".Equals(choice))
{
string data = string.Empty;
using (StreamReader sr = new StreamReader(filepath))
{
while ((data = sr.ReadLine()) != null)
{
Console.WriteLine(data);
}
}
}
Console.ReadKey();

Đáp án:

Bài 3: dung cả filestream vs stream //FileMode.Append: ghi tiep tuc con Create :
ghi de

Class FileText:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
class FileText
{
private string filename;

public string Filename


{
get { return filename; }
set { filename = value; }
}
private System.IO.FileStream fs;
public void WriteData()
{
fs = new System.IO.FileStream(Filename, FileMode.Create, FileAccess.Write,
FileShare.None);
StreamWriter sw = new StreamWriter(fs);
for (int i = 1; i < 10; i++)
{
Console.WriteLine("Moi nhap dong thu "+ i);
String str = Console.ReadLine();
sw.WriteLine(str);
}
sw.Flush();
sw.Close();
fs.Close();
}
public void ReadData()
{
fs = new System.IO.FileStream(Filename, FileMode.Open, FileAccess.Read,
FileShare.None);
StreamReader sr = new StreamReader(fs);
string str = sr.ReadLine();
while (str != null)
{
Console.WriteLine(str);
str = sr.ReadLine();
}
sr.Close();
fs.Close();
}
}
}

Ghi file:

Main

class Program
{
static void Main(string[] args)
{
FileText ft = new FileText();
ft.Filename = @"C:\demo.txt";
ft.WriteData();
Console.ReadLine();
}
}

Đáp án:
Doc file:

Main:

class Program
{
static void Main(string[] args)
{
FileText ft = new FileText();
ft.Filename = @"C:\demo.txt";
ft.ReadData();
Console.ReadLine();
}
}

You might also like