Nhập từ bàn phím lưu vào file ghi đè dữ liệu trong C#
Save File ghi đè dữ liệu C#
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {
            FileStream fs = new FileStream("E://name.txt",FileMode.Append, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            Console.WriteLine("Enter the String");
            string str = Console.ReadLine();
            sw.WriteLine(str);
            sw.Flush();
            fs.Close();
            Console.Read();
        }
    }
}
| Close() | Cho đóng lại các writer và giải phóng mọi nguồn lực chiếm dụng | 
| Flush() | Cho xoá sạch tất cả các buffer đối với writer hiện hành | 
| NewLine | Thuộc tính này dùng làm hằng sang hằng | 
| Write() | Viết một hằng lên text stream không có newline constant | 
| WriteLine() | Viết một hằng lên text stream có newline constant | 
Đọc file dữ liệu trong C#
Đọc file C#
using System; using System.IO; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication10 { class Program { public void ReadData() { FileStream fs = new FileStream("E:\\name.txt", FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader ( fs ); sr.BaseStream.Seek( 0, SeekOrigin.Begin); string str = sr.ReadLine(); while ( str != null ) { Console.WriteLine("{0}", str ); str = sr.ReadLine(); } sr.Close(); fs.Close(); } static void Main(string[] args) { Program p = new Program(); p.ReadData(); Console.Read(); } } }
| Enumeration | Values | 
| FileMode | Append, Create, CreateNew, Open, OpenOrCreate, or Truncate Cho biết bạn muốn mở file như thế nào. | 
| FileAcess | Read, ReadWrite, or Write. Cho biết bạn muốn truy xuất file như thế nào – bạn định đọc hoặc viết file hoặc cả hai. | 
| FileShare | Inheritable, None, Read, ReadWrite, or Write. khả năng truy xuất file. | 
 
 





 
 
 
 
 
0 nhận xét:
Post a Comment