Sthokala
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.MalformedInputExcep tion. 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.getResourceA sStream(ht mlFile);
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().toS tring();
isLoop = false;
}
}
if (isLoop)
forLoop.add(linetext);
else
newHtmlFile.append(linetex t);
linetext = br.readLine();
}
br.close();
I am getting below exception at br.readLine()
java.lang.Exception: sun.io.MalformedInputExcep tion
Caused by: sun.io.MalformedInputExcep tion: null
at sun.io.ByteToCharUTF8.conv ert(ByteTo CharUTF8.j ava:278) ~[na:1.5.0]
at sun.nio.cs.StreamDecoder$C onverterSD .convertIn to(StreamD ecoder.jav a:316) ~[na:1.5.0]
at sun.nio.cs.StreamDecoder$C onverterSD .implRead( StreamDeco der.java:3 66) ~[na:1.5.0]
at sun.nio.cs.StreamDecoder.r ead(Stream Decoder.ja va:252) ~[na:1.5.0]
at java.io.InputStreamReader. read(Input StreamRead er.java:21 2) ~[na:1.5.0]
at java.io.BufferedReader.fil l(Buffered Reader.jav a:157) ~[na:1.5.0]
at java.io.BufferedReader.rea dLine(Buff eredReader .java:320) ~[na:1.5.0]
at java.io.BufferedReader.rea dLine(Buff eredReader .java:383) ~[na:1.5.0]
I am trying to read an html file which is in French from buffer reader and I am getting java.lang.Exception: sun.io.MalformedInputExcep
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.getResourceA
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().toS
isLoop = false;
}
}
if (isLoop)
forLoop.add(linetext);
else
newHtmlFile.append(linetex
linetext = br.readLine();
}
br.close();
I am getting below exception at br.readLine()
java.lang.Exception: sun.io.MalformedInputExcep
Caused by: sun.io.MalformedInputExcep
at sun.io.ByteToCharUTF8.conv
at sun.nio.cs.StreamDecoder$C
at sun.nio.cs.StreamDecoder$C
at sun.nio.cs.StreamDecoder.r
at java.io.InputStreamReader.
at java.io.BufferedReader.fil
at java.io.BufferedReader.rea
at java.io.BufferedReader.rea
It's probably not UTF-8. You need to read it with the same encoding as was used to write it.
ASKER
Hello,
Thank yuo for your reply. Now the system is not throwing error, but I am getting ? in place of È.
Thank you
Thank yuo for your reply. Now the system is not throwing error, but I am getting ? in place of È.
Thank you
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
how do you get this file? if it is with http request, then the charset is presented in the header.
Reader in = new InputStreamReader(ins, "UTF-8");
try with:
Reader in = new InputStreamReader(ins);