java中的IO整理(3)

字符流

 

【向文件中写入数据】

 

现在我们使用字符流

 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
  * 字符流
  * 写入数据
  * */
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);
         Writer out = new  FileWriter(f);
         String str= "hello" ;
         out.write(str);
         out.close();
     }
}

 

 

当你打开hellotxt的时候,会看到hello

 

其实这个例子上之前的例子没什么区别,只是你可以直接输入字符串,而不需要你将字符串转化为字节数组。

 

当你如果想问文件中追加内容的时候,可以使用将上面的声明out的哪一行换为:

 

Writer out =new FileWriter(f,true);

 

这样,当你运行程序的时候,会发现文件内容变为:

 

hellohello如果想在文件中换行的话,需要使用“\r\n

 

比如将str变为String str="\r\nhello";

 

这样文件追加的str的内容就会换行了。

 

 

 

从文件中读内容:

 

 

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

 

 

【运行结果】:

 

读入的长度为:17

 

内容为hellohello

 

hello

 

 

 

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

 

 

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

 

 

运行结果:

 

内容为hellohello

 

hello

 

 

关于字节流和字符流的区别

 

实际上字节流在操作的时候本身是不会用到缓冲区的,是文件本身的直接操作的,但是字符流在操作的 时候下后是会用到缓冲区的,是通过缓冲区来操作文件的。

 

读者可以试着将上面的字节流和字符流的程序的最后一行关闭文件的代码注释掉,然后运行程序看看。你就会发现使用字节流的话,文件中已经存在内容,但是使用字符流的时候,文件中还是没有内容的,这个时候就要刷新缓冲区。

 

使用字节流好还是字符流好呢?

 

答案是字节流。首先因为硬盘上的所有文件都是以字节的形式进行传输或者保存的,包括图片等内容。但是字符只是在内存中才会形成的,所以在开发中,字节流使用广泛。

 

文件的复制

 

其实DOS下就有一个文件复制功能,比如我们想把d盘下面的hello.txt文件复制到d盘下面的rollen.txt文件中,那么我们就可以使用下面的命令:

 

copy d:\hello.txt d:\rollen.txt

 

 

运行之后你会在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
/**
  * 文件的复制
  * */
import  java.io.*;
class  hello{
     public  static  void  main(String[] args) throws  IOException {
         if (args.length!= 2 ){
             System.out.println( "命令行参数输入有误,请检查" );
             System.exit( 1 );
         }
         File file1= new  File(args[ 0 ]);
         File file2= new  File(args[ 1 ]);
         
         if (!file1.exists()){
             System.out.println( "被复制的文件不存在" );
             System.exit( 1 );
         }
         InputStream input= new  FileInputStream(file1);
         OutputStream output= new  FileOutputStream(file2);
         if ((input!= null )&&(output!= null )){
             int  temp= 0 ;
             while ((temp=input.read())!=(- 1 )){
                 output.write(temp);
             }
         }
         input.close();
         output.close();
     }
}

 

然后在命令行下面

 

javac hello.java

 

java hello d:\hello.txt d:\rollen.txt

 

现在你就会在d盘看到rollentxt了,

 

 

 

 

 

 

OutputStreramWriter InputStreamReader

 

整个IO类中除了字节流和字符流还包括字节和字符转换流。

 

OutputStreramWriter将输出的字符流转化为字节流

 

InputStreamReader将输入的字节流转换为字符流

 

但是不管如何操作,最后都是以字节的形式保存在文件中的。

 

 

 

将字节输出流转化为字符输出流

 

 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
  * 将字节输出流转化为字符输出流
  * */
import  java.io.*;
class  hello{
     public  static  void  main(String[] args) throws  IOException {
         String fileName= "d:" +File.separator+ "hello.txt" ;
         File file= new  File(fileName);
         Writer out= new  OutputStreamWriter( new  FileOutputStream(file));
         out.write( "hello" );
         out.close();
     }
}

 

 

运行结果:文件中内容为:hello

 

将字节输入流变为字符输入流

 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
  * 将字节输入流变为字符输入流
  * */
import  java.io.*;
class  hello{
     public  static  void  main(String[] args) throws  IOException {
         String fileName= "d:" +File.separator+ "hello.txt" ;
         File file= new  File(fileName);
         Reader read= new  InputStreamReader( new  FileInputStream(file));
         char [] b= new  char [ 100 ];
         int  len=read.read(b);
         System.out.println( new  String(b, 0 ,len));
         read.close();
     }
}

 

 

【运行结果】:hello

 

前面列举的输出输入都是以文件进行的,现在我们以内容为输出输入目的地,使用内存操作流

 

ByteArrayInputStream 主要将内容写入内容

 

ByteArrayOutputStream  主要将内容从内存输出

 

使用内存操作流将一个大写字母转化为小写字母

 

 

?
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 str= "ROLLENHOLT" ;
         ByteArrayInputStream input= new  ByteArrayInputStream(str.getBytes());
         ByteArrayOutputStream output= new  ByteArrayOutputStream();
         int  temp= 0 ;
         while ((temp=input.read())!=- 1 ){
             char  ch=( char )temp;
             output.write(Character.toLowerCase(ch));
         }
         String outStr=output.toString();
         input.close();
         output.close();
         System.out.println(outStr);
     }
}

 

 

【运行结果】:

 

rollenholt

 

内容操作流一般使用来生成一些临时信息采用的,这样可以避免删除的麻烦。

 

管道流

 

管道流主要可以进行两个线程之间的通信。

 

PipedOutputStream 管道输出流

 

PipedInputStream 管道输入流

 

验证管道流
?
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
  * 验证管道流
  * */
import  java.io.*;
 
/**
  * 消息发送类
  * */
class  Send implements  Runnable{
     private  PipedOutputStream out= null ;
     public  Send() {
         out= new  PipedOutputStream();
     }
     public  PipedOutputStream getOut(){
         return  this .out;
     }
     public  void  run(){
         String message= "hello , Rollen" ;
         try {
             out.write(message.getBytes());
         } catch  (Exception e) {
             e.printStackTrace();
         } try {
             out.close();
         } catch  (Exception e) {
             e.printStackTrace();
         }
     }
}
 
/**
  * 接受消息类
  * */
class  Recive implements  Runnable{
     private  PipedInputStream input= null ;
     public  Recive(){
         this .input= new  PipedInputStream();
     }
     public  PipedInputStream getInput(){
         return  this .input;
     }
     public  void  run(){
         byte [] b= new  byte [ 1000 ];
         int  len= 0 ;
         try {
             len= this .input.read(b);
         } catch  (Exception e) {
             e.printStackTrace();
         } try {
             input.close();
         } catch  (Exception e) {
             e.printStackTrace();
         }
         System.out.println( "接受的内容为 " +( new  String(b, 0 ,len)));
     }
}
/**
  * 测试类
  * */
class  hello{
     public  static  void  main(String[] args) throws  IOException {
         Send send= new  Send();
         Recive recive= new  Recive();
         try {
//管道连接
             send.getOut().connect(recive.getInput());
         } catch  (Exception e) {
             e.printStackTrace();
         }
         new  Thread(send).start();
         new  Thread(recive).start();
     }
}

 

【运行结果】:

 

接受的内容为 hello , Rollen

 

转自: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、付费专栏及课程。

余额充值