Harry Potter and the Sorcerer's Stone


JsonWriter yordamida JSON faylini yozish uchun C# usulini yozing


Download 192.49 Kb.
bet11/62
Sana17.06.2023
Hajmi192.49 Kb.
#1535967
1   ...   7   8   9   10   11   12   13   14   ...   62
Bog'liq
Algaritim amaliy-2

11. JsonWriter yordamida JSON faylini yozish uchun C# usulini yozing.
Javob:
using (StreamWriter sw = new StreamWriter("outputfile.json"))
{
using (JsonWriter writer = new JsonTextWriter(sw))
{
writer.Formatting = Formatting.Indented;
writer.WriteStartObject();
writer.WritePropertyName("name");
writer.WriteValue("Mohira");
writer.WritePropertyName("versionNo");
writer.WriteValue(1.0);
writer.WriteEndObject();
}
}
12. C# faylidagi matnni o'qish uchun StreamReader dasturidan foydalaning.
Javob:
string filePath = @"C:\path\to\file.txt";
using (StreamReader reader = new StreamReader(filePath))
{
string line = reader.ReadLine();
while (line != null)
{
Console.WriteLine(line);
line = reader.ReadLine();
}
}
13. BinaryReader yordamida ikkilik faylni o'qish uchun C# tilida dastur yozing.
Javob:
string binaryFilePath = @"C:\path\to\binaryfile.bin";
using (BinaryReader reader = new BinaryReader(File.Open(binaryFilePath, FileMode.Open)))
{
int length = (int)reader.BaseStream.Length;
byte[] buffer = new byte[length];
int bytesRead = reader.Read(buffer, 0, length);
foreach (byte b in buffer)
{
Console.Write(b + " ");
}
}
14. BinaryWriter yordamida faylga rasm yozish uchun C# usulini qo'llang.
Javob:
string imagePath = @"C:\path\to\image.png";
byte[] imageBytes = File.ReadAllBytes(imagePath);
using (BinaryWriter writer = new BinaryWriter(File.Open("outputfile.bin", FileMode.Create)))
{
writer.Write(imageBytes);
}
15. C# da CSV faylini o'qish uchun StreamReader dasturidan foydalaning.
Javob:
string csvFilePath = @"C:\path\to\file.csv";
using (StreamReader sr = new StreamReader(csvFilePath))
{
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
string[] values = line.Split(',');
foreach (string value in values)
{
Console.Write(value + " ");
}
Console.WriteLine();
}
}

Download 192.49 Kb.

Do'stlaringiz bilan baham:
1   ...   7   8   9   10   11   12   13   14   ...   62




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling