printStream

PrintStream

介绍:

public class PrintStream:打印流

  • PrintStream为另一个输出流添加了功能,即能够方便地打印各种数据值的表示。
  • 特点:
  • 1.只负责数据的输出,不负责数据的读取
  • 2.与其他输出流不同, PrintStream从不抛出IOException ;
  • 3.有特有的方法:print,println
  • 构造方法:
  • PrintStream(File file)
    使用指定的文件创建一个新的打印流,而不需要自动换行。
  • PrintStream(String fileName)
    使用指定的文件名创建新的打印流,无需自动换行
  • PrintStream(OutputStream out)
    创建一个新的打印流。
  • extends FilterOutputStream继承父亲的方法

注意:

  • 如果使用继承父亲的方法write写数据,那么查看数据的时候会产看编码表97->a
  • 如果使用自己特有的方法print/println方法写数据,写的数据原样输出97->97
public class Demo01PrintStream {
    public static void main(String[] args) throws IOException {
        PrintStream ps=new PrintStream("cn\\itcast\\IO\\PrintStream\\print.txt");
        ps.write(97);
        ps.println(97);
        ps.println("任意类型数据");
        ps.println("\\.");
        ps.println(1256.216);
        ps.close();

    }
}

static void setOut(PrintStream out)

重新分配“标准”输出流。

public class Demo02Setout {
    public static void main(String[] args) throws FileNotFoundException {
        System.out.println("控制台输出");
        System.setOut(new PrintStream("cn\\itcast\\IO\\PrintStream\\systemsetout.txt"));
        System.out.println("输出位置转变");

    }

}

结果显示请读者自行运行

猜你喜欢

转载自blog.csdn.net/tangshuai96/article/details/104283292