Java输入输出(2)--流

1.流的分类。输入、输出都是从程序运行所在内存的角度来划分的。

2.字节流操作的最小数据单元是8位的字节,

   字符流操作的最小数据单元是16位的字符。

3.使用FileInputStream读取自身

  public class FileInputStreamTest

{

  public static void main(String[] args) throws IOException

  {

   //创建字节输入流

    FileInoutStream fis = new FileInputStream("FileInputSteaTest.java");

    byte[] bbuf = new byte[1024];

    int hasRead = 0;

    while((hasRead = fis.read(bbuf)) > 0)

     {

          System.out.println(new String(bbuf,0,hasRead));
     }

  //关闭文件输入流,放在finally块里更安全

    fis.close();

  }

}

4.使用FileReader读取自身

  public class FileReaderTest

{

  public static void main(String[] args) throws IOException

{

   FileReader fr = null;

   try{

      fr = new FileReader("FileReaderTest.java");

      char[]  cbuf = new char[32];

      int  hasRead = 0;  

     while((hasRead =  fr.read(cbuf))>0)

   {

      System.out.println(new String(cbuf,0,hasRead));

   }

  }

catch(IOException ioe)

 {

   ioe.printStackTrace();

 }

finally

{

       if(fr != null)

       {

       fr.close();

      }

 }

 }

 

}

 

 

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值