本文整理汇总了C#中StreamReader类的典型用法代码示例。如果您正苦于以下问题:C# StreamReader类的具体用法?C# StreamReader怎么用?C# StreamReader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
StreamReader类属于命名空间,在下文中一共展示了StreamReader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConcatTwoFiles
static void ConcatTwoFiles(string firstFileName, string secondFileName)
{
/*Write a program that compares two text files line by line and prints the number of lines that are
the same and the number of lines that are different. Assume the files have equal number of lines.*/
int sameLines = 0;
int differentLines = 0;
using (StreamReader readFirstFile = new StreamReader(firstFileName))
{
using (StreamReader readSecondFile = new StreamReader(secondFileName))
{
string lineFirstFile = readFirstFile.ReadLine();
string lineSecondFile = readSecondFile.ReadLine();
while (lineFirstFile != null && lineSecondFile != null)
{
if (lineFirstFile == lineSecondFile)
{
sameLines++;
}
else
{
differentLines++;
}
lineFirstFile = readFirstFile.ReadLine();
lineSecondFile = readSecondFile.ReadLine();
}
}
}
Console.WriteLine("The number of lines that are the same: {0}", sameLines);
Console.WriteLine("The number of lines that are different: {0}", differentLines);
}
示例2: Main
static void Main()
{
var readerOne = new StreamReader("test.txt", Encoding.GetEncoding("Windows-1251"));
var readerTwo = new StreamReader("test2.txt", Encoding.GetEncoding("Windows-1251"));
using (readerOne)
{
int sameCount = 0;
int differentCount = 0;
string lineOne = readerOne.ReadLine();
using (readerTwo)
{
string lineTwo = readerTwo.ReadLine();
while (lineOne != null)
{
if (lineOne == lineTwo)
{
sameCount++;
}
else
{
differentCount++;
}
lineOne = readerOne.ReadLine();
lineTwo = readerTwo.ReadLine();
}
Console.WriteLine("The same lines are {0} and the different lines are {1}",sameCount,differentCount);
}
}
}
示例3: ReplaceTarget
public static void ReplaceTarget(List<string> words)
{
using (StreamReader reader = new StreamReader("test.txt"))
{
using (StreamWriter writer = new StreamWriter("temp.txt"))
{
StringBuilder sb = new StringBuilder();
sb.Append(@"\b(");
foreach (string word in words) sb.Append(word + "|");
sb.Remove(sb.Length - 1, 1);
sb.Append(@")\b");
string pattern = @sb.ToString();
Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
for (string line; (line = reader.ReadLine()) != null; )
{
string newLine = rgx.Replace(line, "");
writer.WriteLine(newLine);
}
}
}
File.Delete("test.txt");
File.Move("temp.txt", "test.txt");
}
示例4: Main
static void Main()
{
try
{
StreamReader reader = new StreamReader(@"D:\newfile.txt");
List<string> newText = new List<string>();
using (reader)
{
string line = reader.ReadLine();
int lineNumber = 0;
while (line != null)
{
lineNumber++;
newText.Add(lineNumber.ToString() + ". " + line);
line = reader.ReadLine();
}
}
StreamWriter writer = new StreamWriter(File.Open(@"D:\ResultIs.txt",FileMode.Create));
using (writer)
{
for (int i = 0; i < newText.Count; i++)
{
writer.WriteLine(newText[i]);
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
示例5: Main
static void Main()
{
StreamReader reader = new StreamReader(@"..\..\input.txt");
List<string> info = new List<string>();
using (reader)
{
string line = reader.ReadLine();
while (line != null)
{
int i = 0;
while (i < line.Length - 1)
{
if ((line.IndexOf('>', i) != -1)
&& (line.IndexOf('>', i) != line.Length - 1)
&& (line.IndexOf('<', line.IndexOf('>', i)) != line.IndexOf('>', i) + 1))
{
info.Add(line.Substring(line.IndexOf('>', i) + 1,
line.IndexOf('<', line.IndexOf('>', i)) - 1 - line.IndexOf('>', i)));
i = line.IndexOf('<', line.IndexOf('>', i)) + 1;
}
else
{
i++;
}
}
line = reader.ReadLine();
}
}
Console.WriteLine(string.Join(",", info));
}
示例6: CountWords
//Write a program that reads a list of words from a file words.txt and finds how many times
//each of the words is contained in another file test.txt. The result should be written in
//the file result.txt and the words should be sorted by the number of their occurrences in
//descending order. Handle all possible exceptions in your methods.
private static Dictionary<string, int> CountWords(string inputPath, string listOfWordsPath)
{
List<string> list = ExtractList(listOfWordsPath);
StreamReader reader = new StreamReader(inputPath);
Dictionary<string, int> result = new Dictionary<string, int>();
using (reader)
{
while (reader.Peek() != -1)
{
string currline = reader.ReadLine();
for (int i = 0; i < list.Count; i++)
{
Regex word = new Regex("\\b" + list[i] + "\\b");
foreach (Match match in word.Matches(currline))
{
//for each word met, if already met - increase counter, else - start counting it
if (result.ContainsKey(list[i]))
{
result[list[i]]++;
}
else
{
result.Add(list[i], 1);
}
}
}
}
}
return result;
}
示例7: Main
static void Main()
{
using (StreamReader input = new StreamReader("../../1.txt"))
using (StreamWriter output = new StreamWriter("../../2.txt"))
for (string line; (line = input.ReadLine()) != null; )
output.WriteLine(line.Replace("start", "finish"));
}
示例8: Main
static void Main()
{
StreamReader reader = new StreamReader(@"..\..\input.txt");
List<string> allEvenLines = new List<string>();
string currLine = null;
while (1 == 1)
{
currLine = reader.ReadLine();//Line1 (1/3/5/7)
currLine = reader.ReadLine();//Line2 (4/6/8/10)
if (currLine == null)
{
break;
}
allEvenLines.Add(currLine);
}
reader.Close();
StreamWriter writer = new StreamWriter(@"..\..\input.txt", false); // after closing the reader
foreach (string line in allEvenLines)
{
writer.WriteLine(line);
}
writer.Close();
}
示例9: Main
static void Main()
{
StreamReader reader = new StreamReader(@"..\..\practice.txt");
StringBuilder builder = new StringBuilder();
using (reader)
{
string line = reader.ReadLine();
int lineIndex = 0;
while (line != null)
{
lineIndex++;
builder.AppendLine(string.Format("{0}. {1}", lineIndex, line));
line = reader.ReadLine();
}
}
StreamWriter writer = new StreamWriter(@"..\..\result.txt");
using (writer)
{
writer.Write(builder.ToString());
}
}
示例10: Main
static void Main()
{
StreamReader reader1 = new StreamReader("../../file1.txt");
StreamReader reader2 = new StreamReader("../../file2.txt");
string line1 = reader1.ReadLine();
string line2 = reader2.ReadLine();
int sameCount = 0;
int totalCount = 0;
while (line1 != null) // * Assume the files have equal number of lines.
{
if (line1 == line2)
{
sameCount++;
}
Console.WriteLine("{0} ?= {1} -> {2}", line1, line2, line1 == line2);
line1 = reader1.ReadLine();
line2 = reader2.ReadLine();
totalCount++;
}
reader1.Close();
reader2.Close();
Console.WriteLine(new string('=', 25));
Console.WriteLine("Identic lines: {0}", sameCount);
Console.WriteLine("Different lines: {0}", totalCount - sameCount);
}
示例11: Main
static void Main()
{
var reader = new StreamReader("..\\..\\input.txt");
string[] input = reader.ReadToEnd().Split(' ');
reader.Close();
string deletedWord = "start";
string newWord = "finish";
for (int i = 0; i < input.Length; i++)
{
if (input[i]==deletedWord)
{
input[i] = newWord;
}
}
string result = string.Join(" ", input);
var writer = new StreamWriter("..\\..\\result.txt");
using (writer)
{
if (result != null)
{
writer.WriteLine(result);
}
}
}
示例12: SameWordsSequences
public static Dictionary<string, int> SameWordsSequences(List<string> words)
{
using (StreamReader InputFileTest = new StreamReader("..//..//InputFileTest.txt"))
{
Dictionary<string, int> dictionary = new Dictionary<string, int>();
string line = InputFileTest.ReadLine();
while (line != null)
{
string[] wordsOnLine = line.ToLower().Split(' ', '.', ';', ':');
foreach (string word in wordsOnLine)
{
if (words.Contains(word))
{
if (!dictionary.ContainsKey(word))
{
dictionary.Add(word, 1);
}
else ++dictionary[word];
}
}
line = InputFileTest.ReadLine();
}
return dictionary;
}
}
示例13: LoadContain
public string LoadContain()
{
if (Request.QueryString["CourseId"] == null)
{
return string.Empty;
}
string ThePath = string.Empty;
string RetData = string.Empty;
using (OleDbConnection Con = new OleDbConnection(constr))
{
OleDbCommand cmd = new OleDbCommand(String.Format("SELECT TOP 1 DataPath FROM CoursenotimeDataPath WHERE CourseId = {0}", Request.QueryString["CourseId"]), Con);
try
{
Con.Open();
ThePath = cmd.ExecuteScalar().ToString();
//if (ThePath != string.Empty)
// ThePath = MapPath(DB.CourseNoTimeFileDir + ThePath);
ThePath = DB.CourseNoTimeFileDir + ThePath;
TextReader TR = new StreamReader(ThePath);
RetData = TR.ReadToEnd();
TR.Close();
TR.Dispose();
}
catch (Exception ex)
{
RetData = ex.Message;
}
Con.Close();
}
return HttpUtility.HtmlDecode(RetData);
}
示例14: Deserialize
public object Deserialize(
StreamReader streamReader,
SerializationContext serializationContext,
PropertyMetaData propertyMetaData = null)
{
return streamReader.ReadInt16();
}
示例15: Main
static void Main()
{
string textOne = "";
string textTwo = "";
try
{
StreamReader reader = new StreamReader(@"../../FirstTextFile.txt");
using (reader)
{
textOne = reader.ReadToEnd();
}
reader = new StreamReader(@"../../SecondTextFile.txt");
using (reader)
{
textTwo = reader.ReadToEnd();
}
ConcatenateTwoStrings(textOne, textTwo);
}
catch
{
Console.WriteLine("Something went wrong! Check file paths or file contents.");
}
}