Link to home
Start Free TrialLog in
Avatar of Sthokala
SthokalaFlag for United States of America

asked on

java.lang.Exception: sun.io.MalformedInputException

Hi,
   I am trying to read an html file which is in French from buffer reader and I am getting java.lang.Exception: sun.io.MalformedInputException. I am attaching the html file and java code which reads the file. Please let me know what is going wrong here.

THank you

Below is the HTML code
<!DOCTYPE html SYSTEM  "xhtml1-transitional.dtd">
<html>
<head>
<title>Council Template</title>
</head>
<body>
<h2></h2>
<h2></h2>
<h2></h2>
<h2></h2>
<h2>RÈGLEMENTS</h2>
<h2></h2>
<h2></h2>
<h2></h2>
<h2></h2>
<h1>CHEVALIERS DE COLOMB</h1>
<h2></h2>
<h2></h2>
<br/>

<br />
</body>
</html>


and java code is

InputStream ins = PdfUtil.class.getResourceAsStream(htmlFile);

            

            Reader in = new InputStreamReader(ins, "UTF-8");
            BufferedReader br = new BufferedReader(in);
            String linetext = br.readLine();
            while (linetext != null) {
                  // Convert Line Code
                  if (linetext.indexOf("<%") > 0) {
                        if (!isLoop) {
                              isLoop = true;
                              forLoop = new ForLoop(linetext);
                              linetext = null;
                        } else {
                              linetext = forLoop.generateLoop().toString();
                              isLoop = false;
                        }
                  }
                  if (isLoop)
                        forLoop.add(linetext);
                  else
                        newHtmlFile.append(linetext);
                  linetext = br.readLine();
            }
            br.close();


I am getting below exception at br.readLine()
java.lang.Exception: sun.io.MalformedInputException
Caused by: sun.io.MalformedInputException: null
at sun.io.ByteToCharUTF8.convert(ByteToCharUTF8.java:278) ~[na:1.5.0]
at sun.nio.cs.StreamDecoder$ConverterSD.convertInto(StreamDecoder.java:316) ~[na:1.5.0]
at sun.nio.cs.StreamDecoder$ConverterSD.implRead(StreamDecoder.java:366) ~[na:1.5.0]
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:252) ~[na:1.5.0]
at java.io.InputStreamReader.read(InputStreamReader.java:212) ~[na:1.5.0]
at java.io.BufferedReader.fill(BufferedReader.java:157) ~[na:1.5.0]
at java.io.BufferedReader.readLine(BufferedReader.java:320) ~[na:1.5.0]
at java.io.BufferedReader.readLine(BufferedReader.java:383) ~[na:1.5.0]
Avatar of Valeri
Valeri
Flag of Bulgaria image

instead of
Reader in = new InputStreamReader(ins, "UTF-8");
try with:
Reader in = new InputStreamReader(ins);
It's probably not UTF-8. You need to read it with the same encoding as was used to write it.
Avatar of Sthokala

ASKER

Hello,
  Thank yuo for your reply. Now the system is not throwing error, but I am getting ? in place of È.

Thank you
ASKER CERTIFIED SOLUTION
Avatar of Valeri
Valeri
Flag of Bulgaria 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
how do you get this file? if it is with http request, then the charset is presented in the header.