cjtightpant
cjtightpant

Reputation: 69

Read Windows text file on Linux --- MIME issues?

I'm trying to read a file, of MIME type "appliaction/octet-stream" line by line via a Java application running on a Linux PC. Clarification: "appliaction/octet-stream" was the result of running "file -ib file.txt" on Linux.

The file I'm trying to read was created on Windows XP.

I've called my file "file.txt".

On linux, "cat file.txt" displays the contents. "cat -v" as well as vim shows the control characters.

When I run code to iterate through it via my Java application (using simple BufferedReader(FileReader) type of code), my output is unexpected.

Any approached I should take? I tried converting the file using dos2unix, but no avail.

EDIT: the input file, when read through vim or "cat -v" is as follows:

[^@S^@y^@s^@t^@e^@m^@]^@^M^@ 

The line simply says "System", but it seems the control characters are rendering the file unreadable via my Java app.

UPDATE: I ran my code using all available Character encodings, and it turns out that the readable CharSets were "x-UTF-16LE-BOM" and "COMPOUND-TEXT". Thanks to everyone for their help.

Upvotes: 0

Views: 575

Answers (1)

TypeIA
TypeIA

Reputation: 17258

Looks like the file was written using the UTF-16 encoding. To read this in Java, you'll just need to specify that encoding in your reader:

InputStreamReader reader = new InputStreamReader(
    new FileInputStream(filename), Charset.forName("UTF-16"));

Upvotes: 2

Related Questions