Harry Potter and the Sorcerer's Stone


JsonReader va JsonWriter yordamida JSON fayliga ma'lumotlarni o'qish va yozish uchun C# dasturini amalga oshiring


Download 192.49 Kb.
bet19/62
Sana17.06.2023
Hajmi192.49 Kb.
#1535967
1   ...   15   16   17   18   19   20   21   22   ...   62
Bog'liq
Algaritim amaliy-2

34. JsonReader va JsonWriter yordamida JSON fayliga ma'lumotlarni o'qish va yozish uchun C# dasturini amalga oshiring.
Javob:
using (StreamReader sr = new StreamReader("inputfile.json"))
using (JsonReader reader = new JsonTextReader(sr))
using (StreamWriter sw = new StreamWriter("outputfile.json"))
using (JsonWriter writer = new JsonTextWriter(sw))
{
// Copy the JSON data from the input file to the output file
while (reader.Read())
{
writer.WriteToken(reader);
}
}
Ushbu kod misolida biz o'qish uchun "inputfile.json" faylini ochish uchun StreamReader-dan foydalanamiz. Keyin fayldan JSON qatorini oʻqish uchun JsonReader obyektidan foydalanamiz. Keyinchalik, "outputfile.json" nomli yangi fayl yaratish uchun StreamWriter dan foydalanamiz. Nihoyat, biz JsonWriter ob'ektidan JSON ma'lumotlarini JsonReader obyektidan ning WriteToken usuli yordamida chiqish fayliga yozish uchun foydalanamiz. JsonWriter.
35. C# da tasvir faylini o'qish va uni bayt massiviga aylantirish uchun BinaryReader dan foydalaning.
Javob:
using System.IO;
string imagePath = "path/to/image/file.jpg";
byte[] imageBytes;
using (BinaryReader reader = new BinaryReader(File.Open(imagePath, FileMode.Open)))
{
imageBytes = reader.ReadBytes((int)reader.BaseStream.Length);
}
36. BinaryWriter yordamida tasvir fayliga bayt massivi yozish uchun C# dasturini yozing.
Javob:
using System.IO;
byte[] imageBytes = /* array of bytes */;
string outputPath = "path/to/output/file.jpg";
using (BinaryWriter writer = new BinaryWriter(File.Open(outputPath, FileMode.Create)))
{
writer.Write(imageBytes, 0, imageBytes.Length);
}
Ushbu kod JPEG tasvirlar uchun ekanligini unutmang. Boshqa tasvir turlari uchun fayl kengaytmasi va/yoki formatini mos ravishda moslashtirishingiz kerak bo'lishi mumkin.
37. BinaryReader-dan C# da ikkilik faylni o'qish va uni C# ob'ektiga aylantirish uchun foydalaning.
Javob:
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
string binFilePath = "path/to/binary/file.bin";
YourObject obj;
using (BinaryReader reader = new BinaryReader(File.Open(binFilePath, FileMode.Open)))
{
BinaryFormatter formatter = new BinaryFormatter();
obj = (YourObject)formatter.Deserialize(reader.BaseStream);
}
Iltimos, kod parchasidagi YourObject ni ikkilik maʼlumotlaringiz uchun foydalanayotgan haqiqiy sinf/tur bilan almashtiring.

Download 192.49 Kb.

Do'stlaringiz bilan baham:
1   ...   15   16   17   18   19   20   21   22   ...   62




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