IO(输入_输出)4——打印流、序列化流

IO——打印流、序列化流文章目录IO——打印流、序列化流打印流重定向标准I/O序列化流概述使用序列化保存对象信息序列化和反序列化实现注册和登录Properties集合概述使用场景打印流字节打印流 PrintStream字符打印流 PrintWriter优势:支持向文件中输出任意的数据类型。自动刷新的功能,不需要调用flush()或close(),操作更加简洁方法:void pr...
摘要由CSDN通过智能技术生成

打印流

字节打印流 PrintStream

字符打印流 PrintWriter

优势:支持向文件中输出任意的数据类型。自动刷新的功能,不需要调用flush()或close(),操作更加简洁

  • 方法:

void print(String str): 输出任意类型的数据,

void println(String str): 输出任意类型的数据,自动写入换行操作

可以通过构造方法,完成文件数据的自动刷新功能

  • 构造方法:

开启文件自动刷新写入功能

public PrintWriter(OutputStream out, boolean autoFlush)

public PrintWriter(Writer out, boolean autoFlush)

/*打印流:
 *  PrintWriter:是Writer一个子类,只能写入,不能读取
 *  
 *  PrintWriter(OutputStream out) 
    PrintWriter(Writer out) 

 *  void print(boolean b) 
 *  void print(int i) 
 
 *  void println(int x) 
    void println(String x) 
   PrintWriter特点:
     1.保证数据原样写入
     2.PrintWriter底层也使用了缓冲区,利用PrintWriter写数据,其实也是
                写入了PrintWriter底层的缓冲区,我们需要通过手动刷新或者close()
     3.自动刷新
       PrintWriter(OutputStream out, boolean autoFlush) 
       PrintWriter(Writer out, boolean autoFlush) 
       当autoFlush为true的时候会启动自动刷新,自动刷新只针对println有效
 */
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class Demo {
   
    public static void main(String[] args) throws IOException {
   
        //PrintWriter自带刷新,需要在构造方法中开启
        PrintWriter pw = new PrintWriter(new FileWriter("print.txt"),true);//默认不自动刷新
        pw.print(12);
        pw.println(12.22);//自动刷新,println()
        pw.print("aaaaaa");
        pw.println();//flush()
    }
}

重定向标准I/O

System.in和System.out。它们是java提供的两个标准的输入/输出流,主要用于从键盘接收数据以及向屏幕输出数据。

  • System.in常见方法:

    • int read(),此方法从键盘接收一个字节的数据,返回值是该字符的ASCII码
    • int read(byte []buf),此方法从键盘接收多个字节的数据,保存至buf中,返回值是接收字节数据的个数。
  • System.out常见方法:

    • print(),向屏幕输出数据,不换行,参数可以是java的任意数据类型
    • println(),向屏幕输出数据,换行,参数可以是java的任意数据类型

System类提供了3个重定向标准输入/输出的方法

方法 说明
static void setErr(PrintStream) 重定向标准错误输出流
static void setIn(InputStream in) 重定向标准输入流
static void setOut(PrintStream out) 重定向标准输出流

程序向文件输出,System.out.println()直接输出,不要重复创建FileWriter

import java.io.FileInputStream;
import java.util.Scanner;

public class Demo {
   
    public static void main(String[] args) throws Exception {
   
        //InputStream in = System.in;//Console控制台
        //Scanner(InputStream)
        //Scanner读取任意数据类型
        //Scanner input = new Scanner(System.in);
        //Scanner从文件中读取数据aa.txt
        Scanner input = new Scanner(new FileInputStream("aa.txt"));
        //Scanner具有从console获取用户输入数据的功能
        String content = input.nextLine();
        System.out.println(content);
        //Scanner:控制台的数据-->java程序   Input

        input.close();
    }
}
public class Demo {
   
    public static void main(String[] args) throws IOException {
   
        //模仿Scanner从控制台获取用户输入的数据
        //InputStream-->Reader  转换流
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        //接收用户输入
        System.out.print("请输入您的姓名:");
        String name = br.readLine();
        System.out.println
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值