Harry Potter and the Sorcerer's Stone


FileStream yordamida JSON fayliga ma'lumotlarni o'qish va yozish uchun C# usulini qo'llang


Download 192.49 Kb.
bet14/62
Sana17.06.2023
Hajmi192.49 Kb.
#1535967
1   ...   10   11   12   13   14   15   16   17   ...   62
Bog'liq
Algaritim amaliy-2

23. FileStream yordamida JSON fayliga ma'lumotlarni o'qish va yozish uchun C# usulini qo'llang.
Javob:
using System.IO;
using Newtonsoft.Json;
public static void WriteToJsonFile(string filePath, T objectToWrite)
{
using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
using (StreamWriter streamWriter = new StreamWriter(fileStream))
using (JsonWriter jsonWriter = new JsonTextWriter(streamWriter))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(jsonWriter, objectToWrite);
}
}
public static T ReadFromJsonFile(string filePath)
{
using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
using (StreamReader streamReader = new StreamReader(fileStream))
using (JsonReader jsonReader = new JsonTextReader(streamReader))
{
JsonSerializer serializer = new JsonSerializer();
return serializer.Deserialize(jsonReader);
}
}
Ushbu usuldan foydalanish uchun JSON fayliga ma'lumotlarni yozish uchun WriteToJsonFile usulini va JSON faylidan ma'lumotlarni o'qish uchun ReadFromJsonFile usulini chaqirishingiz mumkin. Foydalanish misoli:
// creates an object to write to the JSON file
var myObject = new { Name = "John", Age = 30 };
// writes the object to a JSON file
WriteToJsonFile("myFile.json", myObject);
// reads the object from the JSON file
var objectFromFile = ReadFromJsonFile("myFile.json");
// do something with the object
Console.WriteLine($"Name: {objectFromFile.Name}, Age: {objectFromFile.Age}");
Ushbu misol Name va Age xususiyatiga ega myObject anonim obyektini yaratadi, uni myFile deb nomlangan JSON fayliga yozadi. json fayldan ma'lumotlarni objectFromFile-ga o'qiydi va keyin konsolga Name va Age qiymatlarini chiqaradi.< /p>
24. C# da tasvir faylini o'qish uchun BinaryReader dasturidan foydalaning.
Javob:
using System.IO;
// open the image file as a binary stream
using (FileStream stream = new FileStream("path/to/image.png", FileMode.Open, FileAccess.Read))
using (BinaryReader reader = new BinaryReader(stream))
{
// read the image file header to determine the image format
byte[] header = reader.ReadBytes(16);
string format = GetImageFormatFromHeader(header); // assume this method returns the image format (e.g. "PNG", "JPEG", etc.)

// read the image data


byte[] imageData = reader.ReadBytes((int)(stream.Length - 16)); // subtract the header length from the total length to get the image data length
}
Ushbu misolda biz rasm faylini faqat o'qish rejimida ochish uchun FileStream dan foydalanamiz va ikkilik ma'lumotlarni o'qish uchun uni BinaryReader-ga o'rab olamiz. Biz faylning dastlabki 16 baytini (sarlavhani o'z ichiga olgan) reader.ReadBytes(16) yordamida o'qiymiz, so'ngra tasvirni aniqlash uchun uni taxminiy GetImageFormatFromHeader usuliga o'tkazamiz. format.
Keyin, biz rasm ma'lumotlarini reader.ReadBytes((int)(stream.Length - 16)) yordamida o'qiymiz. Tasvir maʼlumotlari uzunligini olish uchun oqimning umumiy uzunligidan (sarlavha va tasvir maʼlumotlarini oʻz ichiga oladi) 16 ni ayiramiz.

Download 192.49 Kb.

Do'stlaringiz bilan baham:
1   ...   10   11   12   13   14   15   16   17   ...   62




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