Bài viết này chúng ta sẽ tìm hiểu cách sử dụng lớp StreamReader để đọc hay tải một tập tin văn bản thành đối tượng String trong ngôn ngữ lập trình C#.
StreamReader Class
Thực hiện một TextReader(đại diện cho một người đọc có thể đọc một loạt các ký tự theo từng tự.) để đọc các ký tự từ một dòng byte được mã hóa. Hiểu đơn giản là nó đọc dữ liệu từ tâp tin theo dạng byte và chuyển đổi nó thành ký tự cụ thể.
Ví dụ - Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace LoadFileToString
{
class Program
{
static void Main(string[] args)
{
StreamReader streamReader = new StreamReader("D:\\VNLIVES.NET_DEMO.txt");
string text = streamReader.ReadToEnd();
streamReader.Close();
System.Console.WriteLine("Content Text File: " + text);
System.Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace LoadFileToString
{
class Program
{
static void Main(string[] args)
{
StreamReader streamReader = new StreamReader("D:\\VNLIVES.NET_DEMO.txt");
string text = streamReader.ReadToEnd();
streamReader.Close();
System.Console.WriteLine("Content Text File: " + text);
System.Console.ReadLine();
}
}
}
Kết quả - Result
No comments:
Post a Comment