SlideShare a Scribd company logo
1 of 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

More Related Content

More from 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
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
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...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
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...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
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
 

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