C# StreamWriter 以日语输出

Jos*_*orn 2 c# streamwriter

出于某种奇怪的原因,我正在从 .txt(记事本)文件中读取数据,我只想去掉第一个 5 位数字。程序运行后,我的输出文件以我认为是日语的格式打印。我将在下面发布一些代码以及示例输入和输出。任何帮助将不胜感激追查为什么会发生这种情况。谢谢你。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace BasicFileStripper
{
class Program
{
    static void Main(string[] args)
    {
        //var encoding = new ASCIIEncoding();
        StreamWriter write = new StreamWriter(@"C:\Users\Josh\Desktop\MillerEpicOutputs\ClientIds.txt");
        StreamReader read = new StreamReader(@"C:\Users\Josh\Desktop\MillerEpicOutputs\j.txt");//, encoding);
        string line;
        int count = 0;
        write.Write("{");
        while ((line = read.ReadLine()) != null)
        {
            count++;
            string copy = line.Substring(0, 5);
            write.Write(copy + ",");
            Console.WriteLine(line);
        }
        write.WriteLine("};");
        write.WriteLine("Count: " + count);
        write.Close();
    }
}
}
Run Code Online (Sandbox Code Playgroud)

样本输入:

68669 - (DO NOT USE)
68363 - 100 Men of Blue Hills
68364 - 10484 Marty LLC
68365 - 21st Century Therapy
69006 - 21st Century Therapy PC
69007 - 31 Dodge Partnership
69008 - 34 Merriam, LLC
69009 - 3525 Sage Council of Co-Owners
Run Code Online (Sandbox Code Playgroud)

示例输出:??????????????????????????????????

cyn*_*nic 5

我假设您正在尝试在记事本中读取输出(请参阅(这篇维基百科文章)。使用StreamWriter(String, Boolean, Encoding)构造函数with Encoding.UTF8,这将导致将 BOM 写入输出文件,使记事本正确读取它。如果您不需要要在记事本中阅读它,保持原样,并注意读取它并期望它是 UTF-8 的任何其他东西都会正确读取它。