Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Writing to file

Reply
Thread Tools

Writing to file

 
 
sara
Guest
Posts: n/a
 
      11-30-2011
Hi All,

I have a program which generates many lines of data where each line
includes an integer and two floats. I want to know the fastest way to
write these generated lines to a file. Currently, I am writing to a
binary file as follows:
FileOutputStream fos = new FileOutputStream(fileName);
DataOutputStream dos = new DataOutputStream(fos);
for (n : N) {
dos.writeInt(n.id);
dos.writeFloat(x.floatValue());
dos.writeFloat(y.floatValue());
}

However it seems that this approach is very slow. Can I use any kind
of buffering technique to speed up writing to binary file?

Best
Sara
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      11-30-2011
On Wednesday, November 30, 2011 11:31:33 AM UTC-8, sara wrote:
> Hi All,
>
> I have a program which generates many lines of data where each line
> includes an integer and two floats. I want to know the fastest way to
> write these generated lines to a file. Currently, I am writing to a
> binary file as follows:
> FileOutputStream fos = new FileOutputStream(fileName);
> DataOutputStream dos = new DataOutputStream(fos);
> for (n : N) {
> dos.writeInt(n.id);
> dos.writeFloat(x.floatValue());
> dos.writeFloat(y.floatValue());
> }
>
> However it seems that this approach is very slow. Can I use any kind
> of buffering technique to speed up writing to binary file?


Have you considered reading the API docs?
<http://docs.oracle.com/javase/7/docs/api/java/io/BufferedOutputStream.html>

Note that you will need to build the DataOutputStream on top of the BufferedOutputStream rather than the other way around.

As to whether this helps performance, what measurements have you done, and how do you know that the result is "very slow"? Compared to what?

What were the conditions of the measurements (other load on the system, hard drive configuration, etc.)?

--
Lew

--
Lew
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      12-02-2011
On Wed, 30 Nov 2011 11:31:33 -0800 (PST), sara <>
wrote, quoted or indirectly quoted someone who said :

>DataOutputStream dos = new DataOutputStream(fos);
>for (n : N) {
>dos.writeInt(n.id);
>dos.writeFloat(x.floatValue());


See http://mindprod.com/applet/fileio.html

It will generate you code to do this. Make sure you ask for buffered
if you want speed.

Get rid of Float objects and just use pure float primitives if you
want speed.

There is not much overhead, just flipping the little endian Intel uses
internally to big endian.

--
Roedy Green Canadian Mind Products
http://mindprod.com
For me, the appeal of computer programming is that
even though I am quite a klutz,
I can still produce something, in a sense
perfect, because the computer gives me as many
chances as I please to get it right.

 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      12-03-2011
On 11/30/2011 2:31 PM, sara wrote:
> I have a program which generates many lines of data where each line
> includes an integer and two floats. I want to know the fastest way to
> write these generated lines to a file. Currently, I am writing to a
> binary file as follows:
> FileOutputStream fos = new FileOutputStream(fileName);
> DataOutputStream dos = new DataOutputStream(fos);
> for (n : N) {
> dos.writeInt(n.id);
> dos.writeFloat(x.floatValue());
> dos.writeFloat(y.floatValue());
> }
>
> However it seems that this approach is very slow. Can I use any kind
> of buffering technique to speed up writing to binary file?


You can use a BufferedOutputStream between the DataOutputStream
and the FileOutputStream.

But I am a bit skeptical about the choice of format for
persisting data.

Arne

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Any problems with writing the information into a file - Multi-users perform writing the same file at the same time ???? HNguyen ASP .Net 4 12-21-2004 01:53 PM
Question: Writing text file based TestBenches vs. Waveform file based simulation. BLF VHDL 4 08-07-2004 12:44 AM
File Access error - writing to .txt file John Carnahan ASP .Net 2 07-18-2003 10:35 PM
A failure occurred writing to the resources file. Access is denied. -- RESX file is locked? -- WHY? Mark Kamoski ASP .Net 1 07-04-2003 12:02 PM
A failure occurred writing to the resources file. Access is denied. -- RESX file is locked? -- WHY? Mark Kamoski ASP .Net 1 07-04-2003 12:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57