Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Writing to a file.

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:-

import java.io.*;
import java.util.*;
class ReadRawData {
public static void main (String args[]) {
int abc=1;
DataInputStream data=null;
try {
DataOutputStream bof=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(new File("c:/apar/zee.txt"))));
bof.write(abc);
System.out.println("The Data Has Been Transferred");

}
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);
}

catch (Exception e) {
System.out.println("Unexpected exception: " + e);
System.exit(0);
}


} // end main

} // end ReadRawData

--- Navi
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try getting rid of the File part of your statement.
Change
DataOutputStream bof=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(new File("c:/apar/zee.txt"))));
To:
DataOutputStream bof=new DataOutputStream(new BufferedOutputStream(new FileOutputStream("c:/apar/zee.txt"))));
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
------------------
  • Ryan Burgdorfer
  • Java Acolyte in
  • Columbus, OH USA
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahhh...learn something new everyday
And that's the way I like it
 
Navtaj Singh
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Guys,
I have got the answer.
Thanks Once again!!!
--Navi
 
reply
    Bookmark Topic Watch Topic
  • New Topic