Link to home
Start Free TrialLog in
Avatar of cplusplus030999
cplusplus030999

asked on

How to find a character in a file (which line and which column) which causes sun.io.MalformedException thrown

I have a piece of code inside try block like the following:

BufferedReader reader = new BufferedReader (new InputStreamReader(new FileInputStream(pFile), "UTF-8"));

text = reader.reader.readLine();  line 1

while(text != null)
{
      text = reader.readLine();
}


//notes: text variable has been defined and pFile is passed from function call.

When I run the piece of code, it always throws sun.io.MalformedException at line 1.

It is very possible that it exists invalid character in pFile.  Is there any way to change the piece code to find which line and which column the invalid character is located so that I can just correct it in pFile.

I did the following changes to find which line but didn't work (this change will not find which column the invalid character is located). It always print linenumber is 1. It sounds that the whole file has only one line after creating reader object using UTF-8

int linenumber = 1;
text = reader.readerLine();

while(text != null)
{
      linenumber++;
      text = reader.readLine();
}

Thanks for the help
Avatar of enachemc
enachemc
Flag of Afghanistan image

since MalformedException does not exist,  you are probably reffering to MalformedURLException. This would be caused by the value of pFile variable. Which is the real value of the variable.
Avatar of cplusplus030999
cplusplus030999

ASKER

sun.io.MalformedException is a famous exception. Like I said, the exception is thrown from line 1. pFile is File object without any problem. I have tried other good file and it works
It would be nice to see the value of pFile and also the precise error message.

Mark
pFile = new File("C\:\\temp\\real_file.html");

C: is c drive, temp is folder and real_file.html is file name. They will exist.

sun.io.MalformedException is just like NullPointerException. If you want to get precise error message, you will be disppointed since it is null.

C\: ?? shouldn't that be C:\

Mark
I mean:

pFile = new File("C:\\temp\\real_file.html");

: does not need escaping does it?

Mark

Well i could have posted this all in one post but you should use the pathSeperator from the File class:

http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html

static String pathSeparator
          The system-dependent path-separator character, represented as a string for convenience.
static char pathSeparatorChar
          The system-dependent path-separator character.
static String separator
          The system-dependent default name-separator character, represented as a string for convenience.
static char separatorChar
          The system-dependent default name-separator character.

Mark
ADSLMark,


pFile = new File("C:\\temp\\real_file.html");
and pFile = new File("C\:\\temp\\real_file.html") both work. Like I said, the file name is not an issue. I have the folder name in property file. It works for other files. Please don't take too much time on this. pFile is absolutely valid File object. Thanks.
ASKER CERTIFIED SOLUTION
Avatar of ADSLMark
ADSLMark

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Since BufferedStream allows UTF-8 transferred but why readLine() can not reacognize that. Obviously readLine is only reading one line. If BufferedStream is UTF-8, it should know that
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial