Java基础23--PrintStream和PrintWriter

Java基础23–打印流:PrintStream和PrintWriter

打印流:输出流
(1)PrintStream:
经典代表:System.out
Sysetm.err

@Test
	public void test01(){
		PrintStream out = System.out;
		out.println("hello");
		
//		out.print();//print没有无参方法
		out.println();
	}

在这里插入图片描述
new PrintStream(文件名)
new PrintStream(文件名,编码)
new PrintStream(另一个字节输出流)

	@Test
	public void test02() throws IOException{
		PrintStream ps = new PrintStream("1.txt");
		ps.println("hello");
		ps.println("world");
		ps.close();
	}

在这里插入图片描述
1.txt文件里有打印内容

@Test
	public void test03() throws IOException{
		PrintStream ps = new PrintStream("d:/1.txt","GBK");
		ps.println("中文");
		ps.close();
	}

在这里插入图片描述
不用inputStream,直接打印过来

@Test
	public void test04() throws IOException{
		//所有数据类型写出去,都是按照文本处理
		PrintStream ps = new PrintStream("1.txt");
		ps.println(12);
		ps.println(true);
		ps.println(new Goods("《从入门到放弃》", 99.99, 1000));
		ps.close();
	}

在这里插入图片描述
所有数据类型写出去,都是按照文本处理,比如12不是按int型存进去的,而是按1,2,的字符存进去的,其他也类似

(2)PrintWriter
Web阶段学习时,从服务器端往客户端返回消息时用到response,response.getWriter()可以返回PrintWriter对象。
即 Web服务器往客户端(例如:浏览器)返回html网页时,用的是PrintWriter对象的输出方法。

文本扫描仪:Scannner

System.in:默认情况下是从键盘输入的数据中扫描
Scanner:可以从你指定的文件、流中读取文本数据
在这里插入图片描述
Scanner:

@Test
	public void test01(){
		Scanner input = new Scanner(System.in);
		System.out.print("请输入一个整数:");
		int num = input.nextInt();
		System.out.println("num = " + num);
		input.close();
	}
@Test
	public void test02() throws FileNotFoundException{
		Scanner input = new Scanner(new FileInputStream("1.txt"));//InputStream
		
		while(input.hasNextLine()){
			String line = input.nextLine();
			System.out.println(line);
		}
		
		input.close();
	}

直接文件对象也行

@Test
	public void test03() throws FileNotFoundException{
		Scanner input = new Scanner(new File("1.txt"));//InputStream
		
		while(input.hasNextLine()){
			String line = input.nextLine();
			System.out.println(line);
		}
		
		input.close();
	}
@Test
	public void test04() throws FileNotFoundException{
		Scanner input = new Scanner("1.txt");//InputStream
		
		while(input.hasNextLine()){
			String line = input.nextLine();
			System.out.println(line);
		}
		
		input.close();
	}
@Test
	public void test05() throws FileNotFoundException{
		Scanner input = new Scanner(new File("d:/1.txt"),"GBK");//InputStream
		
		while(input.hasNextLine()){
			String line = input.nextLine();
			System.out.println(line);
		}
		
		input.close();
	}

在这里插入图片描述

System类中的IO流

在Java层面是常量对象,但是可以通过C等底层语言进行修改,例如以下三个:
System.in:
System.out
System.err

观察System,发现inputStream为null,且为静态常量,不能改变
在这里插入图片描述

@Test
	public void test01(){
		PrintStream out = System.out;
		System.out.println(out);
	}

在这里插入图片描述
结果不为null,有对象
原因:静态常量out的赋值创建什么的不是通过java来的
在这里插入图片描述
可以看见system调用了一个静态方法,这个方法没有方法体,且是native修饰,说明底层是非java语言实现的,c语言(java中不能修改的静态常量,可以用非Java语言修改)

	@Test
	public void test02() throws FileNotFoundException{
		System.setOut(new PrintStream("1.txt"));
		
		System.out.println("aaaa");
		System.out.println("bbb");
		System.out.println("ccc");
		System.out.println("ddd");
	}

在这里插入图片描述
没往控制台打印,而是在1.txt文件里,说明不是System.out.println()都是往控制台打印,得看setOut()是往哪儿设置打印得,默认情况下往控制台打印,修改的话用setOut()方法,setOut()底层也是非java语言实现的

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值