日期:2009-05-30 浏览次数:20486 次
[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();
}
}