• 【java】打印流的基本实现及java.io.PrintStream、java.io.PrintWriter示例


     1 package 打印流;
     2 
     3 import java.io.File;
     4 import java.io.FileOutputStream;
     5 import java.io.IOException;
     6 import java.io.OutputStream;
     7 
     8 class MyPrint{
     9     private OutputStream out;
    10     public MyPrint(OutputStream out){
    11         this.out=out;
    12     }
    13     public void print(String str) throws IOException{
    14         out.write(str.getBytes());
    15     }
    16     public void print(int i) throws IOException{
    17         print(String.valueOf(i));
    18     }
    19     public void print(byte b) throws IOException{
    20         print(String.valueOf(b));
    21     }
    22     public void print(short s) throws IOException{
    23         print(String.valueOf(s));
    24     }
    25     public void print(long l) throws IOException{
    26         print(String.valueOf(l));
    27     }
    28     public void print(float f) throws IOException{
    29         print(String.valueOf(f));
    30     }
    31     public void print(double d) throws IOException{
    32         print(String.valueOf(d));
    33     }
    34     public void print(boolean b) throws Exception{
    35         print(String.valueOf(b));
    36     }
    37     public void print(char c) throws IOException{
    38         print(String.valueOf(c));
    39     }
    40     public void close() throws IOException{
    41         out.close();
    42     }
    43 }
    44 public class TestPrint {
    45     public static void main(String[] args) throws IOException {
    46         MyPrint myPrint=new MyPrint(new FileOutputStream(new File("D:"+File.separator+"testA.txt")));
    47         myPrint.print(8.8);
    48         myPrint.print('a');
    49         myPrint.close();
    50     }
    51 }
    MyPrint
     1 package 打印流;
     2 
     3 import java.io.File;
     4 import java.io.FileOutputStream;
     5 import java.io.FileWriter;
     6 import java.io.IOException;
     7 import java.io.PrintStream;
     8 import java.io.PrintWriter;
     9 
    10 public class TestPrintStream {
    11     public static void main(String[] args) throws IOException {
    12         PrintStream printStream=new PrintStream(new FileOutputStream(new File("D:"+File.separator+"Test.txt")));
    13         printStream.print(true);
    14         printStream.println("abc中国人");
    15         printStream.close();
    16         
    17         PrintWriter printWriter=new PrintWriter(new FileWriter(new File("D:"+File.separator+"testA.txt")));
    18         printWriter.print(false);
    19         printWriter.println(89898.23);
    20         printWriter.close();
    21     }    
    22 }
    PrintStream和PrintWriter
     1 package 打印流;
     2 
     3 import java.io.File;
     4 import java.io.FileOutputStream;
     5 import java.io.IOException;
     6 import java.io.PrintStream;
     7 
     8 public class TestPrintStream {
     9     public static void main(String[] args) throws IOException {
    10         String name = "张三";
    11         int age = 20;
    12         float height = 1.7586f;
    13         PrintStream printStream = new PrintStream(new FileOutputStream(
    14                 new File("D:" + File.separator + "Test.txt")));
    15         printStream.printf("姓名:%s,年龄:%d,身高:%1.2f", name, age, height);
    16         printStream.close();
    17 
    18         // PrintWriter printWriter=new PrintWriter(new FileWriter(new
    19         // File("D:"+File.separator+"testA.txt")));
    20         // printWriter.close();
    21         System.out.println(String.format("姓名:%s,年龄:%d,身高:%1.2f", name, age,
    22                 height));
    23     }
    24 }
    格式化输出

    输出操作建议用打印流,而不是输出流

  • 相关阅读:
    进程调度算法
    附近的人,附近的卖家(geohash+前缀树)
    海量信息库,查找是否存在(bloom filter布隆过滤器)
    继承、虚继承和虚函数表对类的大小的影响
    linux 用户空间与内核空间——高端内存详解
    0-1背包问题入门
    范式
    vue的无缝滚动插件vue-seamless-scroll的安装与使用
    在vue项目中使用swiper2.7.6
    vue项目在IE下报 [vuex] vuex requires a Promise polyfill in this browser问题
  • 原文地址:https://www.cnblogs.com/xiongjiawei/p/6684929.html
Copyright © 2020-2023  润新知