java之IO流(一)

前言

1:流

在Java IO中,流是一个核心的概念。流从概念上来说是一个连续的数据流。你既可以从流中读取数据,也可以往流中写数据。流与数据源或者数据流向的媒介相关联。在Java IO中流既可以是字节流(以字节为单位进行读写),也可以是字符流(以字符为单位进行读写)。

2:IO最典型的数据源和目标媒介:
文件 管道 网络连接 内存缓存 System.in, System.out, System.error(注:Java标准输入、输出、错误输出)

从是读媒介还是写媒介的维度看,Java IO可以分为:

输入流:InputStream和Reader
输出流:OutputStream和Writer
而从其处理流的类型的维度上看,Java IO又可以分为:

字节流:InputStream和OutputStream
字符流:Reader和Writer

我们的程序需要通过InputStream或Reader从数据源读取数据,然后用OutputStream或者Writer将数据写入到目标媒介中。其中,InputStream和Reader与数据源相关联,OutputStream和writer与目标媒介相关联。 以下的图说明了这一点:
在这里插入图片描述
3:Java IO的基本用法

字节流写文件

  public static void writeByteToFile() throws IOException{
        String hello= new String( "hello word!");
         byte[] byteArray= hello.getBytes();
        File file= new File( "d:/test.txt");
         //因为是用字节流来写媒介,所以对应的是OutputStream 
         //又因为媒介对象是文件,所以用到子类是FileOutputStream
        OutputStream os= new FileOutputStream( file);
         os.write( byteArray);
         os.close();
  }

字节流读文件

public static void readByteFromFile() throws IOException{
        File file= new File( "d:/test.txt");
         byte[] byteArray= new byte[( int) file.length()];
         //因为是用字节流来读媒介,所以对应的是InputStream
         //又因为媒介对象是文件,所以用到子类是FileInputStream
        InputStream is= new FileInputStream( file);
         int size= is.read( byteArray);
        System. out.println( "大小:"+size +";内容:" +new String(byteArray));
         is.close();
  }

字符流读文件

public static void writeCharToFile() throws IOException{
        String hello= new String( "hello word!");
        File file= new File( "d:/test.txt");
         //因为是用字符流来读媒介,所以对应的是Writer,又因为媒介对象是文件,所以用到子类是FileWriter
        Writer os= new FileWriter( file);
         os.write( hello);
         os.close();
  }

字符流写文件

  public static void readCharFromFile() throws IOException{
        File file= new File( "d:/test.txt");
         //因为是用字符流来读媒介,所以对应的是Reader
         //又因为媒介对象是文件,所以用到子类是FileReader
        Reader reader= new FileReader( file);
         char [] byteArray= new char[( int) file.length()];
         int size= reader.read( byteArray);
        System. out.println( "大小:"+size +";内容:" +new String(byteArray));
         reader.close();
  }

字节流转换为字符流

---------------------------------------------------------------
注:我这有个学习Python基地,里面有很多学习资料,感兴趣的+Q群:895817687
---------------------------------------------------------------

public static void convertByteToChar() throws IOException{
        File file= new File( "d:/test.txt");
         //获得一个字节流
        InputStream is= new FileInputStream( file);
         //把字节流转换为字符流,其实就是把字符流和字节流组合的结果。
        Reader reader= new InputStreamReader( is);
         char [] byteArray= new char[( int) file.length()];
         int size= reader.read( byteArray);
        System. out.println( "大小:"+size +";内容:" +new String(byteArray));
         is.close();
         reader.close();
  }

未完待续

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值