Java IO之打印流,缓冲流,Scanner的用法

    xiaoxiao2021-12-14  19

    首先,我先介绍下,在整个IO包中,打印流是输出信息最方便的类,主要包含字节打印流(PrintStream)和字符打印流(PrintWiter),打印流提供了非常方便的打印功能,可以打印任何的数据类型,列如:小数,整数,字符串等等。

    构造方法:

    public PrintStream(OutputStream out) --> 指定输出位置

    程序介绍:

    public class TestPrint1 { public static void main(String[] args) throws FileNotFoundException { PrintStream ps = null; // 声明打印流对 // 如果现在是使用FileOuputStream实例化,意味着所有的输出是向文件之中 ps = new PrintStream(new FileOutputStream(new File("D:"+File.separator + "test.txt"))); ps.print("hello"); ps.println("world!"); ps.print("1+1="+2); ps.close(); } }

    实际上是将FileOutputStream类的功能包装一下。

    public class TestPrint2 { public static void main(String[] args) throws FileNotFoundException { PrintStream ps = null; ps = new PrintStream(new FileOutputStream(new File("D:"+File.separator + "test.txt"))); String name = "胡清风"; // 定义字符串 int age = 30; // 定义整数 float score = 990.356f; // 定义小数 char sex = 'M'; // 定义字符 ps.printf("姓名:%s;年龄:%d;成绩:%f;性别:%c",name,age,score,sex); ps.close(); } }

    格式化输出:

    姓名:李兴华;年龄:30;成绩:990.356018;性别:M

    总结: 1、PrintStream 可以方便的完成输出的功能。

    2、在以后的输出中基本上都使用PrintStream 完成,因为比较方便一些。

    3、PrintStream 属于装饰设计模式。

    Java5 添加了Java.util.Scanner类,这是一个用于扫描输入文本的新的实用程序。它是以前的StringTokenizer和Matcher类之间的某种结合。任何数据都必须通过同一模式的捕获组索引或通过使用一个索引来检索文本的各个部分。可以结合使用正则表达式和从输入流中检索特定类型数据项的方法。除了使用正则表达式之外,Scanner类还可以任意地对字符串和基本类型(int,double)的数据进行分析。

    程序介绍:

    public class TextScanner { public static void main(String[] args) { //创建Scanner对象 接受从控制台输入 Scanner input = new Scanner(System.in); System.out.println("请输入名字:"); //接受String型 String name = input.next(); System.out.println("请输入学号"); //接受int型 int id = input.nextInt(); //输出结果 System.out.println("名字为:"+name+"\t学号为:"+id); } }

    Java缓冲流本身不具IO功能,只是别的流上加上缓冲提高效率。

    缓冲流分为字节和字符缓冲流

    字节缓冲流:

    BufferedInputStream—字节输入缓冲流 BufferedOutputStream—字节输出缓冲流

    字符缓冲流:

    BufferedReader—字符输入缓冲流 BufferedWriter—字符输出缓冲流

    程序介绍:

    public class BufferedOutStream extends BufferedOutputStream{ public byte[] buf; public BufferedOutStream(OutputStream out) { this(out, 8192); } public BufferedOutStream(OutputStream out, int size) { super(out); if (size <= 0) { throw new IllegalArgumentException("Buffer size <= 0"); } buf = new byte[size]; } public static void main(String[] args) throws FileNotFoundException { try { //创建字节输出流实例 OutputStream out = new FileOutputStream("D:\\workspace\\test.txt"); //根据字节输出流构建字节缓冲流 BufferedOutputStream buf = new BufferedOutputStream(out); String data = "好好学习,天天向上"; buf.write(data.getBytes()); //写入缓冲区 buf.flush(); //刷新缓冲区,即把内容写入 buf.close(); //关闭缓冲流时,也会刷新一次缓冲区 out.close(); } catch (IOException e) { e.printStackTrace(); } } }

    BufferedOutputStream类实现缓冲的输出了,通过设置这种输出流,应用程序就可以将各个字节写入底层输出流中,而不必每一个字节写入都调用底层系统。

    public class BufferedInputStream extends java.io.BufferedInputStream{ public BufferedInputStream(InputStream in) { super(in); } public BufferedInputStream(InputStream in, int size) { super(in, size); } public static void main(String[] args) throws FileNotFoundException { try { //创建字节输入流实例 InputStream in = new FileInputStream("D:\\workspace\\test.txt"); //根据字节输入流构建字节缓冲流 BufferedInputStream buf = new BufferedInputStream(in); byte[] bytes = new byte[1024]; int len = -1; StringBuffer sb = new StringBuffer(); while ((len = buf.read(bytes))!= -1) { sb.append(new String(bytes,0,len)); } System.out.println("内容为:"+sb); buf.close(); in.close(); } catch (IOException e) { e.printStackTrace(); } } }

    BufferedInputStream为别的输入流添加缓冲功能,在创建BufferedInputStream时会创建一个内部缓冲数组,用于缓冲数据,提高性能。

    输出结果:

    内容为:好好学习,天天向上

    字符缓冲流与字节缓冲流的程序结构是一样的,只需要理解程序就可以写出来,这里我就不再重复了。

    转载请注明原文地址: https://ju.6miu.com/read-969898.html

    最新回复(0)