C#에서 BinaryWriter는 기본 유형을 특정 인코딩 스트림의 바이너리 데이터로 작성하는 데 사용되는 클래스입니다. System.IO 네임스페이스 아래에 있습니다.
다음은 BinaryWriter와 관련된 몇 가지 중요한 사항입니다.
BinaryWriter의 객체를 생성하는 생성자는 네 가지 오버로드된 형태로 제공됩니다. 오버로드된 모든 생성자를 사용하여 BinaryWriter 객체를 생성하는 구문은 다음과 같습니다.
구문 #1
protected BinaryWriter();
BinaryWriter 클래스의 인스턴스를 초기화하는 데 사용됩니다.
구문 #2
BinaryWriter binaryWriter = new BinaryWriter(outputStream) ;
위 명령문은 지정된 스트림(outputStream)을 기반으로 UTF-8 문자 인코딩을 사용하여 BinaryWriter 클래스의 새 인스턴스를 초기화합니다.
구문 #3
BinaryWriter binaryWriter = new BinaryWriter(outputStream, encoding);
위 명령문은 지정된 스트림(outputStream) 및 문자 인코딩(encoding)을 기반으로 BinaryWriter의 새 인스턴스를 초기화합니다.
구문 #4
BinaryWriter binaryWriter = new BinaryWriter(outputStream, encoding, true);
코드:
using (BinaryWriter binaryWriter = new BinaryWriter(File.Open(fileName, FileMode.Create )) ) { //user code }
여기서 File.Open() 메서드는 BinaryWriter의 인스턴스를 생성하는 데 도움이 되는 FileStream 개체를 반환합니다.
다음 표는 다양한 데이터 유형에 대한 BinaryWriter의 일부 Write() 메서드에 대한 세부 정보를 보여줍니다.
Method | Description |
Write(Boolean) | This method is used to write the one-byte Boolean value to the present stream; 0 represents false while 1 represents true. |
Write(Byte) | This method is used to write an unsigned byte to the present stream and then it advances the position of the stream by one byte. |
Write(Char) | This method is used to write Unicode character to present stream and also it advances the present stream position according to the character encoding used and according to the characters being written to the present stream. |
Write(Decimal) | This method is used to write a decimal value to the present stream and also it advances the position of the current stream by sixteen bytes. |
Write(Double) | This method is used to write an eight-byte floating-point value to the present stream and then it also advances the position of the current stream by eight bytes. |
Write(Int32) | This method is used to write a four-byte signed integer to the present stream and then it advances the position of current stream by four bytes. |
Write(String) | This method is used to write length prefixed string to present stream in the present encoding of BinaryWriter and also it advances the current stream position according to the encoding used and according to the characters being written to the present stream. |
Example showing the creation of file:
Code:
using System; using System.IO; namespace ConsoleApp4 { public class Demo { string fileLocation = "E:\\Content\\newBinaryFile.dat"; public void WritingFile() { try { //checking if file exists if (File.Exists(fileLocation)) { File.Delete(fileLocation); } FileStream fileStream = new FileStream(fileLocation, FileMode.Create, FileAccess.Write, FileShare.ReadWrite); //creating binary file using BinaryWriter using (BinaryWriter binaryWriter = new BinaryWriter(fileStream)) { //writing data using different Write() methods //of BinaryWriter binaryWriter.Write(5253); binaryWriter.Write("This is a string value."); binaryWriter.Write('A'); } } catch (Exception exception) { Console.WriteLine(exception.Message); } } public void ReadingFile() { try { FileStream fileStream = new FileStream(fileLocation, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); using (BinaryReader binaryReader = new BinaryReader(fileStream)) { Console.WriteLine("IntegerValue = " + binaryReader.ReadInt32()); Console.WriteLine("StringValue = " + binaryReader.ReadString()); Console.WriteLine("CharValue = " + binaryReader.ReadChar()); } } catch (Exception exception) { Console.WriteLine(exception.Message); } } } public class BinaryWriterDemo { static void Main(string[] args) { Demo demoObj = new Demo(); demoObj.WritingFile(); demoObj.ReadingFile(); Console.ReadLine(); } } }
Output:
In C#, the BinaryWriter class is used to write primitive types as binary information to the stream. If the encoding is not defined, then the BinaryWriter class uses the default UTF-8 character encoding to write data to a binary file. An object of BinaryWriter can be created using the Stream object.
위 내용은 C#의 BinaryWriter의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!