aspose file tools
The moose likes Java in General and the fly likes Writing to a file. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Writing to a file." Watch "Writing to a file." New topic
Author

Writing to a file.

Navtaj Singh
Ranch Hand

Joined: Jan 30, 2001
Posts: 37
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
Matts Smith
Ranch Hand

Joined: Feb 03, 2001
Posts: 113
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
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
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
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
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
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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"))));


"JavaRanch, where the deer and the Certified play" - David O'Meara
ryan burgdorfer
Ranch Hand

Joined: Jan 24, 2001
Posts: 219
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


<UL TYPE=SQUARE><I><LI>Ryan Burgdorfer<BR><LI>Java Acolyte</I></UL>
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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
Ahhh...learn something new everyday
And that's the way I like it
Navtaj Singh
Ranch Hand

Joined: Jan 30, 2001
Posts: 37
Thanks Guys,
I have got the answer.
Thanks Once again!!!
--Navi
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Writing to a file.
 
Similar Threads
Could you unzip a file with a java program
problem with writing file , please help
Urgent Problem Regarding I/O
how to insert carriage return
Reading a binary file from a client to server