JavaIO总结Demo大全(2)

4.几个特殊的输入流类分析

LineNumberInputStream

主要完成从流中读取数据时,会得到相应的行号,至于什么时候分行、在哪里分行是由改类主动确定的,并不是在原始中有这样一个行号。在输出部分没有对应的部分,我们完全可以自己建立一个LineNumberOutputStream,在最初写入时会有一个基准的行号,以后每次遇到换行时会在下一行添加一个行号,看起来也是可以的。好像更不入流了。

PushbackInputStream

其功能是查看最后一个字节,不满意就放入缓冲区。主要用在编译器的语法、词法分析部分。输出部分的BufferedOutputStream 几乎实现相近的功能。

StringBufferInputStream

已经被Deprecated,本身就不应该出现在InputStream部分,主要因为String 应该属于字符流的范围。已经被废弃了,当然输出部分也没有必要需要它了!还允许它存在只是为了保持版本的向下兼容而已。

SequenceInputStream

可以认为是一个工具类,将两个或者多个输入流当成一个输入流依次读取。完全可以从IO 包中去除,还完全不影响IO 包的结构,却让其更“纯洁”――纯洁的Decorator 模式。

【案例】将两个文本文件合并为另外一个文本文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.SequenceInputStream;
  
/**
  * 将两个文本文件合并为另外一个文本文件
  * */
public class SequenceInputStreamDemo{
     public static voidmain(String[] args) throws IOException{
         File file1 = newFile( "d:" + File.separator + "hello1.txt" );
         File file2 = newFile( "d:" + File.separator + "hello2.txt" );
         File file3 = newFile( "d:" + File.separator + "hello.txt" );
         InputStream input1 = new FileInputStream(file1);
         InputStream input2 = new FileInputStream(file2);
         OutputStream output = new FileOutputStream(file3);
         // 合并流
         SequenceInputStreamsis = new SequenceInputStream(input1, input2);
         int temp = 0 ;
         while ((temp =sis.read()) != - 1 ){
            output.write(temp);
         }
         input1.close();
         input2.close();
         output.close();
         sis.close();
     }
}

PrintStream

也可以认为是一个辅助工具。主要可以向其他输出流,或者FileInputStream 写入数据,本身内部实现还是带缓冲的。本质上是对其它流的综合运用的一个工具而已。一样可以踢出IO 包!System.err和System.out 就是PrintStream 的实例!

【案例】使用PrintStream进行输出

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
  * 使用PrintStream进行输出
  * */
import java.io.*;
  
class hello {
    public static void main(String[] args) throws IOException {
        PrintStream print = new PrintStream( new FileOutputStream(newFile( "d:"
                 + File.separator + "hello.txt" )));
        print.println( true );
        print.println( "Rollen" );
        print.close();
     }
}

【案例】使用PrintStream进行格式化输出

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
  * 使用PrintStream进行输出
  * 并进行格式化
  * */
import java.io.*;
class hello {
    public static void main(String[] args) throws IOException {
        PrintStream print = new PrintStream( new FileOutputStream(newFile( "d:"
                 + File.separator + "hello.txt" )));
        String name= "Rollen" ;
        int age= 20 ;
        print.printf( "姓名:%s. 年龄:%d." ,name,age);
        print.close();
     }
}

【案例】使用OutputStream向屏幕上输出内容

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
  * 使用OutputStream向屏幕上输出内容
  * */
import java.io.*;
class hello {
    public static void main(String[] args) throws IOException {
        OutputStream out=System.out;
        try {
            out.write( "hello" .getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
     }
}

【案例】输入输出重定向

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
  
/**
  * 为System.out.println()重定向输出
  * */
public class systemDemo{
    public static void main(String[] args){
        // 此刻直接输出到屏幕
        System.out.println( "hello" );
        File file = new File( "d:" + File.separator + "hello.txt" );
        try {
            System.setOut( new PrintStream( new FileOutputStream(file)));
        } catch (FileNotFoundException e){
            e.printStackTrace();
        }
        System.out.println( "这些内容在文件中才能看到哦!" );
     }
}

【案例】使用System.err重定向

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
  
/**
  *System.err重定向这个例子也提示我们可以使用这种方法保存错误信息
  * */
public class systemErr{
    public static void main(String[] args){
        File file = new File( "d:" + File.separator + "hello.txt" );
        System.err.println( "这些在控制台输出" );
        try {
            System.setErr( new PrintStream( new FileOutputStream(file)));
        } catch (FileNotFoundException e){
            e.printStackTrace();
        }
        System.err.println( "这些在文件中才能看到哦!" );
     }
}

【案例】System.in重定向

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
  *System.in重定向
  * */
public class systemIn{
    public static void main(String[] args){
        File file = new File( "d:" + File.separator + "hello.txt" );
        if (!file.exists()){
            return ;
        } else {
            try {
                 System.setIn(newFileInputStream(file));
            } catch (FileNotFoundException e){
                 e.printStackTrace();
            }
            byte [] bytes = new byte [ 1024 ];
            int len = 0 ;
            try {
                 len = System.in.read(bytes);
            } catch (IOException e){
                 e.printStackTrace();
            }
            System.out.println( "读入的内容为:" + new String(bytes, 0 , len));
        }
     }
}

5.字符输入流Reader

定义和说明:

在上面的继承关系图中可以看出:

Reader 是所有的输入字符流的父类,它是一个抽象类。

CharReader、StringReader是两种基本的介质流,它们分别将Char 数组、String中读取数据。PipedReader 是从与其它线程共用的管道中读取数据。

BufferedReader 很明显就是一个装饰器,它和其子类负责装饰其它Reader 对象。

FilterReader 是所有自定义具体装饰流的父类,其子类PushbackReader 对Reader 对象进行装饰,会增加一个行号。

InputStreamReader 是一个连接字节流和字符流的桥梁,它将字节流转变为字符流。FileReader可以说是一个达到此功能、常用的工具类,在其源代码中明显使用了将FileInputStream 转变为Reader 的方法。我们可以从这个类中得到一定的技巧。Reader 中各个类的用途和使用方法基本和InputStream 中的类使用一致。后面会有Reader 与InputStream 的对应关系。

实例操作演示:

【案例】从文件中读取内容

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
  * 字符流
  * 从文件中读出内容
  * */
import java.io.*;
class hello{
    public static void main(String[] args) throws IOException {
        String fileName= "D:" +File.separator+ "hello.txt" ;
        File f= new File(fileName);
        char [] ch= new char [ 100 ];
        Reader read= new FileReader(f);
        int count=read.read(ch);
        read.close();
        System.out.println( "读入的长度为:" +count);
        System.out.println( "内容为" + new String(ch, 0 ,count));
     }
}

注意:当然最好采用循环读取的方式,因为我们有时候不知道文件到底有多大。

【案例】以循环方式从文件中读取内容

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
  * 字符流
  * 从文件中读出内容
  * */
import java.io.*;
class hello{
    public static void main(String[] args) throws IOException {
        String fileName= "D:" +File.separator+ "hello.txt" ;
        File f= new File(fileName);
        char [] ch= new char [ 100 ];
        Reader read= new FileReader(f);
        int temp= 0 ;
        int count= 0 ;
        while ((temp=read.read())!=(- 1 )){
            ch[count++]=( char )temp;
        }
        read.close();
        System.out.println( "内容为" + new String(ch, 0 ,count));
     }
}

【案例】BufferedReader的小例子

注意:BufferedReader只能接受字符流的缓冲区,因为每一个中文需要占据两个字节,所以需要将System.in这个字节输入流变为字符输入流,采用:

BufferedReader buf = new BufferedReader(newInputStreamReader(System.in));

下面是一个实例:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
  
/**
  * 使用缓冲区从键盘上读入内容
  * */
public class BufferedReaderDemo{
    public static void main(String[] args){
        BufferedReader buf = new BufferedReader(
                 newInputStreamReader(System.in));
        String str = null ;
        System.out.println( "请输入内容" );
        try {
            str = buf.readLine();
        } catch (IOException e){
            e.printStackTrace();
        }
        System.out.println( "你输入的内容是:" + str);
     }
}

【案例】Scanner类实例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.Scanner;
/**
  *Scanner的小例子,从键盘读数据
  * */
public class ScannerDemo{
     publicstatic void main(String[] args){
        Scanner sca = new Scanner(System.in);
        // 读一个整数
        int temp = sca.nextInt();
        System.out.println(temp);
        //读取浮点数
        float flo=sca.nextFloat();
        System.out.println(flo);
         //读取字符
        //...等等的,都是一些太基础的,就不师范了。
     }
}

【案例】Scanner类从文件中读出内容

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
  
/**
  *Scanner的小例子,从文件中读内容
  * */
public class ScannerDemo{
    public static void main(String[] args){
  
        File file = new File( "d:" + File.separator + "hello.txt" );
        Scanner sca = null ;
        try {
            sca = new Scanner(file);
        } catch (FileNotFoundException e){
            e.printStackTrace();
        }
        String str = sca.next();
        System.out.println( "从文件中读取的内容是:" + str);
     }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值