Hi there,
Very new to the forum, and new to
Java and programming in general. I am writing a fairly straight-forward program that reads in text from a file. Some of the text is english, and some of it is in a UTF-8 encoded Hebrew. In hebrew, each letter has a number and a meaning associated with it,
so I built a class that associates those things, but for right now, all I am trying to do is read in a simple file with four sentences, the last sentence being partially in hebrew and partially in english.
This here is the file being read:
This is a simple Line of text. This is the second sentence. This is the third.
This is the second line.
This is the third.
This is a series of numbers: 123456789
And here are some Unicode characters: אבגדהוזחטכלמסך
The file was written in Notepad and saved using UTF-8 encoding. I have tried every encoding, from BigEndian to UTF-8 by now.
This is in a Swing Application, using Netbeans. Specifically, this is within the event handler for an openFileChooser dialogue box. So I have copied the relevant code from that function and pasted it below. The code validates the user's selection, passes it into a FileInputStream,
then into a Reader, then into a BufferedReader. With regular English, the code works fine, and counts the number of characters, and prints each line with the readLine function. But as soon as the .readLine function enters the line containing hebrew glyphs, it has problems.
It returns null. My best attempt got it to return little black sqaures indicating that the font didn't contain that glyph....except even when I used a font that absolutely contaisn that glyph I had the same problem. I've been able to manage by myself fine without forums until this point....I have been all across the
internet for hours now, to no avail. Any help would be greatly appreciated. Below is the relevant code, along with the error log:
STARTING WITH THE FILECHOOSER EVENT HANDLER:
private void openFileButtonActionPerformed(java.awt.event.ActionEvent evt) {
// Create FileChooser Object, create file association objects and tie them to it
//Create create a
test to make sure it is either a pure test file, a cvs file, or a PDF (ADD pdf CAPABILITIES AS WE go
int returnValue = openFileChooser.showOpenDialog(this);
if (returnValue == JFileChooser.APPROVE_OPTION)
{
try
{
File fileHebrewText = openFileChooser.getSelectedFile();
FileInputStream finStream = new FileInputStream(fileHebrewText);
boolean doesExist = fileHebrewText.exists();
if (doesExist)
System.out.println("The file sure does exist.");
else System.out.println("The file is having difficulties being found.");
System.out.println(fileHebrewText.getAbsoluteFile().toString());
FileReader fileReader = new FileReader(fileHebrewText);
String path = fileHebrewText.getAbsolutePath();
System.out.println(" this is the path " + path);
BufferedReader buffReader;
buffReader = new BufferedReader(new InputStreamReader(finStream, "UTF-8"));
StringBuilder fileToString = new StringBuilder();
StringBuilder append = new StringBuilder();
int check;
int length;
String line = null;
LivingLetters glyphArray;
int characterCount = 0;
while((check = buffReader.read()) != -1)
{
characterCount++;
}
System.out.println("There are " + characterCount + " in this file.");
System.out.println(buffReader.readLine());
finStream.getChannel().position(0);
BufferedReader newReader = new BufferedReader(new InputStreamReader(finStream, "UTF-8"));
System.out.println(buffReader.readLine());
finStream.getChannel().position(0);
BufferedReader thirdReader = new BufferedReader(new InputStreamReader(finStream, "UTF-8"));
String unicodeString = new String();
while((line = thirdReader.readLine()) != null)
{
unicodeString = thirdReader.readLine();
System.out.println(unicodeString);
}.........
the rest of the function after this is irrelevant.
And here is the output generated:
OUTPUT:
Very beginning
Exception in
thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0
The file sure does exist.
C:\Users\Israel\Documents\NetBeansProjects\TheLivingLetters\src\UTF_8.txt
this is the path C:\Users\Israel\Documents\NetBeansProjects\TheLivingLetters\src\UTF_8.txt
There are 225 in this file.
at thelivingletters.TheLivingLettersForm.openFileButtonActionPerformed(TheLivingLettersForm.java:949)
at thelivingletters.TheLivingLettersForm.access$3200(TheLivingLettersForm.java:47null
This is a simple Line of text. This is the second sentence. This is the third.
This is the second line.
This is a series of numbers: 123456789
null
YOU ARE HERE
In file reading loop
)
Thanks in advance to anyone who can help me understand this problem.