newbie: Open and close streams and files

S

steve

Hello,
I seem to run into problems (exception raised) when i try to close a Write
stream.
In a loop i open a whole bunch of files , read them and then create a file,
*different* for each one.
So i have two streams and one streamwriter and one streamreader.

After each loop, i close the stream and "read" file. When i try to do the
same for the "write" ones, i get into problems. It is only happy when i only
close the write file , *not* its stream as well.

Is this normal?

TIA for any help
-steve
 
O

One Handed Man \( OHM - Terry Burns \)

Make sure when you are writing that you 'Flush' the stream first, and then
close and close your file.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing
 
H

Herfried K. Wagner [MVP]

* "steve said:
I seem to run into problems (exception raised) when i try to close a Write
stream.
In a loop i open a whole bunch of files , read them and then create a file,
*different* for each one.
So i have two streams and one streamwriter and one streamreader.

After each loop, i close the stream and "read" file. When i try to do the
same for the "write" ones, i get into problems. It is only happy when i only
close the write file , *not* its stream as well.

Post your code.
 
S

steve

ok, here it is. i hope the comments are enough.
Notice the last lines :

'saveFileStream.Flush()
saveFileStream.Close()
'saveFile.Close()

if i uncomment them i get errors


thanx for the effort


'****** Loop in all the items in the listbox (files selected) and transform
them one by one *****
For Each inputFile In inputFilesArrayList
' after we select a file we proceed
'Create a file stream for reading from the file selected
Dim openFileStream As New FileStream(inputFile, FileMode.Open,
FileAccess.Read, FileShare.Read)
'Open a file associated with the opening stream and assign text encoding
Dim openFile As New StreamReader(openFileStream, System.Text.Encoding.ASCII)
'Create a file stream for writing to a file
'!!!!! NOTE: We keep the same path and we add a DLY extension !!!
Dim saveFileStream As New FileStream(inputFile + "DLY", FileMode.Create,
FileAccess.Write, FileShare.Write)
'open a file associated with the save Stream to write
Dim saveFile As New StreamWriter(saveFileStream, System.Text.Encoding.ASCII)
'***************************************************************************
*******
Try 'Put writing loop in a try-catch block
'---- load file contents in arraylist -------
Do
inputFileLine = openFile.ReadLine()
If Not inputFileLine Is Nothing Then
inputFileContentsArrayList.Add(inputFileLine)
End If
Loop Until inputFileLine Is Nothing
'---------------------------------------------
'------------- STEP 1 ------------------------
' -- READ data from arraylist
For Each LineStr In inputFileContentsArrayList
'Check each line length IF different than 233
If (LineStr.Length = 234 Or LineStr.Length = 233) Then
'extract Station ID , year , month , element code
tempLine.stationID = LineStr.Substring(0, 7)
tempLine.year = LineStr.Substring(7, 4)
tempLine.month = LineStr.Substring(11, 2)
tempLine.element = LineStr.Substring(13, 3)
'Loop in order to get data for the 31 days of the month on the same line
For i As Integer = 0 To 30
tempLine.value(i) = LineStr.Substring(16 + (i * 7), 7)
Next
'************************************************
'loop 31 times for every line in the array
For i As Integer = 0 To 30
saveFile.Write(tempLine.stationID)
saveFile.Write(delimiter + tempLine.year)
saveFile.Write(delimiter + tempLine.month)
saveFile.Write(delimiter + CStr(i + 1)) 'i+1 represents day
saveFile.Write(delimiter + tempLine.element)
saveFile.Write(delimiter + valid)
saveFile.Write(delimiter + tempLine.value(i))
saveFile.Write(vbCrLf)
Next
'************************************************
'IF file format is NOT correct, advise user and exit loop
Else
MsgBox("Le fichier n'as pas le propre format de 234 characters par ligne." +
vbCrLf + "SVP essayer de nouveau.")
Exit For
End If
Next
'*********************************************
Catch ex As Exception
MsgBox(ex.ToString)
Finally
'close preview file
openFileStream.Close()
openFile.Close()
'close save file
'saveFileStream.Flush()
saveFileStream.Close()
'saveFile.Close()
'empty file contents array
inputFileContentsArrayList.Clear()
'increase progress bar
pbrTransform.Value += 1
End Try
'***************************************************************************
*******
Next
'*************************** Main Loop for all files
***************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top