从VB.NET写入一个文本文件(编码) [英] Writing a text file from VB.NET (encoding)

查看:356
本文介绍了从VB.NET写入一个文本文件(编码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了必须生成具有固定大小列的纯文本文件的应用程序。

I am writing an application that must generate a plain text file with fixed-sized columns.

我目前的code是:

Dim MyFilePath As String = Path & FILE_PREFIX & FileNr & ".TXT"

IO.File.Delete(MyFilePath)

Dim FileStr As New IO.StreamWriter(MyFilePath, False, <ENCODER HERE>)
Do While r.Read
    FileStr.WriteLine(r("TXTLine"))
Loop
FileStr.Close()
r.Close()

我的问题是,我有一些特殊的字符,如:N,A等,我找不到正确的编码

My problem is that I have some special characters like: "ñ", "à", etc., and I can't find the right encoding.

  • 如果我用的是默认的,那么它取代 N两个角色。
  • 如果我使用ASCII那么所有的特殊 字符最终成为:?
  • 如果我使用 UTF-8 那么所有的文本是确定的, 但它增加在第一字节Y型 该文件。
  • If I use the default, then it replaces "ñ" with two characters.
  • If I use ASCII then all special characters end up as: "?"
  • If I use UTF-8 then all text is OK, but it adds a "ÿ" in the first byte of the file.

我需要的特殊字符写入文本文件,就像他们排在了DataReader的。我不能有多余的字符添加,因为列固定lenght的......

I need the special characters to be written in the textfile just as they came in the datareader. And I can't have extra characters added because columns are of fixed lenght...

我能怎么办?

推荐答案

你是如何检查你有正确的编码?

How are you checking that you got the right encoding?

如果你只是打开该文件在记事本,<一个HREF =htt​​ps://en.wikipedia.org/wiki/Windows-1252相对=nofollow>的Windows 1252 可能是你想要的编码。

If you're simply opening the file up in Notepad, Windows-1252 is probably the encoding you want.

Encoding.GetEncoding("Windows-1252")

会给你。

请注意,我注意到你说的一些列固定长度。你是否被其他工具将数据写入到文件中导入?如果是这样,那么你应该检查什么编码的其他工具要求,没有哪一种工具碰巧看pretty的以任何其他工具,你可以用它来看看用(如记事本)的文件。

Note that I notice that you say that some columns are fixed length. Are you writing data to a file for import by another tool? If so, then you should check what encoding that other tool requires, not which tool happens to look pretty in whatever other tool you might use to look at the file with (like Notepad).

这篇关于从VB.NET写入一个文本文件(编码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆