반응형

텍스트 파일을 읽고 내용을 출력해 보자.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class TextReader : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        string path = Application.dataPath;    // D:/UnityTest/My project/Assets
        path += "/Text/Text.txt";    // D:/UnityTest/My project/Assets/Text/Text.txt
 
        /* Text 파일 내용
        First text line.
        Second text line.
        Third text line.
        Fourth text line.
        Fifth text line.
        1,2,3,4,5
        6,7,8,9,10
        */
 
        string[] contents = System.IO.File.ReadAllLines(path);
        if (contents.Length > 0)
        {
            for (int i=0; i<contents.Length; i++)
            {
                if (i < 5)
                {
                    Debug.Log(contents[i]);
                }
                else // 숫자 텍스트
                {
                    string[] txtArr = contents[i].Split(',');
                    int[] numArr = new int[txtArr.Length];
                    int total = 0;
 
                    for (int j=0; j<txtArr.Length; j++)
                    {
                        numArr[j] = int.Parse(txtArr[j]);
                        total += numArr[j];
                    }
 
                    Debug.Log(total);
                }
            }
        }
 
        string content = System.IO.File.ReadAllText(path);
        Debug.Log(content);
    }
}
 

 

 

반응형
Posted by J-sean
: