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.

Hi all, I have managed to get all of the text file to print to the jTextArea now. For those interested here is what I did :

BufferedReader br = new BufferedReader(new InputStreamReader(in,"UTF-16"));
           String strLine; //string for reading in lines of text
           String allText = "";//string for all collected lines of text
           while ((strLine = br.readLine()) != null)   {
               allText += strLine; //append allText with each line as looping
           }
           in.close();
           jTextArea1.setText(allText);

I set up a second string (allText). Each line that has been read by the readLine() method has been appended to allText. I am, however, still having some display problems with the Greek characters. Any ideas on that ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.