Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #33396

Re: Can anyone help me how to read UTF-16 file in java 7.

From Arne Vajhøj <arne@vajhoej.dk>
Newsgroups comp.lang.java.programmer
Subject Re: Can anyone help me how to read UTF-16 file in java 7.
Date 2015-12-27 09:21 -0500
Organization Aioe.org NNTP Server
Message-ID <n5os5f$7d3$1@speranza.aioe.org> (permalink)
References <dad762d3-7cf8-4c8b-bc2d-17b05065fd35@googlegroups.com>

Show all headers | View raw


On 12/27/2015 12:48 AM, Prashant Savadi wrote:
> I am trying read UTF-16 encoded file in JDK 7, can anyone help me out. I can able to read in Python by:
>
> fread = open(r'sample.dat', 'rb').read()
> mytext = fread.decode('utf-16')
> print mytext
> I tried so many ways in java 7 but not succeeded. Thanks in advance.

Traditional:

BufferedReader br = new BufferedReader(new InputStreamReader(new 
FileInputStream(fnm), "UTF-16"));
String line;
while((line = br.readLine()) != null) {
     // process line
}

More close to youe Python code:

List<String> lines = Files.readAllLines(fnm, StandardCharSets.UTF_16);

But there are plenty of alternatives.

Arne

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Can anyone help me how to read UTF-16 file in java 7. Prashant Savadi <prsh.jkd@gmail.com> - 2015-12-26 21:48 -0800
  Re: Can anyone help me how to read UTF-16 file in java 7. Arne Vajhøj <arne@vajhoej.dk> - 2015-12-27 09:21 -0500
    Re: Can anyone help me how to read UTF-16 file in java 7. Lew <lewbloch@gmail.com> - 2015-12-27 09:39 -0800

csiph-web