2014-05-21 2 views
0

나는 이와 같은 간단한 클래스 목록을 가지고 있습니다.txt에서/txt로 클래스 목록 저장 /로드

public class Album 
    { 
     public int IDNumber { get; set; } 
     public string AlbumName { get; set; } 
     public string Artist { get; set; } 
     public int ReleaseDate { get; set; } 
     public int TrackAmount { get; set; } 
     public string Location { get; set; } 
     public int Rating { get; set; } 

     public Album(int _id, string _name, string _artist, int _releasedate, int _trackamount, string _location, int _rating) 
     { 
      IDNumber = _id; 
      AlbumName = _name; 
      Artist = _artist; 
      ReleaseDate = _releasedate; 
      TrackAmount = _trackamount; 
      Location = _location; 
      Rating = _rating; 
     } 
    } 

그리고 파일에 저장하고 파일에서 읽어야합니다. 나는 C++에 상당히 익숙하다. C++의 메서드는 전혀 작동하지 않는다. 이 작업을 수행하는 간단한 방법이 있습니까? 이 파일에 다음과 같이 표시하고 싶습니다.

id albumname artist releasedate trackamout location rating 

간단한 방법이 있습니까?

+2

당신이 찾고있는 것은 '직렬화'라는 용어입니다. "클래스/구조체를 직렬화하는"방법에 대해 많은 예제를 온라인에서 찾을 수있을 것이라고 확신합니다. – Kache

답변

0

는이 같은 수, 파일에 목록을 저장하려면 다음과 같이 말했다 ... 당신은 파일 목록을로드 할 경우 실제로 직렬화를 사용하는 것이 가장 좋은 방법이 될 것입니다

// create a writer and open the file 
    StreamWriter stream = new StreamWriter("myfile.txt"); 

    foreach(Album album in myListOfAlbum) 
    { 
    // write a line of text to the file 
    stream.WriteLine(
     album.IDNumber.ToString() + " " + 
     album.AlbumName.ToString() + " " + 
     ... 
     ... 
    ); 
    } 

    // close the stream 
    stream.Close(); 

너무.