- อ่านและเขียนไฟล์
- การเขียนไฟล์แบบสร้างไฟล์ใหม่
- การเขียนไฟล์แบบเขียนต่อไฟล์เดิม
- การเขียนไฟล์เป็น Windows-874
- การตรวจสอบขนาดไฟล์ (file size)
1.อ่านและเขียนไฟล์
อ่านข้อความจากไฟล์ test.txt และนำมาบันทึกลงไฟล์ out.txt
using System;
using System.Collections;
using System.IO;
using System.Text;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string ipFilename = @"test.txt";
string line;
ArrayList arr = new ArrayList();
using (StreamReader ipFile = new StreamReader(ipFilename, Encoding.Default, true))
{
while ((line = ipFile.ReadLine()) != null)
{
arr.Add(line);
Console.WriteLine(line);
}
}
string opFilename = @"out.txt";
using (StreamWriter w = File.AppendText(opFilename))
{
for (int i = 0; i < arr.Count; i++)
{
w.WriteLine("[{0}] {1}", i, arr[i]);
}
}
}
}
}
2.การเขียนไฟล์แบบสร้างไฟล์ใหม่
using System.IO;
using System.Text;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string opFilename = @"out.txt";
using (TextWriter tw = new StreamWriter(opFilename))
{
tw.WriteLine("Hello World! สวัสดีชาวโลก");
}
}
}
}
3.การเขียนไฟล์แบบเขียนต่อไฟล์เดิม
using System.IO;
using System.Text;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string opFilename = @"out.txt";
using (TextWriter tw = new StreamWriter(opFilename
, true))
{
tw.WriteLine("Hello World! สวัสดีชาวโลก");
}
}
}
}
4.การเขียนไฟล์เป็น Windows-874
ปกติจะเขียนไฟล์เป็น utf-8 ถ้าต้องการให้เขียนเป็น Windows-874 ให้กำหนด Encoding
using System.IO;
using System.Text;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string opFilename = @"out.txt";
using (TextWriter tw = new StreamWriter(opFilename, true
, Encoding.GetEncoding(874)))
{
tw.WriteLine("Hello World! สวัสดีชาวโลก");
}
}
}
}
อ่านไฟล์ UTF8 แล้วเขียนเป็น Windows-874
using System;
using System.Collections;
using System.IO;
using System.Text;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
try
{
string ipFilename = @"inUTF8.csv";
string line;
ArrayList arr = new ArrayList();
using (StreamReader ipFile = new StreamReader(ipFilename
, Encoding.UTF8, true))
{
while ((line = ipFile.ReadLine()) != null)
{
arr.Add(line);
Console.WriteLine(line);
}
}
string opFilename = @"out874.csv";
using (TextWriter tw = new StreamWriter(opFilename, false
, Encoding.GetEncoding(874)))
{
for (int i = 0; i < arr.Count; i++)
{
tw.WriteLine(arr[i]);
}
}
}
catch (ArgumentException ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
อ่านไฟล์ Windows-874 แล้วเขียนเป็น UTF8
ไฟล์ output จะได้เป็น UTF-8 with BOM
using System;
using System.Collections;
using System.IO;
using System.Text;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
try
{
string ipFilename = @"in874.csv";
string line;
ArrayList arr = new ArrayList();
using (StreamReader ipFile = new StreamReader(ipFilename
, Encoding.GetEncoding(874), true))
{
while ((line = ipFile.ReadLine()) != null)
{
arr.Add(line);
Console.WriteLine(line);
}
}
string opFilename = @"outUTF8.csv";
using (TextWriter tw = new StreamWriter(opFilename
, false , Encoding.UTF8))
{
for (int i = 0; i < arr.Count; i++)
{
tw.WriteLine(arr[i]);
}
}
}
catch (ArgumentException ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
5.การตรวจสอบขนาดไฟล์ (file size)
รันแล้วจะได้ขนาดไฟล์เป็น Byte
string path = "logs/20200310.log"; long length = new System.IO.FileInfo(path).Length; log.Info(length); // 27463 // 7431