Solved

java.lang.Exception: sun.io.MalformedInputException

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why cannot we forward request once the response is committed 2 42
servlet example 11 40
configure dependency in POM for new database 3 25
Running JavaFX on JDeveloper 12C 1 31
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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…

776 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