Results 1 to 13 of 13
  1. #1
    PeraPisar is offline Member
    Join Date
    Sep 2009
    Posts
    6
    Rep Power
    0

    Post Writing to file problem

    Hello,

    I developed a java desktop application which is writing data into a few xml and one txt file. Everything works fine, but occasionally (once a week in average) an error occurs. In the middle of work, program starts writing strange symbols in the file. In EditPlus they appear like empty squares, but in NotePad there is nothing.

    I am working with jdk1.6.0_06.

    Has anyone had similar situation?

    Here is part of the code for writing to xml file. The parameter values are hardcoded here.


    Java Code:
    private Document currentDocument;
    private DocumentBuilderFactory docBuilderFactory;
    private DocumentBuilder docBuilder;
    
    private File currentFile;
    private FileManipulator fileManipulator; // FileManipulator is a class which is managing files that are being written
    private String currentDirectory;
    
    public void appendElementToCurrentDocument() throws Exception
    {
    	openCurrentDocument();
    	appendData();
    	closeCurrentDocument();
    }
    
    protected void openCurrentDocument() throws Exception
    {
    	currentFile = fileManipulator.getCurrentFile();    
    	if(currentFile.length()!=0)
    	{
    		try{			
    			currentDocument = docBuilder.parse(currentFile);
    		}
    		catch(Exception ex)
    		{
    			throw new Exception(ex.getMessage());
    		}
    	}
    	else
    	{
    		currentDocument = docBuilder.newDocument();
    		Element elements = currentDocument.createElement("DataEntries");
    		currentDocument.appendChild(elements);
    	}
    
    }
    protected void appendData() throws Exception{
    	try
    	{
    		Element dataEntry = currentDocument.createElement("DataEntry");
    		dataEntry.setAttribute("Number","1");
    		dataEntry.setAttribute("Text","Some text");
    		currentDocument.getFirstChild().appendChild(dataEntry);
    	}
    	catch (Exception ex)
    	{
    		throw new Exception(ex.getMessage());
    	}
    }
    protected void closeCurrentDocument() throws Exception{
    	
    	try
    	{
    		DOMSource source = new DOMSource(currentDocument);
    		FileOutputStream  fos = new FileOutputStream(currentFile);
    		StreamResult result = new StreamResult(fos);
    		TransformerFactory  tf = TransformerFactory.newInstance();
    		Transformer transformer = tf.newTransformer();
    		transformer.transform(source,result);
    		fos.close();
    	}
    	catch (Exception  ex)
    	{
    		throw new Exception(ex.getMessage());
    	}
    	
    }
    Thank you

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default

    By "error" do you mean it's throwing an exception, or that the output has these strange characters in it? If it's the latter then I suspect whatever input to the transformation you're using has a character that the format of the output doesn't recognise.

    Have you looked at the input going into this?

  3. #3
    PeraPisar is offline Member
    Join Date
    Sep 2009
    Posts
    6
    Rep Power
    0

    Post

    Thanks for the reply.

    It does not throw an exception. Only appends those characters to xml.

    I also suspected that it was the wrong input data format so I developed a method that checks if there are any non alphanumeric characters prior to insertion.

    The same data is written simultaneously to two files, and (what is the strangest of all) in one file this error occurred and in second it does not.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default

    Quote Originally Posted by PeraPisar View Post
    Thanks for the reply.

    It does not throw an exception. Only appends those characters to xml.

    I also suspected that it was the wrong input data format so I developed a method that checks if there are any non alphanumeric characters prior to insertion.

    The same data is written simultaneously to two files, and (what is the strangest of all) in one file this error occurred and in second it does not.
    How do you determine the contents of the output files?
    This really smacks of a charset problem.

  5. #5
    PeraPisar is offline Member
    Join Date
    Sep 2009
    Posts
    6
    Rep Power
    0

    Post

    I am not sure that understand your question correctly.

    Program generates the output data. It is like an insert to a database, except it is a xml file. I mean that it does not depend on some entries from users or other format unreliable data. To be precise, it uses data from smart cards and records the action which smart card user took.

  6. #6
    devunion is offline Member
    Join Date
    Jun 2008
    Posts
    56
    Rep Power
    0

    Default

    Add fos.flush(); before fos.close();
    Try Controls4J - Advanced Swing Components.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default

    Quote Originally Posted by PeraPisar View Post
    I am not sure that understand your question correctly.

    Program generates the output data. It is like an insert to a database, except it is a xml file. I mean that it does not depend on some entries from users or other format unreliable data. To be precise, it uses data from smart cards and records the action which smart card user took.
    OK, are you logging the data that's coming in?
    That way you can see what you're getting.

    Is it exactly the same code that produces the incorrect file as produces the correct file?

  8. #8
    PeraPisar is offline Member
    Join Date
    Sep 2009
    Posts
    6
    Rep Power
    0

    Default

    Thank you devunion, I will try it. The problem is that it is uncertain when the bug will appear. So I have to wait.

    Tolls, yes, I have a log file and it shows data that produced the error correctly, like nothing happened. This is for me the strangest things of all. Only later, the log file was also corrupted with same square symbols.

    I am wondering if it has something to do with the memory leaking or something? But in java, it should not be the problem?

  9. #9
    devunion is offline Member
    Join Date
    Jun 2008
    Posts
    56
    Rep Power
    0

    Default

    Yes, you can have some problems with memory leaks in Java. There is one well-known problem with substring() method. you always need to use new String(str.substring(start)). Another problem is related to links. Take a look at the last string in

    public synchronized void removeElementAt(int index) method in Vector class.
    Try Controls4J - Advanced Swing Components.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default

    Quote Originally Posted by devunion View Post
    Yes, you can have some problems with memory leaks in Java. There is one well-known problem with substring() method. you always need to use new String(str.substring(start)).
    That's not a memory leak. And you shouldn't always do new String. That should only be done in certain situations where you know you are retaining the substringed item. Most times it is not necessary.

    Quote Originally Posted by devunion View Post
    Another problem is related to links. Take a look at the last string in

    public synchronized void removeElementAt(int index) method in Vector class.
    What about that method is a cause for a memory leak?

    In any case, there is nothing about this problem that a memory leak in Java would be the cause. All the leak would do is result in an OutOfMemory exception, not corruption of the data.

    To the OP. Are you sure the corruption in the logfile does not happen at the same time as that in the output files?

  11. #11
    PeraPisar is offline Member
    Join Date
    Sep 2009
    Posts
    6
    Rep Power
    0

    Default

    Corruption doesn't always occur in the same order. For example, last time, one of the xml files became corrupted, while writing to the second xml file (writing of the exact same data) and to the log file (which is a txt file) continued without problem for another 45 minutes. After that time both second xml and log file became corrupted (at the same moment).

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default

    Quote Originally Posted by PeraPisar View Post
    Corruption doesn't always occur in the same order. For example, last time, one of the xml files became corrupted, while writing to the second xml file (writing of the exact same data) and to the log file (which is a txt file) continued without problem for another 45 minutes. After that time both second xml and log file became corrupted (at the same moment).
    Oh...ah.
    Well, this is looking to me like something that's not really debuggable remotely. If it's inconsistent like this, then you'd need to be debugging, or somehow monitoring it closely to see what's going on. Not something I can help with, I'm afraid.

  13. #13
    PeraPisar is offline Member
    Join Date
    Sep 2009
    Posts
    6
    Rep Power
    0

    Post

    Quote Originally Posted by Tolls View Post
    Oh...ah.
    Well, this is looking to me like something that's not really debuggable remotely. If it's inconsistent like this, then you'd need to be debugging, or somehow monitoring it closely to see what's going on. Not something I can help with, I'm afraid.

    Thanks anyway for the effort.

Similar Threads

  1. Writing to a .txt File
    By ngc0202 in forum New To Java
    Replies: 7
    Last Post: 07-26-2009, 03:18 AM
  2. Replies: 3
    Last Post: 05-09-2009, 11:31 PM
  3. Problem while writing to file, size greater than 15MB ..?
    By prabhurangan in forum New To Java
    Replies: 3
    Last Post: 12-30-2008, 05:28 PM
  4. Problem while writing xml file
    By Rajesh Sahu in forum Java Servlet
    Replies: 0
    Last Post: 12-23-2008, 04:53 PM
  5. Replies: 9
    Last Post: 07-10-2008, 04:52 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •