把d:\333.doc 复制到d:\444.doc 我用这种文件流的方法,不用file.copy 为什么复制完了以后444.doc 变成乱码的了?本人还是新手,请多指教.string c;Stream datastream = new FileStream("d:\\444.doc", FileMode.Create);
StreamWriter nfile = new StreamWriter(datastream, Encoding.ASCII);
using (StreamReader ofile = new StreamReader("d:\\333.doc", Encoding.ASCII))
{
while ((c=ofile.ReadLine())!=null)
{
//Console.WriteLine(ofile.ReadLine().Length);
nfile.WriteLine(c);
Console.WriteLine(c);
}
ofile.Close();
nfile.Close();
}
StreamWriter nfile = new StreamWriter(datastream, Encoding.ASCII);
using (StreamReader ofile = new StreamReader("d:\\333.doc", Encoding.ASCII))
{
while ((c=ofile.ReadLine())!=null)
{
//Console.WriteLine(ofile.ReadLine().Length);
nfile.WriteLine(c);
Console.WriteLine(c);
}
ofile.Close();
nfile.Close();
}
Stream datastream = new FileStream("d:\\444.doc", FileMode.Create);
StreamWriter nfile = new StreamWriter(datastream);
using (StreamReader ofile = new StreamReader("d:\\333.doc"))
{
while ((c=ofile.ReadLine())!=null)
{
//Console.WriteLine(ofile.ReadLine().Length);
nfile.WriteLine(c);
Console.WriteLine(c);
}
ofile.Close();
nfile.Close();
}
变成这样了
但是还是乱码,而且一个24k 的文件复制完了以后变成28k ,比原来还多了4k 呵
所以直接用StreamReader不行,你换一个BinaryReader看看。
Using f As IO.FileStream = New IO.FileStream("d:\1.doc", IO.FileMode.Open)
Dim byteData(1024) As Byte
Dim intLen As Integer = f.Read(byteData, 0, byteData.Length)
While intLen > 0
fs.Write(byteData, 0, intLen)
intLen = f.Read(byteData, 0, byteData.Length)
End While
End Using
End Using
using( FileStream fs = new FileStream("d:\2.doc", FileMode.Create) )
{
using( FileStream f = new FileStream("d:\1.doc", FileMode.Open) )
{
byte[1024] byteData;
int length = f.Read(byteData, 0, byteData.Length);
while(length > 0)
{
fs.Write(byteData, 0, length);
length = f.Read(byteData, 0, byteData.Length);
}
}
}
5楼正解,但缓冲区大小1024太小了,内部缓冲区大小是4096吧~
FileStream fs = new FileStream("d:\\bb.doc", FileMode.Create);
StreamWriter wFile = new StreamWriter(fs,Encoding.GetEncoding("gb2312"));
wFile.Write(ofile.ReadToEnd());
ofile.Close();
wFile.Close();