ProfessionalAmateur ProfessionalAmateur - 9 days ago 8
Java Question

Reading a line from a text file using FileReader, using System.out.println seems print in unicode?

Im still teaching myself Java so I wanted to try to read a text file and step 1) output it to console and step 2) write the contents to a new txt file.

Here is some code I have google'd to start with and it is reading the file, but when I output the line contents to the console I get the following (looks like its outputting in unicode or something... like every character as an extra byte associated to it....

ÿþFF□u□l□l□ □T□i□l□t□ □P□o□k□e□r□ <SNIP>


Here is what the first line of the file looks like when I open in via notepad:
Full Tilt Poker Game #xxxxxxxxxx: $1 + $0.20 Sit & Go (xxxxxxxx), Table 1 - 15/30 - No Limit Hold'em - 22:09:45 ET - 2009/12/26


Here is my code, do I need to specify the encoding to display txt file contents to the console? I assumed that simple text would be straight forward for java...but Im new and don't understand much about how finicky java is yet.

EDIT: I dont know if it matters but Im using Eclipse as my IDE currently.

package readWrite;

import java.io.*;

public class Read {
public static void main(String args[])
{
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("C:\\Users\\brian\\workspace\\downloads\\poker_text.txt"));
String line = reader.readLine();

while (line!=null) {
// Print read line
System.out.println(line);

// Read next line for while condition
line = reader.readLine();
}

} catch (IOException ioe) {
System.out.println(ioe.getMessage());
} finally {
try { if (reader!=null) reader.close(); } catch (Exception e) {}
}
}
}

Answer

The ÿþ at the beginning appears to be a Byte Order Mark for a UTF-16 encoded file.

http://en.wikipedia.org/wiki/Byte_order_mark#UTF-16

You might need to read the file in a different manner so Java can convert those UTF-16 characters to something your System.out can display.

Try something like this

FileInputStream fis = new FileInputStream("filename");
BufferedReader reader = new BufferedReader(new InputStreamReader(fis, "UTF-16"));

OR

Open up your text file in notepad again, and File/Save As. On the save screen (at least in windows 7) there is a pulldown with the encoding setting. Choose ANSI or UTF-8

Comments