Solved

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

Posted on 2006-11-27
13
430 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:cplusplus030999
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
13 Comments
 
LVL 12

Expert Comment

by:enachemc
ID: 18019846
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.
0
 

Author Comment

by:cplusplus030999
ID: 18019887
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
0
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18019902
It would be nice to see the value of pFile and also the precise error message.

Mark
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:cplusplus030999
ID: 18020078
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.

0
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18020090
C\: ?? shouldn't that be C:\

Mark
0
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18020098
I mean:

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

: does not need escaping does it?

Mark

0
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18020117
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
0
 

Author Comment

by:cplusplus030999
ID: 18020239
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.
0
 
LVL 10

Accepted Solution

by:
ADSLMark earned 125 total points
ID: 18020425
Ok, i understand.

I made my own utf-8 file with some japanese/chinese characters. I dragged it into a hex editor and i saw 0x0D 0x0A (which is carriage return and new line) in there, so i copied the code above and tested if i received multiple lines or just one and i received more lines. So i guess the error (which i obvious cannot reproduce since i do not have the file) seems to be on line 1.

I think the file is not accessible for outsiders?

Mark
0
 

Author Comment

by:cplusplus030999
ID: 18020761
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
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 125 total points
ID: 18022433
Use a http://java.sun.com/j2se/1.5.0/docs/api/java/nio/charset/CharsetDecoder.html

It will allow you to trap encoding exceptions in detail
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question