txt 文件中的特殊字符

发布于 2024-12-15 16:22:06 字数 877 浏览 1 评论 0原文

我正在从 ftp 下载一个带有通用 ftp 库的文本文件。

问题是当我将文件逐行读入数组时,它不接受诸如 æøå 之类的字符。相反,它只显示“?”特点。

这是我的代码,

  FileInputStream fstream = openFileInput("name of text file");
  BufferedReader br = new BufferedReader(new InputStreamReader(fstream, "UTF-8"));
  String strLine;

  ArrayList<String> lines = new ArrayList<String>(); 

  while ((strLine = br.readLine()) != null)   {
      lines.add(strLine);
  }

  String[] linjer = lines.toArray(new String[0]);

  ArrayList<String> imei = new ArrayList<String>(); 

  for(int o=0;o<linjer.length;o++)
  {
      String[] holder = linjer[o].split(" - ");
      imei.add(holder[0] + " - " + holder[2]);
  }

  String[] imeinr = imei.toArray(new String[0]);

我尝试将 UTF-8 放入我的 inputstreamreader 中,并且我尝试使用 UnicodeReader 类,但没有成功。

我对 Java 还很陌生,所以可能只是一些愚蠢的问题,但希望你能提供帮助。 :)

I am downloading a text file from ftp, with common ftp library.

The problem is when i read the file into an array line by line, it doesnt take characters such as æøå. Instead it just show the "?" character.

Here is my code

  FileInputStream fstream = openFileInput("name of text file");
  BufferedReader br = new BufferedReader(new InputStreamReader(fstream, "UTF-8"));
  String strLine;

  ArrayList<String> lines = new ArrayList<String>(); 

  while ((strLine = br.readLine()) != null)   {
      lines.add(strLine);
  }

  String[] linjer = lines.toArray(new String[0]);

  ArrayList<String> imei = new ArrayList<String>(); 

  for(int o=0;o<linjer.length;o++)
  {
      String[] holder = linjer[o].split(" - ");
      imei.add(holder[0] + " - " + holder[2]);
  }

  String[] imeinr = imei.toArray(new String[0]);

I have tried to put UTF-8 in my inputstreamreader, and i have tried with a UnicodeReader class, but with no success.

I am fairly new to Java, so might just be some stupid question, but hope you can help. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酷遇一生 2024-12-22 16:22:06

没有理由使用DataInputStreamDataInputStreamDataOutputStream 类用于序列化原始 Java 数据类型(“序列化”意味着将数据读/写到文件)。您只是逐行读取文本文件的内容,因此没有必要使用 DataInputStream,并且可能会产生不正确的结果。

FileInputStream fstream = openFileInput("name of text file");
//DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream, "UTF-8"));

专业 Java 程序员提示foreach 循环最近被添加到 Java 编程语言中。它允许程序员迭代数组的内容,而无需定义循环计数器。这简化了您的代码,使其更易于阅读和维护。

for(String line : linjer){
  String[] holder = line.split(" - ");
  imei.add(holder[0] + " - " + holder[2]);
}

注意:Foreach 循环也可以与 List 对象一起使用。

There is no reason to use a DataInputStream. The DataInputStream and DataOutputStream classes are used for serializing primitive Java data types ("serializing" means reading/writing data to a file). You are just reading the contents of a text file line by line, so the use of DataInputStream is unnecessary and may produce incorrect results.

FileInputStream fstream = openFileInput("name of text file");
//DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream, "UTF-8"));

Professional Java Programmer Tip: The foreach loop was recently added to the Java programming language. It allows the programmer to iterate through the contents of an array without needing to define a loop counter. This simplifies your code, making it easier to read and maintain over time.

for(String line : linjer){
  String[] holder = line.split(" - ");
  imei.add(holder[0] + " - " + holder[2]);
}

Note: Foreach loops can also be used with List objects.

孤独患者 2024-12-22 16:22:06

我建议该文件可能不是 UTF-8 格式。它可能在 CP1252 或其他东西中,特别是如果您使用的是 Windows。

尝试下载该文件并在本地副本上运行代码以查看是否有效。

I would suggest that the file may not be in UTF-8. It could be in CP1252 or something, especially if you're using Windows.

Try downloading the file and running your code on the local copy to see if that works.

羞稚 2024-12-22 16:22:06

FTP有二进制和ascii两种模式。确保您使用正确的模式。查看此处了解详细信息: http://www.rhinosoft.com/newsletter/NewsL2008- 03-18.asp

FTP has two modes binary and ascii. Make sure you are using the correct mode. Look here for details: http://www.rhinosoft.com/newsletter/NewsL2008-03-18.asp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文