我正在制作一个程序,该程序读取.tyd
文件并尝试翻译";从英语到意大利语。
GoToDesk翻译->";寻找计算机";
问题是,我仍然得到了";内存不足";。
代码为:
using System.Collections;
using System.Net;
using MiscUtil.IO;
namespace Soft_inc
{
class MyProject
{
public static string TranslateText(string input, string languagePair)
{
string url = String.Format("http://translate.google.it/?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair);
WebClient webClient = new WebClient();
webClient.Encoding = System.Text.Encoding.UTF8;
string result = webClient.DownloadString(url);
result = result.Substring(result.IndexOf("<span title="") + "<span title="".Length);
result = result.Substring(result.IndexOf(">") + 1);
result = result.Substring(0, result.IndexOf("</span>"));
return result.Trim();
}
public static void Main()
{
string path_file = @"H:GamesSoftware.In.v11.7.62Software.In.v11.7.62LocalizationItalianoidkUI.tyd";
string Ftext = System.IO.File.ReadAllText(path_file);
ArrayList ar = new ArrayList();
Console.WriteLine("This may require some time.");
foreach (string line in new LineReader(() => new StringReader(Ftext)))
{
if(line.IndexOf(""") == -1) continue;
string text = line.Substring(line.IndexOf("""));
text = text.Replace(""","");
if(text.Length == 0) continue;
ar.Add(text);
}
int idk = 0;
while(true)
{
idk++;
if(idk == ar.Count) break;
string oldT = (string)ar[idk];
Ftext = Ftext.Replace(oldT, TranslateText(oldT,"en|it"));
}
System.IO.File.WriteAllText("UI.tyd",Ftext);
}
}
}
也许是因为文件有2535行文本?我该怎么解决这个问题?
您需要使用StreamReader类。没有必要将所有文件内容读取到RAM中。打开一个StreamReader和一个StreamWriter。逐行整理文件,并将翻译后的数据写入临时文件。翻译完所有内容后,只需将临时文件移动到所需的目的地即可。在移动之前,不要忘记关闭源和目标句柄。