Hi all, I hope you are well. I am in the process of building a small Java application that reads data in from a unicode text file (.txt) and processes it into a jTextArea. The trouble I am having is that the unicode file contains some Greek characters, most of them are being displayed correctly - but not all.
public void read(String file){
// String file gets value of fileName from calling method (Find).
try{
// A FileInputStream obtains input bytes from a file.
//Remember: here I am instantiating an object of TYPE FileInputStream
//called fstream. I am then calling the constructor of class
//FileInputStream and passing in file as a parameter.
FileInputStream fstream = new FileInputStream(file);
// Get the object of DataInputStream. DIS Creates a DataInputStream
//that uses the specified underlying InputStream. (fstream)
DataInputStream in = new DataInputStream(fstream);
//Reads text from a character-input stream, buffering characters
//so as to provide for the efficient reading of characters,
//arrays, and lines. An InputStreamReader is a bridge from byte
//streams to character streams: It reads bytes and decodes them
//into characters using a specified charset.
BufferedReader br = new BufferedReader(new InputStreamReader(in,"UTF-16"));
// UTF-16 is specified to enable reading of Unicode formatting.
String strLine;
//Read File Line By Line and give contents of line to strLine.
if ((strLine = br.readLine()) != null) {
//readLine reads a line of text,
//Print the content on the console if strLine & readline is
//not empty.
jTextArea1.setText(strLine);
//System.out.println (strLine);
}
//Close the input stream, good practice to close streams after use.
in.close();
}catch (Exception e){//Catch exception if any
jTextArea1.setText("Error, database file not found or missing.");
}
Here is a sample of the output, with the troublesome characters:
(Greek: Βασιλεία τῶν Ῥωμαίων, Basileía tôn Rhōmaíōn)
*
Is there something I have omitted whilst using the streams? Many thanks for reading.
EDIT: I have just realised that I can copy the text directly from the jTextArea, and the characters are copied correctly. (see above sample marked *). This means that the jTextArea must not be displaying them correctly. Might this be to do with the font selection in the jTextArea ? I have chosen Times New Roman font. Also, are my comments regarding the operation of file I/O accurate in the code ? Oh, I have also just noticed that, because I am using the condition "if ((strLine = br.readLine()) != null)", the TextArea is getting set only to the first paragraph of text in the file. If I change this IF to WHILE, it only prints out the last paragraph of text from the .txt.