现有两个文件1和文件2;
内容如下:
文件1:1
12
123
1234
12345
.
.
文件2:1
12
123
1234
123456789
输出结果:
123456789
读取这两个文件后以第一个为标准,第二个文件在第一个文件中没有的就将其输出,就像上面的例子一样,第一个文件中“123456789”在第一个文件中没有,所以将“123456789”这行输出。
请问有什么好的方法来实现这个功能!先谢了!
内容如下:
文件1:1
12
123
1234
12345
.
.
文件2:1
12
123
1234
123456789
输出结果:
123456789
读取这两个文件后以第一个为标准,第二个文件在第一个文件中没有的就将其输出,就像上面的例子一样,第一个文件中“123456789”在第一个文件中没有,所以将“123456789”这行输出。
请问有什么好的方法来实现这个功能!先谢了!
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
FileInfo fileA = new FileInfo(@"c:\file1.txt");
FileInfo fileB = new FileInfo(@"c:\file2.txt");
StreamReader readerA = fileA.OpenText();
StreamReader readerB = fileB.OpenText();
string lineA = "";
string lineB = "";
while (!readerA.EndOfStream || !readerB.EndOfStream)
{
lineA = "";
if (!readerA.EndOfStream)
lineA = readerA.ReadLine();
lineB = "";
if (!readerB.EndOfStream)
lineB = readerB.ReadLine();
if (!lineA.Equals(lineB))
Console.WriteLine(lineB);
} }
}
}
string tmp = null;
Hashtable ht = new Hashtable();
using (System.IO.StreamReader sr = new System.IO.StreamReader(fileName))
{
while ((tmp = sr.ReadLine()) != null)
if (tmp.Trim().Length != 0)
ht[tmp] = 0;
} fileName = System.IO.Path.Combine(Application.StartupPath,"2.txt");
using (System.IO.StreamReader sr = new System.IO.StreamReader(fileName))
{
while ((tmp = sr.ReadLine()) != null)
if (!ht.Contains(tmp)) Console.WriteLine(tmp);
}
文件1:
1
12
123
1234
12345 文件2:
123
1234
123456789 或者如行不相等
文件1:
12
123
1234
12345 文件2:
1
1
1
1
1
1
123
1234
123456789
ht[tmp] = 0;
我現在是把讀出的寫到另一個文本中去,但是只能寫入最後一倏記錄,該怎么做!
using (System.IO.StreamReader sr = new System.IO.StreamReader(fileName))
{
fileName = System.IO.Path.Combine(Application.StartupPath,"3.txt");
System.IO.StreamWriter sw = new System.IO.StreamWriter(fileName);
while ((tmp = sr.ReadLine()) != null)
if (!ht.Contains(tmp)) sw.WriteLine(tmp);
sw.Close();
}
FileInfo fileB = new FileInfo(@"d:\file2.txt");
StreamReader readerA = fileA.OpenText();
StreamReader readerB = fileB.OpenText();
string lineA = readerA.ReadToEnd().Replace(" ", "");
string lineB = readerB.ReadToEnd().Replace(" ","");
string[] stsA = lineA.Split(new String[] { "\r\n" },StringSplitOptions.None);
string[] stsB = lineB.Split(new String[] { "\r\n" }, StringSplitOptions.None); string stResult = String.Empty; foreach (string stB in stsB)
{
bool bFlag = false;
foreach (string stA in stsA)
{
if (stA == stB)
{
bFlag = false;
break;
}
else
{
bFlag = true;
}
}
if (bFlag)
{
stResult += stB + ",";
}
}
MessageBox.Show(stResult);