This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

why javaw file io dose not work properly  RSS feed

 
mohsen asghari
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have a problem with file io stream
I want to edit an Xml file
At first I input file and I save it in a String;
I replace some sub strings and then I tried to store this string to another xml file
When I test this program and run it in Netbeans everything is right
But when I create jar file and run program by jar file
The program doesn’t work properly
It create new edited xml file but this file has some errors when I run it
My first xml file is a ms word xml that comes from a doc file
Edited xml size in 2 running way isn’t equal
Please help me.

I am sorry for my bad English.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

I see a few (potential) problems:

Available doesn't do what you think it does.

Even though you request all bytes to be read in one go, it's possible that only a few bytes are read. You need to read the bytes in a loop.

You may get an encoding problem. It's better to use a FileReader or even better, an InputStreamReader with a specified encoding wrapped around the FileInputStream:


You ignore the actual contents of the file in favour of the value of some variable called "temp".
 
mohsen asghari
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot
I changed my program to this

it work but slow
why I use replaceAll(), edited Xml will corrupt?
 
Greg Charles
Sheriff
Posts: 3014
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can speed it up a bit by wrapping the FileInputStream and FileOutputStream in a BufferedInputStream and BufferedOutputStream. Disk I/O is slow so you want to read or write data chunks at a time rather than byte by byte. You should also be closing your input stream, and closing the output stream in a finally block, so if there's some exception, your streams will still get closed.

I'm not sure what's going on with replaceAll(). Maybe the text you're replacing with has bad characters or is null?
 
mohsen asghari
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It isn't null
I think my text's character set is wrong
I test ISO-8859-1 and my output xml become ok but my replaced text shows by "?".
do you know what is the default character set for Xml?
and how can I find character set of a text?

I'm sorry my English is so bad.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no default -- the XML document should specify this itself, in the first line.
 
mohsen asghari
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!