Ric*_*lli 7 java unicode file-io matlab utf-16
我正在使用Matlab创建UTF16文本文件,我稍后使用Java阅读.在Matlab中,我打开一个名为fileName的文件,并按如下方式写入:
fid = fopen(fileName, 'w','n','UTF16-LE');
fprintf(fid,"Some stuff.");
Run Code Online (Sandbox Code Playgroud)
在Java中,我可以使用以下代码读取文本文件:
FileInputStream fileInputStream = new FileInputStream(fileName);
Scanner scanner = new Scanner(fileInputStream, "UTF-16LE");
String s = scanner.nextLine();
Run Code Online (Sandbox Code Playgroud)
这是十六进制输出:
Offset(h) 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 00000000 73 00 6F 00 6D 00 65 00 20 00 73 00 74 00 75 00 66 00 66 00 s.o.m.e. .s.t.u.f.f.
上述方法工作正常.但是,我希望能够使用带有BOM的UTF16写出文件,以便为我提供更大的灵活性,这样我就不必担心大端或小端.在Matlab中,我编码:
fid = fopen(fileName, 'w','n','UTF16');
fprintf(fid,"Some stuff.");
Run Code Online (Sandbox Code Playgroud)
在Java中,我将代码更改为:
FileInputStream fileInputStream = new FileInputStream(fileName);
Scanner scanner = new Scanner(fileInputStream, "UTF-16");
String s = scanner.nextLine();
Run Code Online (Sandbox Code Playgroud)
在这种情况下,字符串s是乱码,因为Matlab没有写BOM.如果我手动添加BOM,我可以使Java代码正常工作.使用添加的BOM,以下文件可以正常工作.
Offset(h) 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 00000000 FF FE 73 00 6F 00 6D 00 65 00 20 00 73 00 74 00 75 00 66 00 66 00 ÿþs.o.m.e. .s.t.u.f.f.
如何让Matlab写出BOM?我知道我可以单独编写BOM,但我宁愿让Matlab自动完成.
附录
我从Amro中选择了以下答案,因为它完全解决了我提出的问题.
对我来说,一个关键的发现是Unicode标准和UTF(Unicode转换格式)之间的区别(参见http://unicode.org/faq/utf_bom.html).Unicode标准为字符提供唯一标识符(代码点).UTF提供每个代码点到"唯一字节序列"的映射.由于我使用的几个字符都在前128个代码点中,所以我将转而使用UTF-8,正如Romeo建议的那样.Matlab支持UTF-8(下面显示的警告不需要被抑制.)和Java,对于我的应用程序将生成较小的文本文件.
我压制了Matlab警告
Warning: The encoding 'UTF-16LE' is not supported.
Run Code Online (Sandbox Code Playgroud)
同
warning off MATLAB:iofun:UnsupportedEncoding;
Run Code Online (Sandbox Code Playgroud)
尝试以下代码(我使用UNICODE2NATIVE和NATIVE2UNICODE函数来进行转换):
\n\n%# convert string and write as bytes\nstr = \'Some stuff.\';\nb = unicode2native(str,\'UTF-16\');\nfid = fopen(\'utf16.txt\',\'wb\');\nfwrite(fid, b, \'*uint8\');\nfclose(fid);\n
Run Code Online (Sandbox Code Playgroud)\n\n我们甚至可以检查写入字节的十六进制值(前两个是 BOM ):
\n\n>> cellstr(dec2hex(b))\'\nans = \n Columns 1 through 10\n \'FF\' \'FE\' \'53\' \'00\' \'6F\' \'00\' \'6D\' \'00\' \'65\' \'00\'\n Columns 11 through 20\n \'20\' \'00\' \'73\' \'00\' \'74\' \'00\' \'75\' \'00\' \'66\' \'00\'\n Columns 21 through 24\n \'66\' \'00\' \'2E\' \'00\'\n\n>> char(b)\nans =\n\xc3\xbf\xc3\xbeS o m e s t u f f . \n
Run Code Online (Sandbox Code Playgroud)\n\n现在我们可以使用 MATLAB 自己的方法读取创建的文件:
\n\n>> cellstr(dec2hex(b))\'\nans = \n Columns 1 through 10\n \'FF\' \'FE\' \'53\' \'00\' \'6F\' \'00\' \'6D\' \'00\' \'65\' \'00\'\n Columns 11 through 20\n \'20\' \'00\' \'73\' \'00\' \'74\' \'00\' \'75\' \'00\' \'66\' \'00\'\n Columns 21 through 24\n \'66\' \'00\' \'2E\' \'00\'\n\n>> char(b)\nans =\n\xc3\xbf\xc3\xbeS o m e s t u f f . \n
Run Code Online (Sandbox Code Playgroud)\n\n如果您愿意,也可以直接使用Java方法:
\n\n%# read bytes and convert back to Unicode string\nfid = fopen(\'utf16.txt\', \'rb\');\nb = fread(fid, \'*uint8\')\'; %\'\nfclose(fid);\nstr = native2unicode(b,\'UTF-16\')\n
Run Code Online (Sandbox Code Playgroud)\n\n两者都应该正确读取字符串...
\n 归档时间: |
|
查看次数: |
1785 次 |
最近记录: |