Java IO流-InputStream和OutputStream

Java IO流-InputStream和OutputStream


InputStream是输入流  OutputStream是输出流;

InputStream输入流可以把文件从硬盘读取到内存;

OutputStream输出流可以把文件从内存写入到硬盘;


我们实际使用的都是InputStream和OutputStream的子类;

比如文件操作方面用的是FileInputStream和FileOutputStream;

我们给下实例,视频教程里会详细讲解:

准备工作,我们在C盘建一个txt文件 测试文件.txt

随便加点内容:

QQ鎴浘20170107104246.jpg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package  com.java1234.chap10.sec03;
 
import  java.io.File;
import  java.io.FileInputStream;
import  java.io.InputStream;
 
public  class  Demo1 {
 
     public  static  void  main(String[] args)  throws  Exception {
         File file= new  File( "C://测试文件.txt" );
         InputStream inputStream= new  FileInputStream(file);   // 实例化FileInputStream
         byte  b[]= new  byte [ 1024 ];
         int  len=inputStream.read(b);
         inputStream.close();  // 关闭输入流
         System.out.println( "读取的内容是:" + new  String(b, 0 ,len));
     }
}

把文件从硬盘读取到内存,并且输出:

运行结果:

读取的内容是:我是人 www.java1234.com


上面那个是定义了固定字节数组 一批读取的,我们现在改进下,获取文件长度,然后定义指定字节数组的长度;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package  com.java1234.chap10.sec03;
 
import  java.io.File;
import  java.io.FileInputStream;
import  java.io.InputStream;
 
public  class  Demo2 {
 
     public  static  void  main(String[] args)  throws  Exception {
         File file= new  File( "C://测试文件.txt" );
         InputStream inputStream= new  FileInputStream(file);   // 实例化FileInputStream
         int  fileLength=( int )file.length();
         byte  b[]= new  byte [fileLength];
         inputStream.read(b);
         inputStream.close();  // 关闭输入流
         System.out.println( "读取的内容是:" + new  String(b));
     }
}

运行结果也一样;


我们再来一种方式 一个字节一个字节读取;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package  com.java1234.chap10.sec03;
 
import  java.io.File;
import  java.io.FileInputStream;
import  java.io.InputStream;
 
public  class  Demo3 {
 
     public  static  void  main(String[] args)  throws  Exception {
         File file= new  File( "C://测试文件.txt" );
         InputStream inputStream= new  FileInputStream(file);   // 实例化FileInputStream
         int  fileLength=( int )file.length();
         byte  b[]= new  byte [fileLength];
         int  temp= 0 ;
         int  len= 0 ;
         while ((temp=inputStream.read())!=- 1 ){
             // 一个字节一个字节读取,放到b字节数组里
             b[len++]=( byte )temp;
         }
         inputStream.close();  // 关闭输入流
         System.out.println( "读取的内容是:" + new  String(b));
     }
}

运行结果还是一样的;



下面是讲下输出流;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package  com.java1234.chap10.sec03;
 
import  java.io.File;
import  java.io.FileOutputStream;
import  java.io.OutputStream;
 
public  class  Demo4 {
 
     public  static  void  main(String[] args)  throws  Exception {
         File file= new  File( "C://测试文件.txt" );
         OutputStream out= new  FileOutputStream(file);
         String str= "你好,我好,大家好,Java好" ;
         byte  b[]=str.getBytes();
         out.write(b);  //  将b字节数组写入到输出流
         out.close();   // 关闭输出流
     }
}

我们把指定文件写入到文件;


上面那种是直接覆盖的,我们再来一个追加的;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package  com.java1234.chap10.sec03;
 
import  java.io.File;
import  java.io.FileOutputStream;
import  java.io.OutputStream;
 
public  class  Demo5 {
 
     public  static  void  main(String[] args)  throws  Exception {
         File file= new  File( "C://测试文件.txt" );
         OutputStream out= new  FileOutputStream(file, true );
         String str= "你好,我好,大家好,Java好" ;
         byte  b[]=str.getBytes();
         out.write(b);  //  将b字节数组写入到输出流
         out.close();   // 关闭输出流
     }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值