IO----字节流和字符流

IO中的输入输出流就像搬家公司搬家,有个标准的流程。这个流程有这样四步:

1、先搬家首先得有个家不是。这就是创建一个源;

2、打电话找一个搬家公司,摇人。这就是选择一个流;

3、好了你们开始搬吧。这就是操作过程,读或者写;

4、搬完了给钱打发走人,别在这站我地了。这就是close() 释放资源。

以下是InputScream和OutputScream两大抽象类的常用方法。具体细节可以参考API。

InputStream类的常用方法:

1. available() 方法,获取与之关联的文件剩余可读的字节数。
2. int read() 方法,读取输入流。读取输入流的下一个字节,返回一个0-255之间的int类型整数。如果到达流的末端,返回-1。
3. int read(byte[] b) 方法,读取输入流。读取多个字节,存入字节数组b,返回实际读入的字节数。如果到达流的末端,返回-1。
4. int read (byte[] b, int off, int len); 方法,读取输入流。每次读取len个字节,存入字节数组b,从off下标开始存储。如果到达流的末端,返回-1。
5. close() 方法,关闭当前流,释放与该流相关的资源,防止资源泄露。在带资源的try语句中将被自动调用。关闭流之后还试图读取字节,会出现IOException异常。

OutputStream类的常用方法

1. write (int b); 将指定的字节写入此输出流。
2. write(byte[] byte); 将 b.length 个字节从指定的 byte 数组写入此输出流。
3. write(byte[] byte, int off, int len); 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。
4. flush();  用于清空缓存里的数据,并通知底层去进行实际的写操作。(强制把缓存区里面的数据写入到文件)
5. close();关闭当前流,释放与该流相关的资源。

ps:进行写操作时,如果文件不存在则自动创建该文件。
————————————————————————————————————————————————————————

IO.txt文件创建在项目目录下。

一:字节输入流和字节输出流

我们需要使用字节数组来进行操作。

字节输入流

package cn.test.io;

import java.io.*;

public class IOtest01 {
    public static void main(String[] args) {
        //1、创建源
        File src=new File("IO.txt");

        //2、选择流
        InputStream is=null;
        try {
            is=new FileInputStream(src);

            //3、操作
            byte[] flush=new byte[1024];
            int len=-1;
            while((len=is.read(flush))!=-1){
                String str=new String(flush,0,len);
                System.out.print(str);
              }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (null!=is){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                ;
            }
        }


    }
}

 

字节输出流:

package cn.test.io;

import java.io.*;

/**
 *
 * 字节输出流
 */

public class IOtest02 {
    public static void main(String[] args) {

        //1、创建源
        File dest=new File("IO2.txt");

        //2、选择流
        OutputStream os=null;
        try {
            os=new FileOutputStream(dest);
            String msg="this is FileOutputScream";

            //3、操作
            byte[] datas=msg.getBytes();  //进行编码
            os.write(datas,0,datas.length);
            os.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(null!=os){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

二:字符输入流和字符输出流

字符输入流

package cn.test.io;

import java.io.*;

/**
 * 字符输入流
 *
 */
public class IOtest03 {
    public static void main(String[] args) {

        //1、创建源
        File src=new File("IO3.txt");

        //2、选择流

        Reader reader=null;
        try {
            reader=new FileReader(src);
            //3、操作
            char[] flush=new char[1024];
            int len=-1;
            while ((len = reader.read(flush)) != -1) {
                String str=new String(flush,0,len);
                System.out.print(str);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(null!=reader){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        
    }
}

字符输出流

package cn.test.io;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

/**
 * 字符输出流
 *
 */
public class IOtest04 {
    public static void main(String[] args) {

        //1、创建源
        File dest=new File("IO4.txt");

        //2、选择流

        Writer wr=null;
        try {
            wr=new FileWriter(dest);
            String msg="醉后不知天在水,满船清梦压星河";

            //3、操作
            char[] datas=msg.toCharArray();
            wr.write(datas,0,datas.length);
            wr.flush();

            /*也可以不使用数组
            直接 :
            String msg="醉后不知天在水,满船清梦压星河";
            wr.writer(msg);
            wr.close()
             */
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            //4、释放资源
            if(null!=wr){
                try {
                    wr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值