using System;
using System.IO;
using System.Text;
/// <summary>
/// 텍스트 파일 관리자
/// </summary>
public class TextFileManager : IDisposable
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 텍스트 파일 관리자 종류
/// </summary>
private TextFileManagerType textFileManagerType;
/// <summary>
/// 스트림 작성자
/// </summary>
private StreamWriter streamWriter;
/// <summary>
/// 스트림 리더기
/// </summary>
private StreamReader streamReader;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 텍스트 파일 관리자 종류 - TextFileManagerType
/// <summary>
/// 텍스트 파일 관리자 종류
/// </summary>
public TextFileManagerType TextFileManagerType
{
get
{
return this.textFileManagerType;
}
}
#endregion
#region 스트림 작성자 - StreamWriter
/// <summary>
/// 스트림 작성자
/// </summary>
public StreamWriter StreamWriter
{
get
{
return this.streamWriter;
}
}
#endregion
#region 스트림 리더기 - StreamReader
/// <summary>
/// 스트림 리더기
/// </summary>
public StreamReader StreamReader
{
get
{
return this.streamReader;
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - TextFileManager(streamWriter)
/// <summary>
/// 생성자
/// </summary>
/// <param name="streamWriter">스트림 작성자</param>
public TextFileManager(StreamWriter streamWriter)
{
this.textFileManagerType = TextFileManagerType.StreamWriter;
this.streamWriter = streamWriter;
this.streamReader = null;
}
#endregion
#region 생성자 - TextFileManager(streamReader)
/// <summary>
/// 생성자
/// </summary>
/// <param name="streamReader">스트림 리더기</param>
public TextFileManager(StreamReader streamReader)
{
this.textFileManagerType = TextFileManagerType.StreamReader;
this.streamWriter = null;
this.streamReader = streamReader;
}
#endregion
#region 생성자 - TextFileManager(textFileManagerType, filePath, fileMode)
/// <summary>
/// 생성자
/// </summary>
/// <param name="textFileManagerType">텍스트 파일 관리자 종류</param>
/// <param name="filePath">파일 경로</param>
/// <param name="fileMode">파일 모드</param>
public TextFileManager(TextFileManagerType textFileManagerType, string filePath, FileMode fileMode)
{
this.textFileManagerType = textFileManagerType;
if(this.textFileManagerType == TextFileManagerType.StreamWriter)
{
FileStream fileStream = new FileStream(filePath, fileMode, FileAccess.Write);
this.streamWriter = new StreamWriter(fileStream);
}
else
{
FileStream fileStream = new FileStream(filePath, fileMode, FileAccess.Read);
this.streamReader = new StreamReader(fileStream);
}
}
#endregion
#region 생성자 - TextFileManager(filePath)
/// <summary>
/// 생성자
/// </summary>
/// <param name="filePath">파일 경로</param>
public TextFileManager(string filePath) : this(TextFileManagerType.StreamReader, filePath, FileMode.Open)
{
}
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 생성자 - TextFileManager()
/// <summary>
/// 생성자
/// </summary>
private TextFileManager()
{
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Destructor
#region 소멸자 - ~TextFileManager()
/// <summary>
/// 소멸자
/// </summary>
~TextFileManager()
{
Close();
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 쓰기 - Write<T>(source)
/// <summary>
/// 쓰기
/// </summary>
/// <typeparam name="T">T형</typeparam>
/// <param name="source">소스</param>
public void Write<T>(T source)
{
this.streamWriter.Write(source);
}
#endregion
#region 행 쓰기 - WriteLine<T>(source)
/// <summary>
/// 행 쓰기
/// </summary>
/// <typeparam name="T">T형</typeparam>
/// <param name="source">소스</param>
public void WriteLine<T>(T source)
{
this.streamWriter.WriteLine(source);
}
#endregion
#region 행 쓰기 - WriteLine()
/// <summary>
/// 행 쓰기
/// </summary>
public void WriteLine()
{
this.streamWriter.WriteLine();
}
#endregion
#region 읽기 - Read(targetCharacterArray, targetCharacterArrayOffset, readLength)
/// <summary>
/// 읽기
/// </summary>
/// <param name="targetCharacterArray">타겟 문자 배열</param>
/// <param name="targetCharacterArrayOffset">타겟 문자 배열 오프셋</param>
/// <param name="readLength">읽기 길이</param>
/// <returns>실제 읽은 길이</returns>
public int Read(char[] targetCharacterArray, int targetCharacterArrayOffset, int readLength)
{
return this.streamReader.Read(targetCharacterArray, targetCharacterArrayOffset, readLength);
}
#endregion
#region 읽기 - Read(targetCharacterArray)
/// <summary>
/// 읽기
/// </summary>
/// <param name="targetCharacterArray">타겟 문자 배열</param>
/// <returns>실제 읽은 길이</returns>
public int Read(char[] targetCharacterArray)
{
return Read(targetCharacterArray, 0, targetCharacterArray.Length);
}
#endregion
#region 읽기 - Read(readLength)
/// <summary>
/// 읽기
/// </summary>
/// <param name="readLength">읽기 길이</param>
/// <returns>실제 읽은 길이</returns>
public string Read(int readLength)
{
int actualReadLength = -1;
char[] targetCharacterArray = new char[readLength];
actualReadLength = this.streamReader.Read(targetCharacterArray, 0, readLength);
StringBuilder stringBuilder = new StringBuilder();
for(int i = 0; i < actualReadLength; i++)
{
stringBuilder.Append(targetCharacterArray[i]);
}
if(stringBuilder.Length == 0)
{
return null;
}
return stringBuilder.ToString();
}
#endregion
#region 행 읽기 - ReadLine()
/// <summary>
/// 행 읽기
/// </summary>
/// <returns>읽은 행</returns>
public string ReadLine()
{
return this.streamReader.ReadLine();
}
#endregion
#region 끝까지 읽기 - ReadToEnd()
/// <summary>
/// 끝까지 읽기
/// </summary>
/// <returns>읽은 문자열</returns>
public string ReadToEnd()
{
return this.streamReader.ReadToEnd();
}
#endregion
#region 닫기 - Close()
/// <summary>
/// 닫기
/// </summary>
public void Close()
{
if(this.streamWriter != null)
{
this.streamWriter.Flush();
this.streamWriter.Close();
this.streamWriter.Dispose();
this.streamWriter = null;
}
if(this.streamReader != null)
{
this.streamReader.Close();
this.streamReader.Dispose();
this.streamReader = null;
}
}
#endregion
#region 해제하기 - Dispose()
/// <summary>
/// 해제하기
/// </summary>
public void Dispose()
{
Close();
}
#endregion
}