2023.11.24 TIL(VB기본강의3)

SaGo_MunGcci·2023년 11월 24일
0

VB

목록 보기
3/8

Today do list

⦁ 파일 입출력



TIL

파일 출력1

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        '파싱할때 구분자로 나누어서 출력(문장을 편집하고 싶을때 사용)
        'FileIO.TextFieldParser클래스의 생성자 생성
        'Dim parsindTxt As FileIO.TextFieldParser
        'parsindTxt = New FileIO.TextFieldParser("C:\Users\user\Desktop\vbTest.txt")
        Dim parsindTxt As FileIO.TextFieldParser = New FileIO.TextFieldParser("C:\Users\user\Desktop\vbTest.txt")
        'Delimited: 파일을 읽어올때 한줄당 "특정문자"를 기준으로 문장을 구분할때 사용
        'FixedWidth : 파일을 읽어올때 한줄당 "문자갯수"만 읽어서 출력한다.
        'parsindTxt.TextFieldType = FileIO.FieldType.Delimited
        'parsindTxt.SetDelimiters("/")
        parsindTxt.TextFieldType = FileIO.FieldType.FixedWidth
        '한줄에 있는 처음 두글자, 그다음 3글자 -1은 나머지 모든 문장을 출력
        ' -1자체는 한줄에 있는 모든 문장 출력
        parsindTxt.SetFieldWidths(2, 3, -1)

        'EndOfData : 읽어올 텍스트파일의 끝
        While Not parsindTxt.EndOfData
            'Dim currentTxt As String()
            '문자열 배열을 선언합니다.
            '이 경우, currentTxt는 문자열의 배열을 나타내는 변수가 됩니다.
            '이 배열은 여러 문자열을 저장할 수 있는 구조체입니다.

            'Dim currentTxt As String
            '단일 문자열을 저장하는 변수를 선언합니다.
            '이 경우, currentTxt는 단일 문자열을 나타내는 변수가 됩니다.
            Dim currentTxtArr As String()
            'TextFieldParser는 기본적으로 한줄씩 읽어온다.
            currentTxtArr = parsindTxt.ReadFields()

            Dim currentTxt As String
            For Each currentTxt In currentTxtArr
                Label1.Text = currentTxt
                MsgBox(currentTxt,, "Title")
            Next

        End While



        'Dim sentence As String
        'PC 내부의 텍스트파일을 읽어서 스트링변수에 담음
        'sentence = My.Computer.FileSystem.ReadAllText("C:\Users\user\Desktop\vbTest.txt")
        'Label1.Text = sentence
        'MsgBox(sentence,, "Title")

    End Sub
End Class

파일 출력2


Public Class Form1

    Public Class Product
        Public pName As String
        Public pNum As String

        ' 생성자 정의
        ' 자바와 C#과는 달리 클래스를 생성해도 기본 생성자를 따로 만들어주지 않는다.
        ' 생성자 선언 코딩도 전혀 다르다.
        Public Sub New()

        End Sub

        Public Sub New(pName As String, pNum As String)
            Me.pName = pName
            Me.pNum = pNum
        End Sub


    End Class

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        '클래스 객체 생성하는 게 궁금해서 그냥 만들어봄
        'Dim product As Product = New Product("자동차", "123")

        Dim chosenFormat As Integer() = {3, 4, 3, 1}
        Dim hyundaiFormat As Integer() = {3, 4, 12, 1}

        Dim txtParser As FileIO.TextFieldParser = New FileIO.TextFieldParser("C:\Users\user\Desktop\vbtest2.txt")
        txtParser.TextFieldType = FileIO.FieldType.FixedWidth
        'txtParser.SetFieldWidths()

        'ReadFields()가 배열로 반환하기 때문이다.
        Dim lineData As String()

        While Not txtParser.EndOfData
            '첫글자가 조선, 현대인지에 따라서 문자수를 자라서 출력한다.
            '해당 파일의 첫글자가 조선인지 현대인지 알수 있는 PeekChars(숫자)함수를 사용한다.
            '해당 파일의 글자를 앞에서부터 지정한 숫자만큼 읽어 온다.
            Dim twoLetter As String = txtParser.PeekChars(2)

            'twoLetter.eqauls("조선")을
            'VB에서 String.Compare(twoLetter, "조선") 사용하는데
            '같다면 0 아니면 0이 아닌 숫자를 반환한다.
            If String.Compare(twoLetter, "조선") = 0 Then

                txtParser.SetFieldWidths(chosenFormat)

            ElseIf String.Compare(twoLetter, "현대") = 0 Then

                txtParser.SetFieldWidths(hyundaiFormat)

            End If
            '첫줄이 끝나면 다음줄에 커서를 가져다가 시작함 
            lineData = txtParser.ReadFields()

            Dim sentence As String = "시대: " & lineData(0) & vbCrLf &
                                     "이름: " & lineData(1) & vbCrLf &
                                     "연락처: " & lineData(2) & vbCrLf &
                                     "성별: " & lineData(3) & vbCrLf
            MsgBox(sentence)
        End While

        'Label1.Text = txtParser.ToString()

    End Sub
End Class


파일 출력3

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        'StreamReader: 한줄의 문장을 처음부터 끝까지 출력
        Dim txtReader As IO.StreamReader
        txtReader = My.Computer.FileSystem.OpenTextFileReader("C:\Users\user\Desktop\vbtest2.txt")

        While Not txtReader.EndOfStream

            MsgBox(txtReader.ReadLine())

        End While

        'StreamReader는 파일을 실행하고 계속 메모리에 올려놓기때문에 
        '만약 클로즈를 안하면 어플리케이션을 종료하기 전까지 해당 txt파일을 수정할수없다.
        'arrayList와 비슷
        txtReader.Close()


    End Sub
End Class


Retrospection



profile
이리저리 생각만 많은 사고뭉치입니다.

0개의 댓글