[C#]
using System;
using System.IO;
using System.Text;
public class CharsToStr
{public static void Main(String[] args)
{// Create a StringBuilder object that can then be modified.
StringBuilder sb = new StringBuilder("Some number of characters");// Define and initialize a character array from which characters
// will be read into the StringBuilder object.
char[] b = {' ','t','o',' ','w','r','i','t','e',' ','t','o','.'};// Create a StringWriter and attach it to the StringBuilder object.
StringWriter sw = new StringWriter(sb);
// Write three characters from the array into the StringBuilder object.
sw.Write(b, 0, 3);
// Display the output.
Console.WriteLine(sb);
// Close the StringWriter.
sw.Close();
}
}
Some number of characters to
[C#]
using System;
using System.IO;
public class CompBuf {private const string FILE_NAME = "MyFile.txt";
public static void Main(String[] args) {if (!File.Exists(FILE_NAME)) { Console.WriteLine("{0} does not exist!", FILE_NAME);return;
}
FileStream fsIn = new FileStream(FILE_NAME, FileMode.Open,
FileAccess.Read, FileShare.Read);
// Create a reader that can read characters from the FileStream.
StreamReader sr = new StreamReader(fsIn);
// While not at the end of the file, read lines from the file.
while (sr.Peek()>-1) {String input = sr.ReadLine();
Console.WriteLine (input);
}
sr.Close();
}
}
[C#]
using System;
using System.IO;
public class ReadBuf {private const string FILE_NAME = "MyFile.txt";
public static void Main(String[] args) {if (!File.Exists(FILE_NAME)) { Console.WriteLine("{0} does not exist!", FILE_NAME);return;
}
FileStream f = new FileStream(FILE_NAME, FileMode.Open,
FileAccess.Read, FileShare.Read);
// Create a reader that can read bytes from the FileStream.
BinaryReader sr = new BinaryReader(f);
// While not at the end of the file, read lines from the file.
while (sr.PeekChar()>-1) {byte input = sr.ReadByte();
Console.WriteLine (input);
}
sr.Close();
}
}
[C#]
using System;
using System.IO;
public class MyWriter {private Stream s;
public MyWriter(Stream stream) {s = stream;
}
public void WriteDouble(double myData) {byte[] b = BitConverter.GetBytes(myData);
// GetBytes is a binary representation of a double data type.
s.Write(b, 0, b.Length);
}
public void Close() {s.Close();
}
}
……