Scanner problem when reading file
Hi, I am using Scanner to read the contents of a txt file and it is working great with this code:
Code:
mReadmeReader = new Scanner(new File("README.txt"));
while(mReadmeReader.hasNextLine())
{
String mHelpText += mReadmeReader.nextLine() + "<br />";
}
return "<html><body>" + mHelpText + "</body></html>";
This I then put in a JLabel to display it in a JFrame.
But when I edit the txt file the scanner stops reading the lines in teh file and this code only returns this:
Quote:
<html><body></body></html>
Would anybody know why this happens to me?
Re: Scanner problem when reading file
Are you seeing any exceptions? You don't have any empty catch blocks do you? How do you change the text file? Do you change the text file name? Have you either used a debugger or added println statements in your code to see what may not be working correctly?
Re: Scanner problem when reading file
my try catch looks like this:
Code:
try
{
mReadmeReader = new Scanner(new File("README.txt"));
}
catch (FileNotFoundException e)
{
System.err.println("Error: could not find file path!\nMessage: " + e + "\nPlease make sure that the path to this folder from the projects main folder exists.");
}
catch (IOException e)
{
System.err.println("Error: could not create/write to file. Check your access to file or folder. Message: " + e);
}
and I get no exceptions but I do println the return value and its what is this:
Quote:
<html><body></body></html>
What I do is open the textfile in notepad and change the text and just save it
it does not get in to this while loop:
Code:
while(mReadmeReader.hasNextLine())
{
mHelpText += mReadmeReader.nextLine() + "<br />";
}
Re: Scanner problem when reading file
Put println statements inside of the method, specifically get the String returned from the nextLine() and print it out, and then add that same String to your concatenating String (better to use a StringBuilder for this).
Re: Scanner problem when reading file
I can not println the nextLine() because it tells me this:
Exception in thread "AWT-EventQueue-0" java.util.NoSuchElementException: No line found
Scanner suddenly thinks there are no lines in the file, but its so strange for me.. it works with the .txt that I copied in from another folder.
I can edit the text and add like 4 lines of code but if I paste what I need to be in the file it don't work.....
Re: Scanner problem when reading file
oh my god, this has to do with that I am using å ä ö letters, I think Scanner has a problem with reading these !
Re: Scanner problem when reading file
Perhaps you want to use the Scanner constructor that lets you also pass in a charsetName.
Re: Scanner problem when reading file
I do use this now:
Code:
mReadmeReader = new Scanner(new File("README.txt"), "CP850");
but then
å = Õ
ä = õ
ö = ÷
I read many on places that said CP850 is the correct one to use. Would you happen to know why it gives me this?
Re: Scanner problem when reading file
can it be that JLabel can not hold å ä ö, because this is what I display the text with?