?
Solved

write 10240 "0"

Posted on 2007-10-12
7
Medium Priority
?
241 Views
Last Modified: 2010-04-23
Hello

I want to write 10KB of "0" into a file.

What's the fastest way of doing it?

Thanks

Sven
0
Comment
Question by:Ruttensoft
7 Comments
 
LVL 5

Expert Comment

by:fmonroy
ID: 20067355
0 as char or 0 as value?
value: use BinaryWriter
char: use TextWriter
0
 

Author Comment

by:Ruttensoft
ID: 20067383
Hi

Yes, sorry that was not what I intended to ask.

I want to know the fastest way to write 10240 times an 0 into a streamwriter

dim a as string = ""
for i as integer = 1 to 10240
a = a & "0"
next

that is veeeeeeeery slow. What's a better way?
0
 
LVL 48

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 2000 total points
ID: 20067411
Hi Ruttensoft,

Insteadstead of looping from 1 to 10240, use something like this....

        Dim sw As New System.IO.StreamWriter("C:\Test.txt")
        sw.Write(New String("0"c, 10240))
        sw.Close()

Regards,

Wayne
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 5

Expert Comment

by:fmonroy
ID: 20067423
yes, because you are concatenating the string before writing to the stream; do it directly without concatenating:

for i as integer = 1 to 10240
YourStream.Write("0");
next
0
 

Author Comment

by:Ruttensoft
ID: 20067573
Bingo Wayne, thanks!
0
 
LVL 23

Expert Comment

by:Christopher Kile
ID: 20067675
This is copied directly from "Reading and Writing to a Newly Created Data File" in MSDN, with the single adaptation of writing 10240 "0" to the file instead of ten integers (also with r.close() substituting for the second, incorrect w.close()); I suggest it because this is Microsoft's recommendation for writing a newly created data file, and may in fact be slightly faster than the other methods:

Option Explicit On
Option Strict On
Imports System
Imports System.IO
Class MyStream
    Private Const FILE_NAME As String = "Test.data"
    Public Shared Sub Main()
        ' Create the new, empty data file.
        If File.Exists(FILE_NAME) Then
            Console.WriteLine("{0} already exists!", FILE_NAME)
            Return
        End If
        Dim fs As New FileStream(FILE_NAME, FileMode.CreateNew)
        ' Create the writer for data.
        Dim w As New BinaryWriter(fs)
        ' Write data to Test.data.
        Dim i As Integer
        For i = 0 To 10240
            w.Write("0")
        Next i
        w.Close()
        fs.Close()
        ' Create the reader for data.
        fs = New FileStream(FILE_NAME, FileMode.Open, FileAccess.Read)
        Dim r As New BinaryReader(fs)
        ' Read data from Test.data.
        Console.WriteLine(r.ReadChars(10240)
        r.Close()
    End Sub
End Class
0
 

Author Comment

by:Ruttensoft
ID: 20067699
Thanks, the new string()- way of wayne is fast enough (64ms on a 200Mhz-Mobile device)

Thanks
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Integration Management Part 2
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question