View Code
  1 public class clsSSA
  2     {
  3         #region 属性
  4 
  5         /// <summary>
  6         /// 解析文件路径
  7         /// </summary>
  8         public string FilePath { get; set; }
  9 
 10         /// <summary>
 11         /// 保存文件路径
 12         /// </summary>
 13         public string SavePath { get; set; }
 14 
 15         /// <summary>
 16         /// 保存文件名
 17         /// </summary>
 18         public string FileName { get; set; }
 19 
 20         #endregion
 21 
 22 
 23         #region 成员变量
 24 
 25         /// <summary>
 26         /// 文本内容
 27         /// </summary>
 28         public List<string> contents = new List<string>();
 29 
 30         #endregion
 31 
 32 
 33         #region 构造函数
 34 
 35         /// <summary>
 36         /// 构造函数
 37         /// </summary>
 38         /// <param name="filePath">文件源路径</param>
 39         /// <param name="savePath">文件保存路径</param>
 40         public clsSSA(string filePath, string savePath, string fileName)
 41         {
 42             this.FilePath = filePath;
 43             this.SavePath = savePath;
 44             this.FileName = fileName;
 45         }
 46 
 47         #endregion
 48        
 49 
 50         #region 自定义方法
 51 
 52         /// <summary>
 53         /// 作用:解析SSA文件
 54         /// 作者:殳亚军
 55         /// 编写日期:2012-04-28
 56         /// </summary>
 57         public bool AnalysisSSA()
 58         {
 59             //定义文件流
 60             FileStream fs = null;
 61             //定义读取器
 62             StreamReader sr = null;
 63             //是否解析成功
 64             bool isAnalysised = true;
 65             try
 66             {
 67                 fs = new FileStream(this.FilePath, FileMode.Open);
 68                 sr = new StreamReader(fs, Encoding.Default);
 69                 //每行内容
 70                 string line = string.Empty;
 71                 while ((line = sr.ReadLine()) != null)
 72                 {
 73                     //将每行按“,”分割成数组
 74                     string[] lines = line.Split(':');
 75                     if (lines[0] == "Dialogue")
 76                     {
 77                         string tempLines = line.Substring(0, line.IndexOf("!Effect") + 7);
 78                         string[] tempLinesArray = tempLines.Split(',');
 79                         //取出起始时间和文本
 80                         string start = tempLinesArray[1];
 81                         string end = tempLinesArray[2];
 82                         string content = string.Empty;
 83                         if (line.Contains('}'))
 84                         {
 85                             content = line.Substring(line.LastIndexOf('}') + 1);
 86                         }
 87                         else
 88                         {
 89                             content = line.Substring(line.IndexOf("!Effect") + 8);
 90                         }
 91                         //判读是否是中英双语
 92                         if (content.Contains(@"\N"))
 93                         {
 94                             string[] strs = content.Split(new string[] { @"\N" }, StringSplitOptions.RemoveEmptyEntries);
 95                             string temp = string.Empty;
 96                             foreach (string s in strs)
 97                             {
 98                                 temp += s + "\t";
 99                             }
100                             content = temp;
101                         }
102                         //存储起来
103                         contents.Add(start + "," + end + "," + content);
104                     }
105                 }  
106 
107                 //写入文件
108                 if (!WriteToFile(contents))
109                 {
110                     isAnalysised = false;
111                 }
112             }
113             catch (Exception e)
114             {
115                 isAnalysised = false;  
116                 return false;
117             }
118             finally
119             {
120                 //关闭读取器
121                 sr.Close();
122                 //关闭文件流
123                 fs.Close();
124 
125             }
126             return isAnalysised;
127         }
128 
129         /// <summary>
130         /// 作用:写入文本文件
131         /// 作者:殳亚军
132         /// 编写日期:2012-04-28
133         /// </summary>
134         /// <param name="contents"></param>
135         private bool WriteToFile(List<string> contents)
136         {
137             //定义文件流
138             FileStream fs = null;
139             //定义读取器
140             StreamWriter sw = null;
141             //是否写入成功
142             bool isWrited = true;
143             try
144             {
145                 string fileName = this.FileName.Substring(0, this.FileName.LastIndexOf('.')) + ".txt";
146                 string savePath = this.SavePath + fileName;
147 
148                 fs = new FileStream(savePath, FileMode.Create);
149                 sw = new StreamWriter(fs, Encoding.Default);
150                 //开始写
151                 foreach (string s in contents)
152                 {
153                     sw.WriteLine(s);
154                 }
155                 //清空缓冲区
156                 sw.Flush();
157             }
158             catch (Exception e)
159             {
160                 isWrited = false;
161                 return false;
162             }
163             finally
164             {
165                 //关闭流
166                 sw.Close();
167                 fs.Close();
168             }
169             return isWrited;
170         }
171 
172         #endregion
173         
174     }
posted on 2012-05-11 13:47  捣乃忒  阅读(348)  评论(0编辑  收藏  举报