Only 49 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

File Output stream adding extra character while writing  RSS feed

 
karan sarin
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Program objective is to make an Xml and save it in File System over a n/w. For that i am using the following lines of code :

public boolean xmlToFile(Document document, String pathAndNameWithExtension)
{
if(pathAndNameWithExtension == null)
pathAndNameWithExtension = "file///XMLOutput.xml";
try
{
File file = new File(pathAndNameWithExtension);
file.createNewFile();
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformar = tFactory.newTransformer();
DOMSource domSource = new DOMSource(document);
FileOutputStream fos = new FileOutputStream(file);
StreamResult result = new StreamResult(fos);
transformar.transform(domSource, result);
System.out.println("XML Document written to Path : " + file.getAbsolutePath());
return true;
}
catch(TransformerConfigurationException tce)
{
System.out.println("[ XmlHelper / xmlToFile() ] Exception : " + tce + "\n" + tce.getStackTrace());
return false;
}
catch(Exception te)
{
System.out.println("[ XmlHelper / xmlToFile() ] Exception : " + te + "\n" + te.getStackTrace());
}
return false;
}

Now the Problem is that sometimes [Randomly] some extra characters are added in an Xml and it becomes a malformed Xml. From Logs the conclusion is Xml is Prepared correctly and no exception is thrown but while saving over a network it becomes mal-formed Xml.
is It becaues of worng use of output stream in the above code.
Plz Help
regards
Karan Sarin
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only thing that jumps out at me quickly is you have no close() on the output stream. See if that helps.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the conclusion is Xml is Prepared correctly and no exception is thrown but while saving over a network it becomes mal-formed Xml.
Okay, so the code you posted here works correctly but some other code that you didn't post, the code that saves the XML over the network, is damaging the XML. If it were me I would be looking at that other code.
 
karan sarin
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
BY n/w saving i mean dir have been mounted to other machine. so no extra effort is req for saving the Xml File. Its more or less a local saving of Xml for me.

Lets c wheather output stream flush and closing helps in solving the problem..
Karan Sarin
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose if you have a Windows machine writing to a Unix share or vice versa you could be getting the wrong newline characters, too. Are they all the same OS? Have you visually inspected the file that's written so you can see what's wrong with it?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!