java基础之IO流

IO流概念

输入流:把能够读取一个字节序列的对象称为输入流。
输出流:把能够写一个字节序列的对象称为输出流。
通俗理解:对于初学者,可能常常不清楚何时该用输入流,何时该用输出流。本人将这两个流记为‘’读入写出‘’,那么我就清楚输入流就有read(读)方法,输出流就有write(写)方法。然后,再思考‘’读入‘’从哪读到哪?‘’写出‘’从哪写到哪?这里只需要记住时刻以内存作为参照就行,‘’读入‘’就是从外面(磁盘)往内存中读,用输入流,‘’写出‘’就是从内存往外面写(磁盘),所以用输出流。

一些常用的IO流

1.FileInputStream和FileOutputStream
案例1:将本地文件taofut.txt的内容输出到控制台。
代码:

/**
 * FileInputStream
 * 将本地文件taofut.txt的内容输出到控制台
 */
public class Demo1 {

    public static void main(String[] args) {
        //加载本地文件信息到File
        File file=new File("D://taofut.txt");
        InputStream in=null;
        try {
            //如果本地文件不存在,则创建
            if(!file.exists()){
                file.createNewFile();
            }
            //将文件加载到输入流
            in=new FileInputStream(file);
            byte[] buf=new byte[1024];
            int length=0;
            //字节数组(内存)从输入流中读入数据
            while ((length=in.read(buf))!=-1){
                //字节数组转成字符串格式,输出到控制台
                System.out.println(new String(buf,0,length));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

案例2: FileInputStream和FileOutputStream一起完成文件复制。
代码:

/**
 * FileInputStream和FileOutputStream一起完成文件复制
 */
public class Demo2 {
    public static void main(String[] args) {
        //加载本地文件信息到oldFile
        File oldFile=new File("D://taofut1.txt");
        InputStream in=null;
        OutputStream out=null;
        try {
            //如果本地文件不存在,则创建
            if(!oldFile.exists()){
                oldFile.createNewFile();
            }
            //加载本地文件信息到newFile
            File newFile=new File("D://taofut1_new.txt");
            //将文件加载到输入流
            in=new FileInputStream(oldFile);
            //将文件加载到输出流,文件如果不存在,则自动创建
            out=new FileOutputStream(newFile);
            byte[] buff=new byte[1024];
            int len=0;
            //字节数组(内存)从输入流中读入数据
            while ((len=in.read(buff))!=-1){
                //输出流将数据从字节数组(内存)中写出到文件
                out.write(buff,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(in!=null){
                    in.close();
                }
                if(out!=null){
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

2.字符流InputStreamReader
案例:在控制台输入,将内容写到taofut3.txt中(中文会乱码)
代码:

/**
 * 在控制台输入,将内容写到taofut3.txt中(中文会乱码)
 */
public class Demo3 {

    public static void main(String[] args) {
        File file=new File("D://taofut3.txt");
        OutputStream out=null;
        InputStreamReader reader=null;
        try {
            //定义字符输入流,用来接收控制台输入的内容
            reader=new InputStreamReader(System.in);
            //定义文件输出流,用来将控制台内容写出到taofut3.txt
            out=new FileOutputStream(file);
            int len=0;
            while ((len=reader.read())!=-1){
                //len是字符编码
                out.write(len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(out!=null){
                    out.close();
                }
                if(reader!=null){
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

3.字符缓冲流BufferedReader
案例:控制台输入内容,控制台输出内容。
代码:

/**
 * 控制台输入内容,控制台输出内容
 */
public class Demo4 {

    public static void main(String[] args) {
        BufferedReader bufRead=null;
        BufferedWriter bufWrite=null;
        String line=null;
        try {
            bufRead=new BufferedReader(new InputStreamReader(System.in,"utf-8"));
            bufWrite=new BufferedWriter(new OutputStreamWriter(System.out,"utf-8"));
            //将控制台输入的内容读入BufferedReader中
            while ((line=bufRead.readLine())!=null){
                //将BufferedReader中的内容写出到控制台
                bufWrite.write(line);
                bufWrite.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(bufRead!=null){
                    bufRead.close();
                }
                if(bufWrite!=null){
                    bufWrite.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

代码地址:https://github.com/taofut/java_base_code

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值