Results 1 to 10 of 10
- 04-29-2009, 07:33 PM #1
Member
- Join Date
- Sep 2008
- Location
- Ankara-TURKEY
- Posts
- 42
- Rep Power
- 0
- 04-29-2009, 08:13 PM #2
Member
- Join Date
- Sep 2008
- Posts
- 43
- Rep Power
- 0
<CODE>
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Write some data in binary.
*
* @author Ian F. Darwin, Ian Darwin's Site
* @version $Id: WriteBinary.java,v 1.3 2004/02/08 23:57:29 ian Exp $
*/
public class WriteBinary {
public static void main(String[] argv) throws IOException {
int i = 42;
double d = Math.PI;
String FILENAME = "binary.dat";
DataOutputStream os = new DataOutputStream(new FileOutputStream(
FILENAME));
os.writeInt(i);
os.writeDouble(d);
os.close();
System.out.println("Wrote " + i + ", " + d + " to file " + FILENAME);
}
</CODE>
U can start with that to write in, think I have another for reading.. hang on.. 2 secs
- 04-29-2009, 08:22 PM #3
Member
- Join Date
- Sep 2008
- Posts
- 43
- Rep Power
- 0
Sorry, this ones better.
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
class DataIODemo {
public static void main(String args[]) throws IOException {
FileOutputStream fout = new FileOutputStream("Test.dat");
DataOutputStream out = new DataOutputStream(fout);
out.writeDouble(98.6);
out.writeInt(1000);
out.writeBoolean(true);
out.close();
FileInputStream fin = new FileInputStream("Test.dat");
DataInputStream in = new DataInputStream(fin);
double d = in.readDouble();
int i = in.readInt();
boolean b = in.readBoolean();
System.out.println("Here are the values: " + d + " " + i + " " + b);
in.close();
}
}
All the best ;)
- 04-29-2009, 09:09 PM #4
Member
- Join Date
- Sep 2008
- Location
- Ankara-TURKEY
- Posts
- 42
- Rep Power
- 0
thank you. I add the code to my program. and there is a small problem. I try to write an integer array(with size 1000) to a binary file. I can not append all into file. It only writes last one:(
how can I append ?
kind regards.A stitch in time saves nine:D
- 04-29-2009, 09:29 PM #5
Member
- Join Date
- Sep 2008
- Posts
- 43
- Rep Power
- 0
post your code up here and let us see what you've done
- 04-29-2009, 09:49 PM #6
Member
- Join Date
- Sep 2008
- Location
- Ankara-TURKEY
- Posts
- 42
- Rep Power
- 0
I would like to write a kind of data compression. So here is a part of code...
Java Code:try{ FileInputStream fstream1 = new FileInputStream("c:\\Output\\scr.img"); DataInputStream in1 = new DataInputStream(fstream1); BufferedReader br1 = new BufferedReader(new InputStreamReader(in1)); String strLine1; while ((strLine1 = br1.readLine()) != null) { //hello if (z == 0) { try { BufferedWriter out = new BufferedWriter( new FileWriter("c:\\Test.dat", true)); out.write(linebyline_reserve[0] + "\r\n"); out.close(); } catch (IOException e1) { } } if (z == 1) { try { BufferedWriter out = new BufferedWriter(new FileWriter("c:\\Test.dat",true)); out.write(linebyline_reserve[1] + "\r\n"); out.close(); } catch (IOException e1) { } } //hello if (z != 0 && z != 1) { for (int d = 0; d < counter; d++) { if (strLine1.equals(copy_of_linebyline[d])) { FileOutputStream fout = new FileOutputStream("c:\\Test.dat"); DataOutputStream out = new DataOutputStream(fout); out.writeInt(map[d]); out.writeBoolean(true); out.close(); } } } z++; } //Close the input stream in1.close(); }catch (Exception e1){//Catch exception if any System.err.println("Error: " + e1.getMessage()); }
A stitch in time saves nine:D
- 04-29-2009, 10:57 PM #7
Member
- Join Date
- Sep 2008
- Location
- Ankara-TURKEY
- Posts
- 42
- Rep Power
- 0
I think problem is loop. I mean when I put a loop here
Java Code:import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; class DataIODemo { public static void main(String args[]) throws IOException { for(int i=0; i<20; i++){ FileOutputStream fout = new FileOutputStream("Test.dat"); DataOutputStream out = new DataOutputStream(fout); out.writeDouble(98.6); out.writeInt(1000); out.writeBoolean(true); out.close(); } } }
kind regards.A stitch in time saves nine:D
- 04-29-2009, 11:05 PM #8
move the close() method
Because your closing the DataOutputStreamit in the for loop... take the close() method out of the loop and place it after the loop.
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 04-30-2009, 07:26 PM #9
Member
- Join Date
- Sep 2008
- Location
- Ankara-TURKEY
- Posts
- 42
- Rep Power
- 0
Thank you it works. I would like to ask one more question. Could you tell me why it works when I call BufferedWriter in a loop and it does not work when I call DataOutputStream. I mean I call .close() with BufferedWriter in a loop and it works.But not with DataOutputStream.
footnot: could you post related links please(binary file IO).
kind regards.A stitch in time saves nine:D
- 05-01-2009, 12:28 AM #10
Actually, you bring up a good point (and I hadn't noticed it). The following:
Java Code:FileOutputStream fout = new FileOutputStream("Test.dat"); DataOutputStream out = new DataOutputStream(fout);
Java Code:[COLOR="Blue"]FileOutputStream fout = new FileOutputStream("Test.dat"); DataOutputStream out = new DataOutputStream(fout);[/COLOR] for(int i=0; i<20; i++){ out.writeDouble(98.6); out.writeInt(1000); out.writeBoolean(true); } [COLOR="blue"]out.close();[/COLOR]
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Similar Threads
-
Executable java to read and write to a txt file
By njoymirror in forum New To JavaReplies: 2Last Post: 03-21-2009, 01:19 AM -
How to read and write to a file without taking out the comments in the file
By MAGNUM in forum New To JavaReplies: 5Last Post: 02-05-2009, 10:28 AM -
How to read from and write to .properties file from a jsp
By MAGNUM in forum New To JavaReplies: 5Last Post: 01-20-2009, 09:08 AM -
Read and Write file
By mrdestroy in forum New To JavaReplies: 13Last Post: 10-31-2008, 12:11 PM -
File read/write problems
By p900128 in forum New To JavaReplies: 4Last Post: 06-27-2008, 12:15 AM
Bookmarks