txt 文件中的特殊字符
我正在从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没有理由使用
DataInputStream
。DataInputStream
和DataOutputStream
类用于序列化原始 Java 数据类型(“序列化”意味着将数据读/写到文件)。您只是逐行读取文本文件的内容,因此没有必要使用 DataInputStream,并且可能会产生不正确的结果。专业 Java 程序员提示:foreach 循环最近被添加到 Java 编程语言中。它允许程序员迭代数组的内容,而无需定义循环计数器。这简化了您的代码,使其更易于阅读和维护。
注意:Foreach 循环也可以与
List
对象一起使用。There is no reason to use a
DataInputStream
. TheDataInputStream
andDataOutputStream
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 ofDataInputStream
is unnecessary and may produce incorrect results.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.
Note: Foreach loops can also be used with
List
objects.我建议该文件可能不是 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.
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