2016-07-23 7 views
-1

고정 파일을 거쳐 | 필요한 경우 프로그램이 올바르게 작동하고 콘솔에 올바르게 표시됩니다. 문제는 콘솔에서 파일의 줄에 줄을 쓸 수 없다는 것입니다. 모든 시도는 빈 파일로 끝나거나 개별 줄로 작성된 줄의 각 문자열로 끝납니다. 아래 코드는 파일에 출력을 써야하지만 파일은 비어있는 파일을 보여줍니다..NET Console.WriteLine() Console.SetOut

Imports System.IO 
Module Module1 
    Sub Main() 

     Dim stdFormat As Integer() = {3, 13, 11, 5, 2, 2, 13, 14, 30, 15, 76, 80, 95, 100, 50, 2, 10, 30} 

     Using MyReader As New FileIO.TextFieldParser("SOURCE.txt") 
      MyReader.TextFieldType = FileIO.FieldType.FixedWidth 
      MyReader.FieldWidths = stdFormat 

      Dim currentRow As String() 
      While Not MyReader.EndOfData 

       Try 

        Dim rowType = MyReader.PeekChars(3) 

        If String.Compare(rowType, "Err") = 0 Then 


        Else 

         MyReader.SetFieldWidths(stdFormat) 

        End If 

        currentRow = MyReader.ReadFields 

        For Each newString In currentRow 


         Console.Write(newString & "|") 


        Next 


        Dim file = New FileStream("test.txt", FileMode.Append) 

        Dim standardOutput = Console.Out 

        Using writer = New StreamWriter(file) 

         Console.SetOut(writer) 

         Console.WriteLine() 

         Console.SetOut(standardOutput) 

        End Using 


       Catch ex As FileIO.MalformedLineException 



       End Try 

      End While 


     End Using 

     Console.ReadLine() 


    End Sub 

End Module 

답변

0

당신은 다음 스트림을 표준을 재설정 (writer로 리디렉션) 표준 출력에 하나의 줄 바꿈을 작성, writer에 스트림을 표준을 설정하고 있습니다.

당신이해야 할 일은 파일에 쓰는 것입니다. 스트림을 리디렉션하지 마세요. 우리가 콘솔과 파일 쓰기를 결합한다면, 우리는 이것을 좀 더 깔끔하게 만들 수 있습니다.

Using writer = New StreamWriter(file) 
    String newStr = "" 
    For Each columnStr In currentRow 
     newStr = columnStr & "|" 
     writer.WriteLine(newStr) 
     // If you don't want the console output, just remove the next line 
     Console.WriteLine(newStr) 
    Next 
End Using