在UCS-2中编写文本文件Little Endian,Java [英] Write text file in UCS-2 Little Endian, Java

查看:430
本文介绍了在UCS-2中编写文本文件Little Endian,Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试写入.txt文件,它必须是UCS-2 Little Endian,但是当我尝试

  writer = new PrintWriter(path,UTF-16LE); 

从我读取的内容应该是一样的,但它不会在服务器上的特定应用程序中工作。当我在Notepad ++中打开工作(手动创建)的文件时,它表示它是UCS-2 Little Endian,但是当它像这样在Java中创建时,它表示UCS-2 LE w / o BO,服务器无法读取它。 / p>

我该怎么写才能工作?这是整个代码:

  writer = new PrintWriter(path,UTF-16LE); (int j =(i  -  itemsInPlaylist + 1); j <= i; j ++){
writer.println(listParsedFile.get(j).getNameOfFile());

}
itemsInPlaylist = 0;
writer.close();

感谢任何建议。

解决方案

首先,您需要编写字节顺序标记,以便您应用程序可以检测编码。 (不清楚你的意思是它说的 - 清楚的是,它是可以猜到它是UTF-16LE,但不如可靠的比你有BOM。)



所以只需添加:

  writer.write(\FEFF); 

在您写任何其他内容之前。


I'm trying to write .txt file and it have to be UCS-2 Little Endian, but when I tried

writer = new PrintWriter(path, "UTF-16LE");

From what I read it should be the same, but it won't work in specific application on server. When I open file which works (created manually) in Notepad++ it says that it's "UCS-2 Little Endian" but when it's created in Java like this it says "UCS-2 LE w/o BO" and server cannot read it.

How could I write it so it will work? This is whole code:

writer = new PrintWriter(path, "UTF-16LE");
for (int j = (i - itemsInPlaylist + 1); j <= i; j++) {
    writer.println(listParsedFile.get(j).getNameOfFile());
}
itemsInPlaylist = 0;
writer.close();

Thanks for any suggestions.

解决方案

It sounds like you need to write a byte-order mark first, so that your application can detect the encoding. (It's not clear what you mean by "it says" - clearly whatever "it" is can guess that it's UTF-16LE, but less reliably than when you've got the BOM.)

So just add:

writer.write("\uFEFF");

before you write anything else.

这篇关于在UCS-2中编写文本文件Little Endian,Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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