Hi! Guys. Q)I trying to write Integer to a file .The "xxx.java" file compiles and runs but when i open the destination file,there is no sign of the value which i have just written. The source code is as follows:-
} catch(ArrayIndexOutOfBoundsException e) { System.out.println("You have to give me the name of a file to open."); System.exit(0); } catch (FileNotFoundException e) { System.out.println("Could not open input file "); System.exit(0); } catch(IOException e) { System.out.println("Error while opening input file"); System.exit(0); }
Close your output streams in the reverse order you opened them. this is a peculiar thing about output streams in java... it seems buffered data is not always flushed on program termination.
later
Roseanne Zhang
Ranch Hand
Joined: Nov 14, 2000
Posts: 1953
posted
0
It is NOT specific to Java, I remember either Pascal or C does the samething. It is a good habit when you open something, always close them in the right order It is definitely more important in xml now. Thanks! Roseanne Join our SCJD Study Group when certified
Roseanne Zhang
Ranch Hand
Joined: Nov 14, 2000
Posts: 1953
posted
0
How to keep trace all the open/close pairs? My suggestion is when you write open(), you immediately write close(), before your add ANYTHING in between. Even Parenthesis like () [] {} are included. I learned this technique since Pascal time, used it in Pascal, C/C++, Java, html, xml, ... you name it. In C++, when I write a class, immediately write a virtual destructor for it, saved a lot of mem-leak headache later.
Matts Smith
Ranch Hand
Joined: Feb 03, 2001
Posts: 113
posted
0
Roseanne I'm not quite sure about C not flushing it's buffers. You should test it... (I don't have a c compiler anymore) I always tought that java behaved that way because the outputstreams were not finalized at the end of the program. The buffer would then be lost. just a theory tough...
Roseanne Zhang
Ranch Hand
Joined: Nov 14, 2000
Posts: 1953
posted
0
It is probably Turbo Pascal, I remember Unix Pascal/ VMS Pascal / Turbo Pascal worked not all the same. Always close things you opened, no matter what... Roseanne
"JavaRanch, where the deer and the Certified play" - David O'Meara
ryan burgdorfer
Ranch Hand
Joined: Jan 24, 2001
Posts: 219
posted
0
Cindy, If he omits the "new file" part of his DataOutputStream, won't he have to create the file somewhere else (previously in the code)? Or are you saying that it will be created automatically if it does not exist? ------------------
Well, the original question was answered by others, so I was just adding a comment. With FileOutputStream a file will get created if it does not exist. File does not create a new file. However if you pass a file to a stream it DOES create the file, it just complicates the code and could be skipped. This is a good thread on FileOutputStream: http://www.javaranch.com/ubb/Forum24/HTML/001695.html
ryan burgdorfer
Ranch Hand
Joined: Jan 24, 2001
Posts: 219
posted
0
Ahhh...learn something new everyday And that's the way I like it
Navtaj Singh
Ranch Hand
Joined: Jan 30, 2001
Posts: 37
posted
0
Thanks Guys, I have got the answer. Thanks Once again!!! --Navi