728x90
๐ 1. Binary ์ ์ถ๋ ฅ (์ด๋ฏธ์ง, ์ซ์, ์ด์ง ๋ฐ์ดํฐ ์ ์ฅ)
โ๏ธ BinaryWriter, BinaryReader๋ฅผ ํ์ฉํ ์ซ์ ์ ์ฅ
using System.IO;
// ์ซ์ ์ ์ฅ
using (BinaryWriter writer = new BinaryWriter(File.Open("number.dat", FileMode.Create)))
{
writer.Write(123); // int
writer.Write(3.14); // double
writer.Write("Hello"); // string
}
// ์ซ์ ์ฝ๊ธฐ
using (BinaryReader reader = new BinaryReader(File.Open("number.dat", FileMode.Open)))
{
int num = reader.ReadInt32();
double d = reader.ReadDouble();
string text = reader.ReadString();
Console.WriteLine($"์ฝ์ ๊ฐ: {num}, {d}, {text}");
}
โ๏ธ ์ด๋ฏธ์ง ๋ฑ ๋ฐ์ด๋๋ฆฌ ํ์ผ ๋ณต์ฌ (raw byte ์ฒ๋ฆฌ)
string source = "input.jpg";
string target = "copy.jpg";
byte[] data = File.ReadAllBytes(source);
File.WriteAllBytes(target, data);
Console.WriteLine("์ด๋ฏธ์ง ํ์ผ ๋ณต์ฌ ์๋ฃ");
๐ 2. ๋๋ ํ ๋ฆฌ ๊ด๋ฆฌ (Directory ํด๋์ค)
โ๏ธ ๋๋ ํ ๋ฆฌ ์์ฑ
string folderPath = "myFolder";
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
Console.WriteLine("ํด๋ ์์ฑ ์๋ฃ");
}
โ๏ธ ํ์ผ/๋๋ ํ ๋ฆฌ ๋ชฉ๋ก ๊ฐ์ ธ์ค๊ธฐ
string path = "."; // ํ์ฌ ํด๋
string[] files = Directory.GetFiles(path);
string[] dirs = Directory.GetDirectories(path);
Console.WriteLine("๐ ํ์ผ ๋ชฉ๋ก:");
foreach (var file in files)
Console.WriteLine(file);
Console.WriteLine("\n๐ ๋๋ ํ ๋ฆฌ ๋ชฉ๋ก:");
foreach (var dir in dirs)
Console.WriteLine(dir);
โ๏ธ ํด๋ ์ญ์ (์ฃผ์: ์ฌ๊ท ์ญ์ ํฌํจ)
Directory.Delete("myFolder", recursive: true);
Console.WriteLine("ํด๋ ์ญ์ ์๋ฃ");
๐งช 3. JSON/XML ์ ์ถ๋ ฅ
โ๏ธ JSON ์ ์ถ๋ ฅ (System.Text.Json)
using System.Text.Json;
// ์ ์ฅํ ๊ฐ์ฒด
var user = new { Name = "ํ๊ธธ๋", Age = 30 };
// JSON ๋ฌธ์์ด๋ก ์ ์ฅ
string json = JsonSerializer.Serialize(user);
File.WriteAllText("user.json", json);
// JSON ํ์ผ ์ฝ๊ธฐ
string jsonFromFile = File.ReadAllText("user.json");
var readUser = JsonSerializer.Deserialize<Dictionary<string, object>>(jsonFromFile);
Console.WriteLine($"์ด๋ฆ: {readUser["Name"]}, ๋์ด: {readUser["Age"]}");
โ๏ธ XML ์ ์ถ๋ ฅ (System.Xml)
using System.Xml;
XmlDocument doc = new XmlDocument();
// XML ๋ฌธ์์ด ์์ฑ
doc.LoadXml("<user><name>ํ๊ธธ๋</name><age>30</age></user>");
doc.Save("user.xml");
// XML ์ฝ๊ธฐ
XmlDocument readDoc = new XmlDocument();
readDoc.Load("user.xml");
string name = readDoc.SelectSingleNode("/user/name").InnerText;
string age = readDoc.SelectSingleNode("/user/age").InnerText;
Console.WriteLine($"์ด๋ฆ: {name}, ๋์ด: {age}");
โ +
1. JSON ์ง๋ ฌํ ๋์์ ๊ผญ ํด๋์ค์ฌ์ผ ํ๋์?
์๋์. ์ต๋ช ๊ฐ์ฒด๋ Dictionary<string, object>๋ ์ฌ์ฉ ๊ฐ๋ฅํ์ง๋ง, class๋ก ์ ์ํ๋ฉด ๋ ์ ํํ๋ ์ฒ๋ฆฌ์ ์ ์ง๋ณด์์ ์ ๋ฆฌํจ.
2. XML์ ํด๋์ค๋ก deserialize ํ ์ ์๋์?
๊ฐ๋ฅํจ. XmlSerializer๋ฅผ ์ฌ์ฉํ๋ฉด XML ↔ ๊ฐ์ฒด ๋งคํ์ ํ ์ ์์. ์์ ์์ฒญ ์ ๋ณ๋ ์์ฑํด์ค๊ฒ.
3. ๋์ฉ๋ ์ด๋ฏธ์ง๋ ๋ฐ์ด๋๋ฆฌ ํ์ผ์ ์ฒ๋ฆฌํ ๋ ์ฃผ์์ ์?
FileStream์ผ๋ก chunk ๋จ์๋ก ์ฝ๊ณ ์ฐ๋ ๋ฐฉ์์ด ํจ์จ์ ์. ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ์ ์ต์ํํ๊ธฐ ์ํด ์คํธ๋ฆฌ๋ฐ ์ฒ๋ฆฌ ๊ถ์ฅ.
'C# > C# TIP' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
C#์ ์ ์ถ๋ ฅ(IO) - ํ์ผ ์ ์ถ๋ ฅ(File IO) ์ค์ฌ (1) | 2025.06.23 |
---|---|
C#์ ์ ์ถ๋ ฅ(IO) - ํฐ๋ฏธ๋ ์ ์ถ๋ ฅ ์ค์ฌ(์ฝ์ ๊ธฐ๋ฐ) (0) | 2025.06.23 |
C# ์ฝ๋ ์ปจ๋ฒค์ (Code convention) (0) | 2025.05.21 |
C# ๋ณ์ (0) | 2025.05.21 |
์ฃผ์ // /**/ /// (0) | 2025.02.09 |