- Code:
//Tao file,
// nhập nội dung vào file
//dọc nội dung từ file đó, xuất ra màn hình
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace GhiDLvaofile
{
class Program
{
static void Main(string[] args)
{
string tenfile;
Console.WriteLine("Moi ban nhap duong dan va ten file: ");
tenfile = Console.ReadLine();
//tao moi mot file sau do dong lai
FileStream taofile = new FileStream(tenfile,FileMode.CreateNew);
taofile.Close();
Console.WriteLine("Bat dau nhap nop dung, dong ket thuc bang stop ");
//tao stremwriter de nhap du lieu vao, dung vong for de
//nhap cho den khi nguoi nhap nhap vao stop
StreamWriter nhapnoidung = new StreamWriter(tenfile);
for (int i = 0; i >= 0; i++)
{
string noidung;
noidung = Console.ReadLine();
//nguoi nhap nhap stop thi ngung nhap
if (noidung == "stop")
break;
nhapnoidung.WriteLine(noidung);
}
//doc va xuat tung dong, den khi het (gap null) thi dung lai
string docline;
nhapnoidung.Close();
Console.WriteLine("Doc file da tao ");
StreamReader docnoidung = new StreamReader(tenfile);
do
{
docline = docnoidung.ReadLine();
Console.WriteLine(docline);
} while (docline != null);
Console.ReadLine();
}
}
}
Bài tập C# - Xu ly file - Tao file, nhập nội dung vào file sau đó đọc nội dung từ file đó, xuất ra màn hình
Giới tính :
Đến từ : TPHCM
Ngày Tham gia : 03/04/2011
Tổng số bài gửi : 2292
» Nhap xau-in xau dao,so lan xuat hien cua ki tu-ki tu xuat hien nhieu lan nhat,chuyen ki tu thanh hoa-thuong,kiem tra xau Palidrom
» Nhập xuất trên Windows Forms c++
» Nhập xuất tiếng Việt trong Pascal với thư viện TCVN
» Trích xuất văn bản từ hình ảnh ?
» Tự động đăng nhập vào Windows 7, bỏ qua màn hình Welcome
» Nhập xuất trên Windows Forms c++
» Nhập xuất tiếng Việt trong Pascal với thư viện TCVN
» Trích xuất văn bản từ hình ảnh ?
» Tự động đăng nhập vào Windows 7, bỏ qua màn hình Welcome
|
|