java - Convert .txt file in UCS-2 file format -
i have .txt file , want convert file ucs-2 format
should correct way convert
file 700mb can not open in notepad ++ n convert
please suggest .
ok, so, first of all: notepad++ shows ansi, , ansi not character encoding. according this answer , various others, appears windows-1252.
as ucs-2, has been superseded utf-16 can encode more code points. anyway, @ time ucs-2 defined, encoded more code points windows-1252, using utf-16 ok here.
however, utf-16, usc-2 did, depends on endianness. assume little endian here.
therefore:
final path src = paths.get("/path/to/original/file.txt") final path dst = paths.get("/path/to/destination/file.txt"); final char[] buf = new char[1 << 20]; // 1 mb char buffer int nrchars; try ( final bufferedreader reader = files.newbufferedreader(src, charset.forname("windows-1252")); final bufferedwriter writer = files.newbufferedwriter(dst, standardcharsets.utf_16le, standardopenoption.create); ) { while ((nrchars = reader.read(buf, 0, buf.length)) != -1) writer.write(buf, 0, nrchars); writer.flush(); }
this should work.
Comments
Post a Comment