'텍스트파일입출력'에 해당되는 글 1건

  1. 2015.06.17 [Unity] 텍스트 파일입출력


text파일에 string 데이터 쓰고 읽기


1. 파일을 저장할때는 쉼표단위로 저장하였다.

 예) 1,sword,attack

2. 읽을때는 한줄읽어 쉼표로 구분된 데이터를 추출한다.

 - values 배열에 쉼표로 구분된 데이터가 저장된다.


using UnityEngine;

using System.Collections;

using System.IO;


public class Parser  : MonoBehaviour{


    void Start () {

        

    }

// Use this for initialization

    string m_strPath = "Assets/Resources/";


    public void WriteData(string strData)

    {


        FileStream  f = new FileStream( m_strPath  + Data.txt", FileMode.Append, FileAccess.Write);

        StreamWriter writer = new StreamWriter(f, System.Text.Encoding.Unicode);

        writer.WriteLine(strData);

        writer.Close();

    }


    public void Parse()

    {

        TextAsset data = Resources.Load("Data", typeof(TextAsset)) as TextAsset;

        StringReader sr = new StringReader(data.text);


        // 먼저 한줄을 읽는다. 


        string source = sr.ReadLine();

        string [] values;                // 쉼표로 구분된 데이터들을 저장할 배열 (values[0]이면 첫번째 데이터 )

        

        

        while (source != null)

        {

            values = source.Split(',');  // 쉼표로 구분한다. 저장시에 쉼표로 구분하여 저장하였다.

            if( values.Length == 0 )

            {

                sr.Close();

                return;

            }

           source = sr.ReadLine();    // 한줄 읽는다.

        }

    }


   void Update()

   {


   }

Posted by 닉리
,