黑马程序员--java高级编程输入输出流(各种节点流类)(2)

3.各种节点流类
   1)理解流的概念
    流是字节序列的抽象概念。还可以理解为:是同一台计算机或网络中不同计算机之间有序运动的数据序列。
    数据序列:可以是原始的二进制字节数据,也可以是经过特定编码处理的有格式的数据

    文件是数据的静态存储形式,而流是指数据传输时的形态。

   2)InputStream类和OutputStream类

    InputStream类用来描述所有输入流的抽象概念
    InputStream类中的方法:
      int read()
     
      int read(byte[] b)

     int read(byte[] b,int off,int len)

       long skip(long n)
     
       int available()
   
       void reset()

      OutputStream类用来描述所有输出流的抽象概念

      输出数据的方法:
      void write(int b) throws IOException ;

      void write(byte[] b) throws IOException ;

      void write(byte[] b,int off,int len) throws IOException ;
      关闭输出流:

       public void close() throws IOException;
      清空缓冲区:

       public void flush() throws IOException;


   3)FileInputStream类和FileOutputStream类

    FileInputStream用于顺序访问本地文件。
    它从父类InputStream中继承read()、close()等方法对本机上的文件进行操作,但不支持mark()方法和reset()方法。
  
    构造方法
       public FileInputStream(string name) throws FileNotFoundException

       public FileInputStream(File file) throws FileNotFoundException
   读取字节的方法
     public int read() throws IOException

     public int read(byte[] b)throws IOException

     public int read(byte[] b,int off,int len) throws IOException

 
   FileOutputStream类用于向一个文件写数据。它从超类OutputStream中继承write()、close()等方法。

  构造方法
     public FileOutputStream(String name) throws FileNotFoundException

     public FileOutputStream(File file) throws FileNotFoundException

     public FileOutput.Stream (String name,boolean append) throws FileNotFoundException

     写入字节的方法
        public void write(int b) throws IOException

        public void write(byte[] b) throws IOException

        public void write(byte[] b,int off,int len) throws IOException

 


例题:打开文件
import java.io.*;
public class ReadFileTest
{
   public static void main(String[] args) throws IOException
   {
   try
   {
   //创建文件输入流对象
   FileInputStream fis =new FileInputStream("ReadFileTest.java");       
         int n=fis.available();       
         byte b[]=new byte[n];       
         //读取输入流        
         while((fis.read(b,0,n))!=-1)   
         {                   
          System.out.print(new String(b));        
         }
         System.out.println();
         //关闭输入流
         fis.close();   
      }
      catch(IOException ioe)
      {
         System.out.println(ioe.getMessage());
      }

   4)Reader类与Writer类
 
       Reader类与Writer类 是所有字符流类的抽象基类,用于简化对字符串的输入输出编程
    
       用法:和FileInputStream类和FileOutputStream类 都差不多

   5)PipedInputStream类与PipdeOutputStream类

     PipedInputStream类与PipdeOutputStream类用于在应用程序中的创建管理通信

   6)ByteArrayInputStream类与ByteArrayOutputStream类

      ByteArrayInputStream类与ByteArrayOutputStream类用于以IO流方式来完成对字节
                   数组内容的读写, 来支持类似内存虚拟文件

编程举例:  编写一个把输入流中所有英文字母变成大写字母,然后将结果写入到一个输出流对象。
            用这个函数来将一个字符串中的所有字符转换成大写的。


import java.io.*;

public class ByteArrayTest {
 
 public static void main(String[] args) throws IOException{
  //定义一个字符串
  String tmp = "abcdefghijklmnaefd";
  //定义一个字节数组,转成字节
  byte[]src = tmp.getBytes();
  //创建ByteArrayInputStream  输入流对象
  ByteArrayInputStream input = new ByteArrayInputStream(src);
  //创建ByteArrayOutputputStream  输出流对象
  ByteArrayOutputStream output =new ByteArrayOutputStream();
  //调用方法
  transform(input,output);
  
  byte[] result = output.toByteArray();
  
  System.out.print(new String(result));
  
 }

 public static void transform(InputStream in,OutputStream out) throws IOException{
  
       int ch =0;
       while((ch=in.read())!=-1){
       int upperCh =Character.toUpperCase((char)ch);
      
       out.write(upperCh);
       }
  }
 }

 


    重视:IO程序代码的复用
 
    1.System.in连接到键盘,是InputStream类型的实例对象,System.out连接到显示器,是PrintStream类的实例对象。

    2.不管各种底层物理设备用什么方式实现数据的终止点,InputStream的read方法总是返回-1来表示输入流的结束。

    3.在Windows下,按下Ctrl+Z组合键可以产生键盘输入流的结束标记,在Linux下,按下Ctrl+D组合

 

    编程举例:借助刚编写的函数,将键盘上输入的内容转变成大写字母后打印在屏幕上


import java.io.*;

public class ByteArrayTest {
 
 public static void main(String[] args) throws IOException{
  //定义一个字符串
  String tmp = "abcdefghijklmnaefd";
  //定义一个字节数组,转成字节
  byte[]src = tmp.getBytes();
  //创建ByteArrayInputStream  输入流对象
  ByteArrayInputStream input = new ByteArrayInputStream(src);
  //创建ByteArrayOutputputStream  输出流对象
  ByteArrayOutputStream output =new ByteArrayOutputStream();
  //调用方法
  //transform(input,output);
  
  transform(System.in,System.out);//修改这一行就可以了
  
  byte[] result = output.toByteArray();
  
  System.out.print(new String(result));
  
 }

 public static void transform(InputStream in,OutputStream out) throws IOException{
  
       int ch =0;
       while((ch=in.read())!=-1){
       int upperCh =Character.toUpperCase((char)ch);
      
       out.write(upperCh);
       }
  }
 }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值