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

    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

  • 相关阅读:
    Junit单元测试
    Stream流方法引用
    Stream流思想和常用方法
    算法
    函数式接口
    Zookeeper理解
    GreenPlum学习之(Share-nothing)架构
    链表反转问题
    KMP算法的java实现
    KMP详解之二
  • 原文地址:https://www.cnblogs.com/frost-yen/p/5388170.html
Copyright © 2011-2022 走看看