Trang chủ
Bài viết mới
Diễn đàn
Bài mới trên hồ sơ
Hoạt động mới nhất
VIDEO
Mùa Tết
Văn Học Trẻ
Văn Học News
Media
New media
New comments
Search media
Đại Học
Đại cương
Chuyên ngành
Triết học
Kinh tế
KHXH & NV
Công nghệ thông tin
Khoa học kĩ thuật
Luận văn, tiểu luận
Phổ Thông
Lớp 12
Ngữ văn 12
Lớp 11
Ngữ văn 11
Lớp 10
Ngữ văn 10
LỚP 9
Ngữ văn 9
Lớp 8
Ngữ văn 8
Lớp 7
Ngữ văn 7
Lớp 6
Ngữ văn 6
Tiểu học
Thành viên
Thành viên trực tuyến
Bài mới trên hồ sơ
Tìm trong hồ sơ cá nhân
Credits
Transactions
Xu: 0
Đăng nhập
Đăng ký
Có gì mới?
Tìm kiếm
Tìm kiếm
Chỉ tìm trong tiêu đề
Bởi:
Hoạt động mới nhất
Đăng ký
Menu
Đăng nhập
Đăng ký
Install the app
Cài đặt
Chào mừng Bạn tham gia Diễn Đàn VNKienThuc.com -
Định hướng Forum
Kiến Thức
- HÃY TẠO CHỦ ĐỀ KIẾN THỨC HỮU ÍCH VÀ CÙNG NHAU THẢO LUẬN Kết nối:
VNK X
-
VNK groups
| Nhà Tài Trợ:
BhnongFood X
-
Bhnong groups
-
Đặt mua Bánh Bhnong
CÔNG NGHỆ
Công Nghệ Thông Tin
Code
Sử dụng File I/O trong C# như thế nào?
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Trả lời chủ đề
Nội dung
<blockquote data-quote="uocmo_kchodoi" data-source="post: 177570" data-attributes="member: 165510"><p><a href="https://vnkienthuc.com/forums/hoi-dap-tin-hoc.378/" target="_blank"><span style="font-size: 18px">Một tập tin </span></a><span style="font-size: 18px">là một tập hợp các dữ liệu được lưu trữ trong một đĩa với một tên cụ thể và một đường dẫn thư mục. Khi một tập tin được mở để đọc hoặc viết, nó sẽ trở thành một luồng tin.</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Các luồng tin cơ bản là trình tự của byte đi qua <a href="https://vnkienthuc.com/forums/code.377/" target="_blank">con đường truyền thông</a>. Có hai luồng chính: luồng đầu vào và luồng đầu ra. Các luồng đầu vào được sử dụng để đọc dữ liệu từ tập tin (đọc hoạt động) và các luồng sản lượng được sử dụng cho việc ghi lên tập tin (ghi hoạt động).</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #0000ff"><strong>1. Lớp I/O trong C#</strong></span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">System.IO namespace có nhiều lớp đa dạng mà được sử dụng để thực hiện các hoạt động khác nhau với File, như tạo và xóa file, đọc và ghi một File, đóng một File, …</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Bảng sau hiển thị một số lớp non-abstract được sử dụng phổ biến tron<a href="https://vnkienthuc.com/threads/tim-hieu-ve-indexer-trong-c.77355/" target="_blank">g System.IO namespace trong C#:</a></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><strong>I/O Class:</strong></span></p><p><span style="font-size: 18px"><span style="color: #ff8000">BinaryReader: </span> Đọc dữ liệu gốc (primitive data) từ một binary stream</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">BinaryWriter: </span> Ghi dữ liệu gốc trong định dạng nhị phân</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">BufferedStream:</span> Một nơi lưu giữ tạm thời cho một stream</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">Directory:</span> Giúp ích trong việc thao tác một cấu trúc thư mục</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">DirectoryInfo:</span> Được sử dụng để thực hiện các hoạt động trên các thư mục</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">DriveInfo: </span>Cung cấp thông tin cho các Drive</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">File</span>: Giúp ích trong việc thao tác các File</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">FileInfo:</span> Được sử dụng để thực hiện các hoạt động trên các File</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">FileStream:</span> Được sử dụng để đọc và ghi bất kỳ vị trí nào trong một File</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">MemoryStream:</span> Được sử dụng để truy cập ngẫu nhiên tới stream được lưu giữ trong bộ nhớ</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">Path</span>: Thực hiện các hoạt động trên thông tin path</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">StreamReader</span>: Được sử dụng để đọc các ký tự từ một stream</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">StreamWriter</span>: Được sử dụng để ghi các ký tự tới một stream</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">StringReader</span>: Được sử dụng để đọc từ một string buffer</span></p><p><span style="font-size: 18px"><span style="color: #ff8000">StringWriter:</span> Được sử dụng để ghi vào một string buffer</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><span style="color: #0000ff"><strong>2. Lớp FileStream trong C#</strong></span></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><a href="https://vnkienthuc.com/threads/tim-hieu-ve-delegate-trong-c.77356/" target="_blank">Lớp FileStream </a>trong System.IO namespace trong C# giúp đỡ trong việc đọc từ, ghi và đóng các File. Lớp này kế thừa từ lớp abstract là Stream.</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Bạn cần tạo một đối tượng FileStream để tạo một File mới hoặc mở một File đang tồn tại. Cú pháp để tạo một đối tượng FileStream trong C# như sau:</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">FileStream <tên_đối_tượng> = new FileStream( <tên_file>, <FileMode>, <FileAccess>, <FileShare>);</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Ví dụ: chúng ta tạo một đối tượng FileStream là F để đọc một File với tên sample.txt, như sau:</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">FileStream F = new FileStream("sample.txt", FileMode.Open, FileAccess.Read, FileShare.Read);</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><strong>Tham số:</strong></span></p><p><span style="font-size: 18px"><strong><em>* FileMode </em></strong></span></p><p><span style="font-size: 18px">- FileMode enumerator định nghĩa các phương thức đa dạng để mở các File.</span></p><p><span style="font-size: 18px">- Các thành viên của FileMode enumerator là:</span></p><ul> <li data-xf-list-type="ul"><span style="font-size: 18px">Append: Nó mở một File đang tồn tại và đặt con trỏ tại phần cuối của File, hoặc tạo File, nếu File đó chưa tồn tại<br /> </span></li> <li data-xf-list-type="ul"><span style="font-size: 18px">Create: Nó tạo một File mới<br /> </span></li> <li data-xf-list-type="ul"><span style="font-size: 18px">CreateNew: Nó xác định tới Hệ điều hành rằng nó nên tạo một File mới<br /> </span></li> <li data-xf-list-type="ul"><span style="font-size: 18px">Open: Nó mở một File đang tồn tại<br /> </span></li> <li data-xf-list-type="ul"><span style="font-size: 18px">OpenOrCreate: Nó xác định tới Hệ điều hành rằng nó nên mở một File nếu nó tồn tại, nếu không thì nó nên tạo một File mới<br /> </span></li> <li data-xf-list-type="ul"><span style="font-size: 18px">Truncate: Nó mở một File đang tồn tại và truncate (cắt) kích cỡ của nó về 0 byte</span></li> </ul><p><span style="font-size: 18px"><strong><em>* FileAccess </em></strong></span></p><p><span style="font-size: 18px">- FileAccess enumerators có các thành viên là: Read, ReadWrite và Write.</span></p><p><span style="font-size: 18px"><strong><em>* FileShare </em></strong></span></p><p><span style="font-size: 18px">- FileShare enumerators có các thành viên sau:</span></p><ul> <li data-xf-list-type="ul"><span style="font-size: 18px">Inheritable: Nó cho phép một File truyền tính kế thừa tới các tiến trình con<br /> </span></li> <li data-xf-list-type="ul"><span style="font-size: 18px">None: Nó từ chối việc chia sẻ File hiện tại<br /> </span></li> <li data-xf-list-type="ul"><span style="font-size: 18px">Read: Nó cho phép mở File để đọc<br /> </span></li> <li data-xf-list-type="ul"><span style="font-size: 18px">ReadWrite: Nó cho phép mở File để đọc và ghi<br /> </span></li> <li data-xf-list-type="ul"><span style="font-size: 18px">Write: Nó cho phép mở File để ghi</span></li> </ul><p><span style="font-size: 18px"><strong>Ví dụ</strong></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Dưới đây là ví dụ minh họa cách sử dụng của lớp FileStream trong C#:</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">using System;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">using System.IO;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">namespace VietJackCsharp</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">{</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> class TestCsharp</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> static void Main(string[] args)</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("Vi du minh hoa File I/O trong C#");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.WriteLine("---------------------------------");</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> FileStream F = new FileStream("binary.dat", FileMode.OpenOrCreate, FileAccess.ReadWrite);</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> for (int i = 1; i <= 20; i++)</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> F.WriteByte((byte)i);</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> F.Position = 0;</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> for (int i = 0; i <= 20; i++)</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> {</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.Write(F.ReadByte() + " ");</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> F.Close();</span></em></span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> Console.ReadKey();</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3"> }</span></em></span></p><p><span style="font-size: 18px"><em><span style="color: #5900b3">}</span></em></span></p><p><span style="font-size: 18px">Nếu bạn không sử dụng lệnh Console.ReadKey(); thì chương trình sẽ chạy và kết thúc luôn (nhanh quá đến nỗi bạn không kịp nhìn kết quả). Lệnh này cho phép chúng ta nhìn kết quả một cách rõ ràng hơn.</span></p><p><span style="font-size: 18px"></span></p><p><span style="font-size: 18px">Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau:</span></p><p><span style="font-size: 18px"></span></p><p style="text-align: center"><span style="font-size: 18px"><img src="https://vietjack.com/csharp/images/file-io.PNG" alt="" class="fr-fic fr-dii fr-draggable " data-size="" style="" /></span></p> <p style="text-align: center"><span style="font-size: 18px"></span></p> <p style="text-align: right"><span style="font-size: 18px">Nguồn: vietjack.com</span></p></blockquote><p></p>
[QUOTE="uocmo_kchodoi, post: 177570, member: 165510"] [URL='https://vnkienthuc.com/forums/hoi-dap-tin-hoc.378/'][SIZE=5]Một tập tin [/SIZE][/URL][SIZE=5]là một tập hợp các dữ liệu được lưu trữ trong một đĩa với một tên cụ thể và một đường dẫn thư mục. Khi một tập tin được mở để đọc hoặc viết, nó sẽ trở thành một luồng tin. Các luồng tin cơ bản là trình tự của byte đi qua [URL='https://vnkienthuc.com/forums/code.377/']con đường truyền thông[/URL]. Có hai luồng chính: luồng đầu vào và luồng đầu ra. Các luồng đầu vào được sử dụng để đọc dữ liệu từ tập tin (đọc hoạt động) và các luồng sản lượng được sử dụng cho việc ghi lên tập tin (ghi hoạt động). [COLOR=#0000ff][B]1. Lớp I/O trong C#[/B][/COLOR] System.IO namespace có nhiều lớp đa dạng mà được sử dụng để thực hiện các hoạt động khác nhau với File, như tạo và xóa file, đọc và ghi một File, đóng một File, … Bảng sau hiển thị một số lớp non-abstract được sử dụng phổ biến tron[URL='https://vnkienthuc.com/threads/tim-hieu-ve-indexer-trong-c.77355/']g System.IO namespace trong C#:[/URL] [B]I/O Class:[/B] [COLOR=#ff8000]BinaryReader: [/COLOR] Đọc dữ liệu gốc (primitive data) từ một binary stream [COLOR=#ff8000]BinaryWriter: [/COLOR] Ghi dữ liệu gốc trong định dạng nhị phân [COLOR=#ff8000]BufferedStream:[/COLOR] Một nơi lưu giữ tạm thời cho một stream [COLOR=#ff8000]Directory:[/COLOR] Giúp ích trong việc thao tác một cấu trúc thư mục [COLOR=#ff8000]DirectoryInfo:[/COLOR] Được sử dụng để thực hiện các hoạt động trên các thư mục [COLOR=#ff8000]DriveInfo: [/COLOR]Cung cấp thông tin cho các Drive [COLOR=#ff8000]File[/COLOR]: Giúp ích trong việc thao tác các File [COLOR=#ff8000]FileInfo:[/COLOR] Được sử dụng để thực hiện các hoạt động trên các File [COLOR=#ff8000]FileStream:[/COLOR] Được sử dụng để đọc và ghi bất kỳ vị trí nào trong một File [COLOR=#ff8000]MemoryStream:[/COLOR] Được sử dụng để truy cập ngẫu nhiên tới stream được lưu giữ trong bộ nhớ [COLOR=#ff8000]Path[/COLOR]: Thực hiện các hoạt động trên thông tin path [COLOR=#ff8000]StreamReader[/COLOR]: Được sử dụng để đọc các ký tự từ một stream [COLOR=#ff8000]StreamWriter[/COLOR]: Được sử dụng để ghi các ký tự tới một stream [COLOR=#ff8000]StringReader[/COLOR]: Được sử dụng để đọc từ một string buffer [COLOR=#ff8000]StringWriter:[/COLOR] Được sử dụng để ghi vào một string buffer [COLOR=#0000ff][B]2. Lớp FileStream trong C#[/B][/COLOR] [URL='https://vnkienthuc.com/threads/tim-hieu-ve-delegate-trong-c.77356/']Lớp FileStream [/URL]trong System.IO namespace trong C# giúp đỡ trong việc đọc từ, ghi và đóng các File. Lớp này kế thừa từ lớp abstract là Stream. Bạn cần tạo một đối tượng FileStream để tạo một File mới hoặc mở một File đang tồn tại. Cú pháp để tạo một đối tượng FileStream trong C# như sau: [I][COLOR=#5900b3]FileStream <tên_đối_tượng> = new FileStream( <tên_file>, <FileMode>, <FileAccess>, <FileShare>);[/COLOR][/I] Ví dụ: chúng ta tạo một đối tượng FileStream là F để đọc một File với tên sample.txt, như sau: [I][COLOR=#5900b3]FileStream F = new FileStream("sample.txt", FileMode.Open, FileAccess.Read, FileShare.Read);[/COLOR][/I] [B]Tham số:[/B] [B][I]* FileMode [/I][/B] - FileMode enumerator định nghĩa các phương thức đa dạng để mở các File. - Các thành viên của FileMode enumerator là:[/SIZE] [LIST] [*][SIZE=5]Append: Nó mở một File đang tồn tại và đặt con trỏ tại phần cuối của File, hoặc tạo File, nếu File đó chưa tồn tại [/SIZE] [*][SIZE=5]Create: Nó tạo một File mới [/SIZE] [*][SIZE=5]CreateNew: Nó xác định tới Hệ điều hành rằng nó nên tạo một File mới [/SIZE] [*][SIZE=5]Open: Nó mở một File đang tồn tại [/SIZE] [*][SIZE=5]OpenOrCreate: Nó xác định tới Hệ điều hành rằng nó nên mở một File nếu nó tồn tại, nếu không thì nó nên tạo một File mới [/SIZE] [*][SIZE=5]Truncate: Nó mở một File đang tồn tại và truncate (cắt) kích cỡ của nó về 0 byte[/SIZE] [/LIST] [SIZE=5][B][I]* FileAccess [/I][/B] - FileAccess enumerators có các thành viên là: Read, ReadWrite và Write. [B][I]* FileShare [/I][/B] - FileShare enumerators có các thành viên sau:[/SIZE] [LIST] [*][SIZE=5]Inheritable: Nó cho phép một File truyền tính kế thừa tới các tiến trình con [/SIZE] [*][SIZE=5]None: Nó từ chối việc chia sẻ File hiện tại [/SIZE] [*][SIZE=5]Read: Nó cho phép mở File để đọc [/SIZE] [*][SIZE=5]ReadWrite: Nó cho phép mở File để đọc và ghi [/SIZE] [*][SIZE=5]Write: Nó cho phép mở File để ghi[/SIZE] [/LIST] [SIZE=5][B]Ví dụ[/B] Dưới đây là ví dụ minh họa cách sử dụng của lớp FileStream trong C#: [I][COLOR=#5900b3]using System;[/COLOR][/I] [I][COLOR=#5900b3]using System.IO;[/COLOR][/I] [I][COLOR=#5900b3]namespace VietJackCsharp[/COLOR][/I] [I][COLOR=#5900b3]{[/COLOR][/I] [I][COLOR=#5900b3] class TestCsharp[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] static void Main(string[] args)[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("Vi du minh hoa File I/O trong C#");[/COLOR][/I] [I][COLOR=#5900b3] Console.WriteLine("---------------------------------");[/COLOR][/I] [I][COLOR=#5900b3] FileStream F = new FileStream("binary.dat", FileMode.OpenOrCreate, FileAccess.ReadWrite);[/COLOR][/I] [I][COLOR=#5900b3] for (int i = 1; i <= 20; i++)[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] F.WriteByte((byte)i);[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] F.Position = 0;[/COLOR][/I] [I][COLOR=#5900b3] for (int i = 0; i <= 20; i++)[/COLOR][/I] [I][COLOR=#5900b3] {[/COLOR][/I] [I][COLOR=#5900b3] Console.Write(F.ReadByte() + " ");[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] F.Close();[/COLOR][/I] [I][COLOR=#5900b3] Console.ReadKey();[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3] }[/COLOR][/I] [I][COLOR=#5900b3]}[/COLOR][/I] Nếu bạn không sử dụng lệnh Console.ReadKey(); thì chương trình sẽ chạy và kết thúc luôn (nhanh quá đến nỗi bạn không kịp nhìn kết quả). Lệnh này cho phép chúng ta nhìn kết quả một cách rõ ràng hơn. Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau: [/SIZE] [CENTER][SIZE=5][IMG]https://vietjack.com/csharp/images/file-io.PNG[/IMG] [/SIZE][/CENTER] [RIGHT][SIZE=5]Nguồn: vietjack.com[/SIZE][/RIGHT] [/QUOTE]
Tên
Mã xác nhận
Gửi trả lời
CÔNG NGHỆ
Công Nghệ Thông Tin
Code
Sử dụng File I/O trong C# như thế nào?
Top