Solved

java.lang.Exception: sun.io.MalformedInputException

Posted on 2014-04-17
5
1,270 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

757 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

23 Experts available now in Live!

Get 1:1 Help Now