facing problem in reading & writing the files

Hi,

  I have to read & write the files in the Hungarian Language. Can anybody tell me which Cahracter set I should use for read & write  of the file?getting caused by : sun.io.MalformedInputException.

Vikas
vikas31Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Try using the following pattern:


//read


BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileNameIn), "ISO8859_2")));

// write

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileNameOut), "ISO8859_2")));
vikas31Author Commented:
I am using the same but facing problem when applying xslt to the xml the resulting xml have default encoding set to UTF-8 how I can change it during transformation?
CEHJCommented:
Not sure. Can you post some code around the area you're doing that?
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

vikas31Author Commented:
public String map(MapperParams params) throws MapperException, CBSException {
      logger.debug("Executing map");
      logger.debug("mapping parameters" + params.toString());
      String fileExtension = null;
      this.xslMappingFile = params.getXslMappingFile();
      this.inputFile = params.getInputFile();
        logger.debug("this.inputFile....."+inputFile);
      String fileType = "";
      switch (params.getFileType()) {
            case MapperParams.WIT : {
         fileType="_WIT";
         fileExtension = ".xml";
                  break;
            }
            case MapperParams.MIB : {
                  fileType="_MIB";
         fileExtension = ".txt";
                  break;
            }
            default :
                  break;
       }

      this.witXmlFile = inputFile + fileType + fileExtension;

      logger.debug("the new output "+fileType+" XMLFile " + this.witXmlFile);

      DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
      dFactory.setNamespaceAware(true);

         // creating DocumentBuilder
      DocumentBuilder dBuilder = null;
      try {
         dBuilder = dFactory.newDocumentBuilder();
      }
      catch (ParserConfigurationException e) {
         String msg = "DocumentBuilder cannot be created satisfying the configuration requested";
         throw new MapperException(MapperErrorCode.MAPPER_MAP_INIT, this.getClass(), e, msg, "domfactory.newDocumentBuilder() | DocumentBuilder cannot be created");
      }

      // creating XSL Document for mapping
      Document xslDoc = null;
      try {
         xslDoc = dBuilder.parse(FileLoadingUtils.getResourceAsInputStream(xslMappingFile));
      }
      catch (SAXException e) {
         String msg = "Error parsing the given input source (XSL) as an XML document and returning DOM object";
         throw new MapperException(MapperErrorCode.MAPPER_MAP_INIT_XSL, this.getClass(), e, msg, "The XSLPath (stylesheet)    : " + this.xslMappingFile, "Check XSL being well-formed");
      }
      catch (IOException e) {
         String msg = "Path to the transforming stylesheet (XSL) is not correct or does not exist";
         throw new MapperException(MapperErrorCode.MAPPER_MAP_INIT_XSL, this.getClass(), e, msg, "The XSLPath (stylesheet)    : " + this.xslMappingFile);
      }
      DOMSource xslDomSource = new DOMSource(xslDoc);

      // creating XML Document for input
      Document xmlDoc = null;
      try {
         File fs = new File(this.inputFile);
            logger.debug("fs.getName().."+fs.getName());
        logger.debug("input file..."+this.inputFile);
         xmlDoc = dBuilder.parse(fs);
            logger.debug("xmlDoc...."+getDocumentAsString(xmlDoc));
      }
      catch (SAXException e) {
         String msg = "Error parsing the given input source (XML) as an XML document and returning DOM object";
         throw new MapperException(MapperErrorCode.MAPPER_MAP_INIT_XML, this.getClass(), e, msg, "The XMLPath (inputfile)    : " + this.inputFile, "Check XML being well-formed");
      }
      catch (IOException e) {
         String msg = "Path to the inputfile (XML) is not correct or does not exist";
            logger.debug("Got IOException ...");
         throw new MapperException(MapperErrorCode.MAPPER_MAP_INIT_XML, this.getClass(), e, msg, "The XMLPath (inputfile)    : " + this.inputFile);
      }
      DOMSource xmlDomSource = new DOMSource(xmlDoc);

      // creating TransformerFactory
      Transformer transformer = null;
      try {
         transformer = TransformerFactory.newInstance().newTransformer(xslDomSource);
      }
      catch (TransformerConfigurationException e) {
         String msg = "A serious configuration error has occured, Transformer cannot be created satisfying the configuration requested";
         throw new MapperException(MapperErrorCode.MAPPER_MAP_TRANSFORMER_INIT, this.getClass(), e, msg, "The DOMSource (XSL) NodeName   : " + xslDomSource.getNode().getNodeName()); // + "ColumnNumber : " + e.getLocator().getColumnNumber() + "LineNumber : " + e.getLocator().getLineNumber());
      }
      catch (TransformerFactoryConfigurationError transformerFactoryConfigurationError) {
         String msg = "This class of the transformation factory specified in the system properties cannot be found or instantiated";
         throw new MapperException(MapperErrorCode.MAPPER_MAP_TRANSFORMER_INIT, this.getClass(), transformerFactoryConfigurationError, msg, "The DOMSource (XSL) NodeName   : " + xslDomSource.getNode().getNodeName());
      }
  the output xml contains <?xml version="1.0" encoding="UTF-8"?> while I want to put it as <?xml version="1.0" encoding="ISO-8859-2"?>
CEHJCommented:
Have a go with this. If it doesn't work, try tweaking the encoding string:

transformer.setOutputProperty(javax.xml.transform.OutputKeys.ENCODING, "ISO8859-2");
vikas31Author Commented:
Still same output I mean in the resulting xml file <?xml version="1.0" encoding="UTF-8"?>  is coming not <?xml version="1.0" encoding="ISO-8859-2"?>
CEHJCommented:
You're setting it immediately after this i assume?:

>>transformer = TransformerFactory.newInstance().newTransformer(xslDomSource);
vikas31Author Commented:
yes

try {
         transformer = TransformerFactory.newInstance().newTransformer(xslDomSource);
            logger.debug("b4************************************transformer.setOutputProperty");
             transformer.setOutputProperty(javax.xml.transform.OutputKeys.ENCODING, "ISO8859-2");
      }
CEHJCommented:
Try iterating the properties before and after.
vikas31Author Commented:
sorry could not get you.
vikas31Author Commented:
During  xmlDoc = dBuilder.parse(fs); the xmlDoc contains <?xml version="1.0" encoding="UTF-8"?>   while in the fs it is <?xml version="1.0" encoding="ISO-8859-2"?> .

I think if I can change here then it may give the correct result but how I can change do not know.
CEHJCommented:
Presumably you're transforming using http://java.sun.com/j2se/1.4.1/docs/api/javax/xml/transform/Transformer.html#transform(javax.xml.transform.Source, javax.xml.transform.Result)

Try setting the encoding in the Result
vikas31Author Commented:
Thanks  a Lot for your responses.

Yes the code is

DOMResult result = new DOMResult();
               try {
                        logger.debug("xmlDomSource..."+xmlDomSource.toString());
                        logger.debug("result..."+result.toString());
                        try{
                           logger.debug("result.getNode()..."+getDocumentAsString((Document)result.getNode()));
                     }catch(Exception e)
                     {
                        logger.debug("Exception in result.getNode()...");
                     }
                  transformer.transform(xmlDomSource, result);
               }
but how I can set the encoding in the result. there is no method for this it seems.
CEHJCommented:
That's probably because you're using a DOMResult - until things are moved out of memory and into a renderable form, encoding is not really an issue. If you were to use a StreamResult, you could set the encoding.
vikas31Author Commented:
even in StreamResult there is no such method for changing the encoding
CEHJCommented:
BufferedWriter br = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileNameOut), "ISO8859_2")));
StreamResult out = new StreamResult(br);
vikas31Author Commented:
Yes this finally I am doing but my main point is that the output file should have the prolog of <?xml version="1.0" encoding="ISO-8859-2"?> not <?xml version="1.0" encoding="UTF-8"?> . This what you wrote will give the output charset as ISO... but not what I need in the prolog. My requirement is to further read the resulting xml & then do something but as the resulting xml have ISO.. char set & the prolog have UTF-8 so I get the exception.

CEHJCommented:
OK - got you. What about setting the prolog using the xsl?
vikas31Author Commented:
<?xml version="1.0" encoding="ISO-8859-2"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="ISO-8859-2" indent="yes"/>
      <xsl:template match="/">
vikas31Author Commented:
I am using the above prolog. Though the XSLT I have written have all english character's but still I used <?xml version="1.0" encoding="ISO-8859-2"?>

but no use since getting the exception.
CEHJCommented:
Is that not working then?
vikas31Author Commented:
yes it is not working..
CEHJCommented:
I'm not at all sure that the code you're using means 'insert the following attributes as the attributes of the prolog in the result'. It could mean 'this is how we are doing the transform'.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.