[問題] FileInfo and StreamWriter
下面這程式碼 如果file_path不存在 就會有exception出現
public void write(String file_path)
{
FileInfo f = new FileInfo(file_path);
if (f.Exists)
{
;//do nothing
}
else
{
f.Create();
}
StreamWriter sw = f.CreateText();
sw.Write("hello world");
sw.Flush();
sw.Close();
}
若改成這樣 就ok了
public void write(String file_path)
{
FileInfo f = new FileInfo(file_path);
StreamWriter sw = f.CreateText();
sw.Write("hello world");
sw.Flush();
sw.Close();
if (f.Exists)
{
;//do nothing
}
else
{
f.Create();
}
}
其實就是if else放在下面
那為什麼第一段code會有問題呢?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 202.39.212.210
推
09/22 15:24, , 1F
09/22 15:24, 1F