練習題 (8):Open a text file and convert it into HTML file. (File operations/Strings)
這一題其實我覺得出的蠻爛的,讀檔並且把內容轉成 HTML 檔案,那只要把檔案內容讀出來,把它放在下面那個藍色區塊的地方不就好了?可以練習的地方應該就只有,使用 StreamReader 讀檔跟 StreamWriter 寫檔這兩個部份吧。
最簡單的作法:
<html><head><title>HTML File</title></head><body>檔案內容 </body></html>
懶人程式碼:
-
using System;
-
using System.IO;
-
using System.Text;
-
namespace Convert2HTML
-
{
-
internal class Program
-
{
-
private static void Main( string [ ] args)
-
{
-
if (File.Exists ( @"in.txt" ) )
-
{
-
StreamReader sr = new StreamReader ( @"in.txt", Encoding. Default );
-
String s = sr.ReadToEnd ( );
-
StreamWriter sw = new StreamWriter ( @"out.txt", false, Encoding. Default );
-
sw.Write ( "<html><head><title>HTML File</title></head><body>{0}</body></html>", s);
-
sw.Flush ( );
-
sw.Dispose ( );
-
sr.Dispose ( );
-
}
-
else
-
{
-
Console.WriteLine ( "File does not exist!" );
-
}
-
}
-
}
-
}
|