none
StreamWriter文字檔處理的問題 RRS feed

  • 問題

  • 開發工具:Visual Basic 2005

    現在我在寫入文字檔的時候遇到一個問題,程式碼如下

    Dim fs As FileStream
    fs = New FileStream(_Path & "\" & GenFileName(_Pre, IOE), IO.FileMode.CreateNew, FileAccess.Write)
    Dim sw As New StreamWriter(fs, System.Text.Encoding.Default)
    Dim tmpErrMsgE As String = ""
    For Each tmpErrMsg As String In _ErrList
         tmpErrMsgE = tmpErrMsg
         If _UseEn Then
              tmpErrMsgE = EncryptString(tmpErrMsgE)
         End If
         sw.WriteLine(tmpErrMsgE)
    Next
    sw.Close

    這個程式目的是把要顯示的系統錯誤訊息編碼以後存到一個檔案裡面,等檔名用GenFileName來產生(根據系統日期來產生)

    然後現在我寫出來的文字檔案,用記事本開啟後,在每一行的最後都會有三個黑色的長方形 (像這樣 ▊)

    如果用其他的編輯器開如UltraEditor的話他則換行會出問題,換行會變成一次換兩行

    為什麼為變成這樣呢?

    謝謝!

    2007年1月9日 上午 11:30

解答

所有回覆

  • 應該和你的寫入內容有關吧。
    也許你寫入的字串,本身就已經有換行字元,但你用 WriteLine 輸出,又多了一組換行字元。

    2007年1月9日 上午 11:41
    版主
  • 以下是我有關檔案存取的程式碼:

        Private _ErrList As New StringCollection

        Private Function GenFileName(Optional ByVal strPre As String = "", Optional ByVal strLast As String = "") As String
            Try
                Dim str As String = Now.ToString("yyyyMMdd-hhmmss")
                Return strPre & str & strLast & ".txt"
            Catch ex As Exception
                Return ""
            End Try
        End Function

        Public Sub AddOneLine(ByVal strLine As String)
            Try
                If strLine.IndexOf(Chr(13)) <> -1 Then
                    Throw New Exception("Not only a line")
                End If
                _ErrList.Add(strLine)
            Catch ex As Exception
                Debug.Print("ErrLogWriter:CheckMsg:" & ex.Message)
            End Try
        End Sub

        Public Sub AddMulLine(ByVal strMul As String)
            Try
                Dim x As String()
                x = strMul.Split(Chr(13))
                For Each str As String In x
                    _ErrList.Add(str)
                Next
            Catch ex As Exception
                Debug.Print("ErrLogWriter:AddMulLine:" & ex.Message)
            End Try
        End Sub

        Private Function CheckMsg() As Boolean
            Try
                For Each tmpStr As String In _ErrList
                    If tmpStr.IndexOf(Chr(13)) <> -1 Then
                        Return False
                    End If
                Next
                Return True
            Catch ex As Exception
                Debug.Print("ErrLogWriter:CheckMsg:" & ex.Message)
                Return False
            End Try
        End Function

        Public Sub WriteFile()
            If Not CheckMsg() Then
                Exit Sub
            End If
            Dim IOE As String = ""
            Dim ReDo As Boolean = True
            While ReDo
                Dim fs As FileStream
                Try
                    ReDo = False
                    fs = New FileStream(_Path & "\" & GenFileName(_Pre, IOE), IO.FileMode.CreateNew, FileAccess.Write)
                    Dim sw As New StreamWriter(fs, System.Text.Encoding.Default)
                    Dim tmpErrMsgE As String = ""
                    For Each tmpErrMsg As String In _ErrList
                        tmpErrMsgE = tmpErrMsg
                        If _UseEn Then
                            tmpErrMsgE = EncryptString(tmpErrMsgE)
                        End If
                        sw.WriteLine(tmpErrMsgE)
                    Next
                    sw.Close()
                    fs.Close()
                    sw.Dispose()
                    fs.Dispose()
                    sw = Nothing
                    Me.Clear()
                    IOE = ""
                    Debug.Print("WriteFile Completed!")
               Catcb ex As System.IO.IOException
                    IOE="n"
                    ReDo=True           
                Catch ex As Exception
                    Debug.Print("ErrLogWriter:WriteFile:" & ex.Message)
                Finally
                    Try
                        fs = Nothing
                    Catch ex As Exception
                    End Try
                End Try
            End While

        End Sub

        Public Sub Clear()
            _ErrList.Clear()
        End Sub

    我有寫一個CheckMsg來判斷是否每一個字串都是沒有換行字元的

    是以StringCollection來存要存入檔案的字串

    EncryptString則是用DES來加密

    可是結果還是一樣耶...怎麼會這樣?還是有黑色長方形(三個)

    2007年1月9日 下午 12:52
  • 我發現問題了

    我現在改寫程式:

    Public Sub AddMulLine(ByVal strMul As String)
            Try
                '            Dim x As String()
                '            x = strMul.Split(vbCrLf)
                '            For Each str As String In x
                '                _ErrList.Add(str)
                '            Next
                Using sr As New StringReader(strMul)
                    While sr.Peek <> -1
                        _ErrList.Add(sr.ReadLine)
                    End While
                End Using
            Catch ex As Exception
                Debug.Print("ErrLogWriter:AddMulLine:" & ex.Message)
            End Try
        End Sub

    這樣的話就對了@@?

    因為我strMul的來源是Textbox.Text

    好像它的換行不只是vbCrLf或Chr(13)耶?那TextBox或純文字檔他換行到底是什麼東西組成呢?

    為什麼我寫程式的時候用StringReader會是對的,可是如果讀取的時候用vbCrLf來做Split的時候就會不對呢?

    好奇怪@@?

    2007年1月9日 下午 01:06
  • 2007年1月10日 上午 01:29
    版主