Solved

java.lang.Exception: sun.io.MalformedInputException

Posted on 2014-04-17
5
1,286 Views
Last Modified: 2014-04-17
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]
0
Comment
Question by:Sthokala
  • 3
5 Comments
 
LVL 16

Expert Comment

by:Valeri
ID: 40006581
instead of
Reader in = new InputStreamReader(ins, "UTF-8");
try with:
Reader in = new InputStreamReader(ins);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40006619
It's probably not UTF-8. You need to read it with the same encoding as was used to write it.
0
 

Author Comment

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

Thank you
0
 
LVL 16

Accepted Solution

by:
Valeri earned 500 total points
ID: 40006638
what do you get instead of È ?

what about this:
Reader in = new InputStreamReader(ins, Charset.forName("ISO-8859-1"));
0
 
LVL 16

Expert Comment

by:Valeri
ID: 40006647
how do you get this file? if it is with http request, then the charset is presented in the header.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 85
Java / Linux and Regular Expressions 11 69
Java Jpanels and Jframe 8 28
java jdbc batch example 8 20
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now