24 May 2016

Nhập từ bàn phím lưu file và đọc file trong C# sử dụng StreamWriter StreamReader

Write , Save,  Read in C# (console)
Nhập từ bàn phím lưu file đọc file trong C#
C#
using System;
using System.IO;  //Chú ý cần có thư viện IO
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication4
{
    class Student
    {
        public string Name { set; get; }
        public string Age { set; get; }

        public Student(string name, string age)
        {
            Name = name;
            Age = age;
        }

    }
    class Program
    {
        public List<Student> addList(List<Student> list)
        {

            Console.WriteLine("Nhap vao name");
            string name = Console.ReadLine();
            Console.WriteLine("Nhap vao age");
            string age = Console.ReadLine();
            list.Add(new Student(name, age));

            return list;
        }
        public void save(List<Student> list)
        {
            using (StreamWriter sw = new StreamWriter("E:/names.txt"))
            {
                foreach (Student s in list)
                {
                    sw.WriteLine("Full Name: " + s.Name);
                    sw.WriteLine("Age: " + s.Age);

                }
            }
        }

        public void readFile()
        {
            string line = "";
            using (StreamReader sr = new StreamReader("E:/names.txt"))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    Console.WriteLine(line);
                }
            }
        }


        public void display(List<Student> list)
        {
            foreach (Student student in list)
            {
                Console.WriteLine("\n\tFull Name: " + student.Name + "\tAge: " + student.Age + "\n");
            }
        }

        static void Main(string[] args)
        {
            List<Student> list = new List<Student>();
            int choose;
            do
            {
                Console.WriteLine("1 - Add-List, 2 - Save, 3 - Read-File, 4 - Display-List, 5 - Exit");

                choose = int.Parse(Console.ReadLine());
                switch (choose)
                {
                    case 1:
                        new Program().addList(list);
                        break;
                    case 2:
                        new Program().save(list);
                        break;
                    case 3:
                        new Program().readFile();
                        break;
                    case 4:
                        new Program().display(list);
                        break;
                    case 5:
                        break;
                }


            } while (choose != 5);
        }

    }
}
Input - Nhập từ bàn phím
 - C# 
public List<Student> addList(List<Student> list)
        {

            Console.WriteLine("Nhap vao name");
            string name = Console.ReadLine();
            Console.WriteLine("Nhap vao age");
            string age = Console.ReadLine();
            list.Add(new Student(name, age));

            return list;
        }
Save - Lưu File trong c#
C# 
public void save(List<Student> list)
        {
            using (StreamWriter sw = new StreamWriter("E:/names.txt"))
            {
                foreach (Student s in list)
                {
                    sw.WriteLine("Full Name: " + s.Name);
                    sw.WriteLine("Age: " + s.Age);

                }
            }
        }
Read - Đọc file trong C#
C# 
public void readFile()
        {
            string line = "";
            using (StreamReader sr = new StreamReader("E:/names.txt"))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    Console.WriteLine(line);
                }
            }
        }

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang