[Java] 将标准字符流写入到文件中(通过控制台写一个html程序,并保存)

时间:2023-11-26 10:40:50
package test.stream;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter; /**
* 通过控制台写一个html程序,并输出
* @author Frost.Yen
* @E-mail 871979853@qq.com
* @date 2016年4月13日
*/
public class TestHtml {
public static void main(String[] args) {
BufferedReader br = null;
PrintWriter out = null;
try {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new BufferedWriter(new FileWriter("E:\\JAVA\\Examples\\To Learn\\src\\test\\stream\\test.html")));
String str = null;
while((str = br.readLine())!=null){
if (str.equals("exit")) {
System.out.println("thank you");
break;
}
out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(br!=null) br.close();
} catch (IOException e) {
e.printStackTrace();
} try {
if(out!=null) out.close();
} catch (Exception e) {
e.printStackTrace();
}
} }
}

运行后在控制台输入

<html>
<head><title>java test</title></head>
<body>
<marquee><h1>notic java do it<h1><marquee>
</body>
<html>
exit

回车

即可生成test.html