.net - Why does my WriteLine loop terminate prematurely? -
i have function read file, , write file. file trying read , write numbers 1 3000 in order. read file fine , store in variable called numbers
. reason, when write file, writes first 2913 lines correctly, end of output:
2908 2909 2910 2911 2912 2913 2
the file ends in middle of number 2914 every time run program. program not throw exception. why behavior occur?
here code:
sub main() integer dim numbers new list(of string) readfile(numbers, "c:\test.txt") writefile(numbers, "c:\test2.txt") end function private sub readfile(byref lines list(of string), _ byval filepath string) dim sr new system.io.streamreader(filepath) while sr.peek <> -1 lines.add(sr.readline) loop end sub private sub writefile(byval lines list(of string), _ byval filepath string) dim sw new system.io.streamwriter(filepath, false) each line in lines sw.writeline(line) next end sub
you can avoid using
block:
using sw new system.io.streamwriter(filepath, false) each line in lines sw.writeline(line) next end using
if habit of typing "using" instead of "dim", forces think "end using" portion automatically closes , disposes of stream.
Comments
Post a Comment