Results 1 to 13 of 13
Thread: Writing to file problem
- 09-11-2009, 01:16 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 6
- Rep Power
- 0
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.
Thank youJava 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()); } }
- 09-11-2009, 01:31 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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?
- 09-11-2009, 01:54 PM #3
Member
- Join Date
- Sep 2009
- Posts
- 6
- Rep Power
- 0
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.
- 09-11-2009, 02:33 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
- 09-11-2009, 03:41 PM #5
Member
- Join Date
- Sep 2009
- Posts
- 6
- Rep Power
- 0
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.
- 09-11-2009, 10:06 PM #6
Member
- Join Date
- Jun 2008
- Posts
- 56
- Rep Power
- 0
- 09-14-2009, 12:11 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
- 09-15-2009, 10:03 AM #8
Member
- Join Date
- Sep 2009
- Posts
- 6
- Rep Power
- 0
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?
- 09-15-2009, 10:25 AM #9
Member
- Join Date
- Jun 2008
- Posts
- 56
- Rep Power
- 0
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.
- 09-15-2009, 11:33 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
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?
- 09-15-2009, 12:46 PM #11
Member
- Join Date
- Sep 2009
- Posts
- 6
- Rep Power
- 0
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).
- 09-15-2009, 04:03 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
- 09-16-2009, 11:00 AM #13
Member
- Join Date
- Sep 2009
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
Writing to a .txt File
By ngc0202 in forum New To JavaReplies: 7Last Post: 07-26-2009, 03:18 AM -
[SOLVED] how to reading binary file and writing txt file
By tOpach in forum New To JavaReplies: 3Last Post: 05-09-2009, 11:31 PM -
Problem while writing to file, size greater than 15MB ..?
By prabhurangan in forum New To JavaReplies: 3Last Post: 12-30-2008, 05:28 PM -
Problem while writing xml file
By Rajesh Sahu in forum Java ServletReplies: 0Last Post: 12-23-2008, 04:53 PM -
swapping the contents of the file and writing to another file
By Ms.Ranjan in forum New To JavaReplies: 9Last Post: 07-10-2008, 04:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks