java中的IO整理(4)

打印流

 

 

?
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( new  File( "d:"
                 + File.separator + "hello.txt" )));
         print.println( true );
         print.println( "Rollen" );
         print.close();
     }
}

 

 

【运行结果】:

 

true

 

Rollen

 

当然也可以格式化输出

 

 

?
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( new  File( "d:"
                 + File.separator + "hello.txt" )));
         String name= "Rollen" ;
         int  age= 20 ;
         print.printf( "姓名:%s. 年龄:%d." ,name,age);
         print.close();
     }
}

 

 

【运行结果】:

 

姓名:Rollen. 年龄:20.

 

 

 

使用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();
         }
     }
}

 

 

【运行结果】:

 

hello

 

 

 

输入输出重定向

 

 

?
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( "这些内容在文件中才能看到哦!" );
     }
}

 

 

【运行结果】:

eclipse的控制台输出的是hello。然后当我们查看d盘下面的hello.txt文件的时候,会在里面看到:这些内容在文件中才能看到哦!

 

?
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( "这些在文件中才能看到哦!" );
     }
}

 

 

【运行结果】:

 

你会在eclipse的控制台看到红色的输出:“这些在控制台输出”,然后在d盘下面的hello.txt中会看到:这些在文件中才能看到哦!

 

 

?
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
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( new  FileInputStream(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));
         }
     }
}

 

 

【运行结果】:

 

前提是我的d盘下面的hello.txt中的内容是:“这些文件中的内容哦!”,然后运行程序,输出的结果为:读入的内容为:这些文件中的内容哦!

 

 

 

BufferedReader的小例子

 

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

 

 

?
BufferedReader buf = new  BufferedReader(
                 new  InputStreamReader(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(
                 new  InputStreamReader(System.in));
         String str = null ;
         System.out.println( "请输入内容" );
         try {
             str = buf.readLine();
         } catch (IOException e){
             e.printStackTrace();
         }
         System.out.println( "你输入的内容是:"  + str);
     }
}

 

 

运行结果:

 

请输入内容

 

dasdas

 

你输入的内容是:dasdas

 

 

 

Scanner

 

其实我们比较常用的是采用Scanner类来进行数据输入,下面来给一个Scanner的例子吧

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import  java.util.Scanner;
 
/**
  * Scanner的小例子,从键盘读数据
  * */
public  class  ScannerDemo{
     public  static  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可以接受任何的输入流

 

下面给一个使用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);
     }
}

 

【运行结果】:

 

从文件中读取的内容是:这些文件中的内容哦!

 

数据操作流DataOutputStreamDataInputStream

 

 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import  java.io.DataOutputStream;
import  java.io.File;
import  java.io.FileOutputStream;
import  java.io.IOException;
 
public  class  DataOutputStreamDemo{
     public  static  void  main(String[] args) throws  IOException{
         File file = new  File( "d:"  + File.separator + "hello.txt" );
         char [] ch = { 'A' , 'B' , 'C'  };
         DataOutputStream out = null ;
         out = new  DataOutputStream( new  FileOutputStream(file));
         for ( char  temp : ch){
             out.writeChar(temp);
         }
         out.close();
     }
}

 

 

A B C

 

现在我们在上面例子的基础上,使用DataInputStream读出内容

 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import  java.io.DataInputStream;
import  java.io.File;
import  java.io.FileInputStream;
import  java.io.IOException;
 
public  class  DataOutputStreamDemo{
     public  static  void  main(String[] args) throws  IOException{
         File file = new  File( "d:"  + File.separator + "hello.txt" );
         DataInputStream input = new  DataInputStream( new  FileInputStream(file));
         char [] ch = new  char [ 10 ];
         int  count = 0 ;
         char  temp;
         while ((temp = input.readChar()) != 'C' ){
             ch[count++] = temp;
         }
         System.out.println(ch);
     }
}

 

 

【运行结果】:

 

AB

 

合并流 SequenceInputStream

 

SequenceInputStream主要用来将2个流合并在一起,比如将两个txt中的内容合并为另外一个txt。下面给出一个实例:

 

 

?
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  void  main(String[] args) throws  IOException{
         File file1 = new  File( "d:"  + File.separator + "hello1.txt" );
         File file2 = new  File( "d:"  + File.separator + "hello2.txt" );
         File file3 = new  File( "d:"  + File.separator + "hello.txt" );
         InputStream input1 = new  FileInputStream(file1);
         InputStream input2 = new  FileInputStream(file2);
         OutputStream output = new  FileOutputStream(file3);
         // 合并流
         SequenceInputStream sis = new  SequenceInputStream(input1, input2);
         int  temp = 0 ;
         while ((temp = sis.read()) != - 1 ){
             output.write(temp);
         }
         input1.close();
         input2.close();
         output.close();
         sis.close();
     }
}

 

 

【运行结果】

 

结果会在hello.txt文件中包含hello1.txthello2.txt文件中的内容。

 

转自:http://www.cnblogs.com/rollenholt/archive/2011/09/11/2173787.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值