先看下面的程序using System;
using System.IO;class FileRWRandom
{
    static void Main()
    {        Console.WriteLine("输入所要读取的文件名称 :");
        string readFileName = Console.ReadLine();
        Console.WriteLine("输入开始读取的位置 :");
        int position = int.Parse(Console.ReadLine());
        Console.WriteLine("输入所要复制的文件名称 :");
        string writFILEeName = Console.ReadLine();        FileStream FileReadStream = new FileStream
          (readFileName, FileMode.Open, FileAccess.Read);
        FileReadStream.Position = position;        FileStream FileWriteStream = new FileStream
          (writFILEeName, FileMode.Create, FileAccess.Write);        byte[] b = new byte[512];
        int intRead = 0;
        intRead = FileReadStream.Read(b, 0, b.Length);
        Console.WriteLine("总共读取 " + intRead + " 个字节 !!");
        FileWriteStream.Write(b, 0, intRead);        FileReadStream.Close();
        FileWriteStream.Close();        Console.Read();    }
}我复制的源文件一部分内容是:
cysui 9AC.9B0 0000 00:00:13:29.0718 opening log file C:\WINDOWS\debug\cysui.log
cysui 9AC.9B0 0001 00:00:13:29.0718 C:\WINDOWS\system32\cys.exe
cysui 9AC.9B0 0002 00:00:13:29.0718 file timestamp 02/17/2007 06:59:50.000
cysui 9AC.9B0 0003 00:00:13:29.0718 local time 03/22/2009 09:35:55.968
cysui 9AC.9B0 0004 00:00:13:29.0718 running Windows NT 5.2 build 3790 Service Pack 2 (BuildLab:3790.srv03_sp2_gdr.080813-1204) i386
cysui 9AC.9B0 0005 00:00:13:29.0718 logging flags 000100BC
cysui 9AC.9B0 0006 00:00:13:29.0765 Enter Start
cysui 9AC.9B0 0007 00:00:13:29.0765   Enter IsCurrentUserAdministrator
cysui 9AC.9B0 0008 00:00:13:29.0765     Current user is an admin
cysui 9AC.9B0 0009 00:00:13:29.0765   Enter Computer::RemoveLeadingBackslashes 
cysui 9AC.9B0 000A 00:00:13:29.0765   Enter Computer::Refresh
cysui 9AC.9B0 000B 00:00:13:29.0765     Enter IsLocalComputer
cysui 9AC.9B0 000C 00:00:13:29.0765     Enter RefreshLocalInformation
cysui 9AC.9B0 000D 00:00:13:29.0765     Enter GetProductTypeFromRegistry
cysui 9AC.9B0 000E 00:00:13:29.0765       Enter RegistryKey::Open 
设置Position的值为:1得到的复制后的文件为: y s u i   9 A C . 9 B 0   0 0 0 0   0 0 : 0 0 : 1 3 : 2 9 . 0 7 1 8   o p e n i n g   l o g   f i l e   C : \ W I N D O W S \ d e b u g \ c y s u i . l o g 
 
 c y s u i   9 A C . 9 B 0   0 0 0 1   0 0 : 0 0 : 1 3 : 2 9 . 0 7 1 8   C : \ W I N D O W S \ s y s t e m 3 2 \ c y s . e x e 
 
 c y s u i   9 A C . 9 B 0   0 0 0 2   0 0 : 0 0 : 1 3 : 2 9 . 0 7 1 8   f i l e   t i m e s t a m p   0 2 / 1 7 / 2 0 0 7   0 6 : 5 9 : 5 0 . 0 0 0 
 
 c y s u i   9 A C . 9 B 0   0 0 0 3   0 0 : 0 0 : 1 3 : 2 9 . 0 7 1请问这是为什么,你可以自己上机试试

解决方案 »

  1.   

    若设置Position = 10;
    复制后的文件为
    i 9AC.9B0 0000 00:00:13:29.0718 opening log file C:\WINDOWS\debug\cysui.log
    cysui 9AC.9B0 0001 00:00:13:29.0718 C:\WINDOWS\system32\cys.exe
    cysui 9AC.9B0 0002 00:00:13:29.0718 file timestamp 02/17/2007 06:59:50.000
    cysui 9AC.9B0 0003 00:00:13:29.0718 lo看的出来是从源文件的第5个字符开始复制的,难道文本文件是以Unicode方式打开文件的?2个2个字节读?
    而FileStream.Positon的设置是以一个字节为单位?
      

  2.   

    因为你的文件可能不是anscii编码的,也就是说一个字母不一定是1个字节,可能是两个字节,
    所以复制后第一个字符乱码
      

  3.   

    你把文件用记事本打开,另存为,选择anscii保存,再读取应该就不会有问题了