SlideShare une entreprise Scribd logo
1  sur  11
BinaryWriter and BinaryReader
Jason
BinaryWriter
 Writes primitive types in binary to a stream and supports writing strings in a
specific encoding.
 BinaryWriter()
 BinaryWriter(Stream)
 BinaryWriter(Stream, Encoding)
 BinaryWriter(Stream, Encoding, Boolean)
BinaryWriter
 Methods
Write(Boolean) Write(Byte) Write(Byte[]) Write(Char)
Write(Char[]) Write(Decimal) Write(Double) Write(Int16)
Write(Int32) Write(Int64) Write(SByte) Write(Single)
Write(String) Write(UInt16) Write(UInt32) Write(UInt64)
Write(Byte[], Int32, Int32) Write(Char[], Int32, Int32)
BinaryReader
 Reads primitive data types as binary values in a specific encoding.
 BinaryReader(Stream)
 BinaryReader(Stream, Encoding)
 BinaryReader(Stream, Encoding, Boolean)
BinaryReader
 Methods
Read() ReadBoolean ReadByte ReadBytes
ReadChar ReadChars ReadDecimal ReadDouble
ReadInt16 ReadInt32 ReadInt64 ReadSByte
ReadSingle ReadString ReadUInt16 ReadUInt32
Read(Char[], Int32, Int32) Read(Byte[], Int32, Int32)
BinaryReader
 example
using (var writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
{
writer.Write(10);
}
using (var reader = new BinaryReader(File.Open(fileName, FileMode.Open)))
{
reader.ReadInt32();
reader.ReadBytes();
}
reader.ReadInt32()
Answer: 10
reader.ReadBytes()
Answer:
BinaryReader
 example
using (var writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
{
writer.Write(“Jason”);
}
using (var reader = new BinaryReader(File.Open(fileName, FileMode.Open)))
{
reader.ReadString();
reader.ReadBytes();
}
reader.ReadString()
Answer: Jason
reader.ReadBytes()
Answer:
BinaryWriter and BinaryReader
 example
using (var writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
{
writer.Write(1.250F);
writer.Write(@"c:Temp");
writer.Write(10);
writer.Write(true);
}
using (var reader = new BinaryReader(File.Open(fileName, FileMode.Open)))
{
var aspectRatio = reader.ReadSingle();
var tempDirectory = reader.ReadString();
var autoSaveTime = reader.ReadInt32();
var showStatusBar = reader.ReadBoolean();
}
BinaryReader
 Remarks
 BinaryReader does not restore the file position after an unsuccessful read.
BinaryReader VS StreamReader
 If the entirety of your data is a straightforward binary encoding of text data,
use StreamReader.
 If you've fundamentally got binary data which may happen to have some
portions in text, use BinaryReader
Reference
 File and Stream I/O
 http://msdn.microsoft.com/en-us/library/k3352a4t(v=vs.110).aspx
 BinaryReader
 http://msdn.microsoft.com/en-us/library/system.io.binaryreader(v=vs.110).aspx
 BinaryWriter
 http://msdn.microsoft.com/en-us/library/gg712841(v=vs.110).aspx

Contenu connexe

Plus de LearningTech (20)

vim
vimvim
vim
 
PostCss
PostCssPostCss
PostCss
 
ReactJs
ReactJsReactJs
ReactJs
 
Docker
DockerDocker
Docker
 
Semantic ui
Semantic uiSemantic ui
Semantic ui
 
node.js errors
node.js errorsnode.js errors
node.js errors
 
Process control nodejs
Process control nodejsProcess control nodejs
Process control nodejs
 
Expression tree
Expression treeExpression tree
Expression tree
 
SQL 效能調校
SQL 效能調校SQL 效能調校
SQL 效能調校
 
flexbox report
flexbox reportflexbox report
flexbox report
 
Vic weekly learning_20160504
Vic weekly learning_20160504Vic weekly learning_20160504
Vic weekly learning_20160504
 
Reflection & activator
Reflection & activatorReflection & activator
Reflection & activator
 
Peggy markdown
Peggy markdownPeggy markdown
Peggy markdown
 
Node child process
Node child processNode child process
Node child process
 
20160415ken.lee
20160415ken.lee20160415ken.lee
20160415ken.lee
 
Peggy elasticsearch應用
Peggy elasticsearch應用Peggy elasticsearch應用
Peggy elasticsearch應用
 
Expression tree
Expression treeExpression tree
Expression tree
 
Vic weekly learning_20160325
Vic weekly learning_20160325Vic weekly learning_20160325
Vic weekly learning_20160325
 
D3js learning tips
D3js learning tipsD3js learning tips
D3js learning tips
 
git command
git commandgit command
git command
 

Dernier

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Dernier (20)

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 

Binary

  • 2. BinaryWriter  Writes primitive types in binary to a stream and supports writing strings in a specific encoding.  BinaryWriter()  BinaryWriter(Stream)  BinaryWriter(Stream, Encoding)  BinaryWriter(Stream, Encoding, Boolean)
  • 3. BinaryWriter  Methods Write(Boolean) Write(Byte) Write(Byte[]) Write(Char) Write(Char[]) Write(Decimal) Write(Double) Write(Int16) Write(Int32) Write(Int64) Write(SByte) Write(Single) Write(String) Write(UInt16) Write(UInt32) Write(UInt64) Write(Byte[], Int32, Int32) Write(Char[], Int32, Int32)
  • 4. BinaryReader  Reads primitive data types as binary values in a specific encoding.  BinaryReader(Stream)  BinaryReader(Stream, Encoding)  BinaryReader(Stream, Encoding, Boolean)
  • 5. BinaryReader  Methods Read() ReadBoolean ReadByte ReadBytes ReadChar ReadChars ReadDecimal ReadDouble ReadInt16 ReadInt32 ReadInt64 ReadSByte ReadSingle ReadString ReadUInt16 ReadUInt32 Read(Char[], Int32, Int32) Read(Byte[], Int32, Int32)
  • 6. BinaryReader  example using (var writer = new BinaryWriter(File.Open(fileName, FileMode.Create))) { writer.Write(10); } using (var reader = new BinaryReader(File.Open(fileName, FileMode.Open))) { reader.ReadInt32(); reader.ReadBytes(); } reader.ReadInt32() Answer: 10 reader.ReadBytes() Answer:
  • 7. BinaryReader  example using (var writer = new BinaryWriter(File.Open(fileName, FileMode.Create))) { writer.Write(“Jason”); } using (var reader = new BinaryReader(File.Open(fileName, FileMode.Open))) { reader.ReadString(); reader.ReadBytes(); } reader.ReadString() Answer: Jason reader.ReadBytes() Answer:
  • 8. BinaryWriter and BinaryReader  example using (var writer = new BinaryWriter(File.Open(fileName, FileMode.Create))) { writer.Write(1.250F); writer.Write(@"c:Temp"); writer.Write(10); writer.Write(true); } using (var reader = new BinaryReader(File.Open(fileName, FileMode.Open))) { var aspectRatio = reader.ReadSingle(); var tempDirectory = reader.ReadString(); var autoSaveTime = reader.ReadInt32(); var showStatusBar = reader.ReadBoolean(); }
  • 9. BinaryReader  Remarks  BinaryReader does not restore the file position after an unsuccessful read.
  • 10. BinaryReader VS StreamReader  If the entirety of your data is a straightforward binary encoding of text data, use StreamReader.  If you've fundamentally got binary data which may happen to have some portions in text, use BinaryReader
  • 11. Reference  File and Stream I/O  http://msdn.microsoft.com/en-us/library/k3352a4t(v=vs.110).aspx  BinaryReader  http://msdn.microsoft.com/en-us/library/system.io.binaryreader(v=vs.110).aspx  BinaryWriter  http://msdn.microsoft.com/en-us/library/gg712841(v=vs.110).aspx