Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.42!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!198.186.194.247.MISMATCH!news-out.readnews.com!transit3.readnews.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: Writing to file Date: Wed, 30 Nov 2011 14:20:39 -0800 (PST) Organization: http://groups.google.com Lines: 32 Message-ID: <16014809.23.1322691639630.JavaMail.geo-discussion-forums@prgi20> References: Reply-To: comp.lang.java.programmer@googlegroups.com NNTP-Posting-Host: 2620:0:1000:fd2b:224:d7ff:fe69:5838 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: posting.google.com 1322691639 3772 127.0.0.1 (30 Nov 2011 22:20:39 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 30 Nov 2011 22:20:39 +0000 (UTC) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=2620:0:1000:fd2b:224:d7ff:fe69:5838; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T User-Agent: G2/1.0 X-Google-Web-Client: true Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:10383 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? 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