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();
}
}
Do'stlaringiz bilan baham: |