【问题标题】:Encoding conversion for large file大文件的编码转换
【发布时间】:2011-10-01 11:13:41
【问题描述】:

我面临一个大文件(~ 18 GB),从 SQL Server 导出为 Unicode 文本文件,这意味着它的编码是 UTF-16(小端)。该文件现在存储在运行 Linux 的计算机中,但我还没有找到将其转换为 UTF-8 的方法。

起初我尝试使用 iconv,但文件太大了。我的下一个方法是使用拆分并逐个转换文件,但这也不起作用 - 转换过程中出现了很多错误。

那么,关于如何将其转换为 UTF-8 的任何想法?任何帮助将不胜感激。

【问题讨论】:

    标签: utf-8 large-files utf-16 iconv


    【解决方案1】:

    由于您使用的是 SQL 服务器,我假设您的平台是 Windows。在最简单的情况下,您可以快速编写一个脏 .NET 应用程序,该应用程序逐行读取源代码并在转换后的文件中写入。像这样的:

    using System;
    using System.IO;
    using System.Text;
    
    namespace UTFConv {
        class Program {
            static void Main(string[] args) {
                try {
                    Encoding encSrc = Encoding.Unicode;
                    Encoding encDst = Encoding.UTF8;
                    uint lines = 0;
                    using (StreamReader src = new StreamReader(args[0], encSrc)) {
                        using (StreamWriter dest = new StreamWriter(args[1], false, encDst)) {
                            string ln;
                            while ((ln = src.ReadLine()) != null) {
                                lines++;
                                dest.WriteLine(ln);
                            }
                        }
                    }
                    Console.WriteLine("Converted {0} lines", lines);
                } catch (Exception x) {
                    Console.WriteLine("Problem converting the file: {0}", x.Message);
                }
            }
        }
    }
    

    只需打开 Visual Studio,启动一个新的 C# 控制台应用程序项目,将这段代码粘贴到那里,编译并从命令行运行它。第一个参数是你的源文件,第二个参数是你的目标文件。应该可以。

    【讨论】:

    • 嗨,ROM,我正在从异常中获取内存
    猜你喜欢
    • 2014-05-20
    • 2021-01-21
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多