IT干货网

c#之使用 UTF-8 解码文件流

jirigala 2024年12月31日 编程设计 53 0

我有一个 XML 文档,它非常大(大约 120M),我不想立即将其加载到内存中。我的目的是检查此文件是否使用有效的 UTF-8 编码。

在不将整个文件以 byte[] 的形式读入内存的情况下进行快速检查的任何想法?

我正在使用 VSTS 2008 和 C#。

使用时 XMLDocument加载一个包含无效字节序列的 XML 文档,有一个异常(exception),但是当将所有内容读入一个字节数组然后检查 UTF-8 时,没有任何异常(exception),有什么想法吗?

这是显示我的 XML 文件内容的屏幕截图,或者您可以从 here 下载该文件的副本。



编辑 1:

class Program 
{ 
    public static byte[] RawReadingTest(string fileName) 
    { 
        byte[] buff = null; 
 
        try 
        { 
            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); 
            BinaryReader br = new BinaryReader(fs); 
            long numBytes = new FileInfo(fileName).Length; 
            buff = br.ReadBytes((int)numBytes); 
        } 
        catch (Exception ex) 
        { 
            Console.WriteLine(ex.Message); 
        } 
 
        return buff; 
    } 
 
    static void XMLTest() 
    { 
        try 
        { 
            XmlDocument xDoc = new XmlDocument(); 
            xDoc.Load("c:\\abc.xml"); 
        } 
        catch (Exception ex) 
        { 
            Console.WriteLine(ex.Message); 
        } 
    } 
 
    static void Main() 
    { 
        try 
        { 
            XMLTest(); 
            Encoding ae = Encoding.GetEncoding("utf-8"); 
            string filename = "c:\\abc.xml"; 
            ae.GetString(RawReadingTest(filename)); 
        } 
        catch (Exception ex) 
        { 
            Console.WriteLine(ex.Message); 
        } 
 
        return; 
    } 
} 

编辑 2:使用时 new UTF8Encoding(true, true)会有异常(exception),但是当使用 new UTF8Encoding(false, true) 时,没有抛出异常。我很困惑,因为它应该是控制是否抛出异常的第二个参数(如果有无效的字节序列),为什么第一个参数很重要?
    public static void TestTextReader2() 
    { 
        try 
        { 
            // Create an instance of StreamReader to read from a file. 
            // The using statement also closes the StreamReader. 
            using (StreamReader sr = new StreamReader( 
                "c:\\a.xml", 
                new UTF8Encoding(true, true) 
                )) 
            { 
                int bufferSize = 10 * 1024 * 1024; //could be anything 
                char[] buffer = new char[bufferSize]; 
                // Read from the file until the end of the file is reached. 
                int actualsize = sr.Read(buffer, 0, bufferSize); 
                while (actualsize > 0) 
                { 
                    actualsize = sr.Read(buffer, 0, bufferSize); 
                } 
            } 
        } 
        catch (Exception e) 
        { 
            // Let the user know what went wrong. 
            Console.WriteLine("The file could not be read:"); 
            Console.WriteLine(e.Message); 
        } 
 
    } 

请您参考如下方法:

var buffer = new char[32768] ; 
 
using (var stream = new StreamReader (pathToFile,  
    new UTF8Encoding (true, true))) 
{ 
    while (true) 
    try 
    { 
        if (stream.Read (buffer, 0, buffer.Length) == 0) 
            return GoodUTF8File ; 
    } 
    catch (ArgumentException) 
    { 
        return BadUTF8File ; 
    } 
} 


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!