Java学习-I/O流

I/O也称为Input/Output,即输入和输出,指的是某个设备或环境进行数据的输入或者输出。例如:键盘就是输入设备,输入用户需要打的信息,再比如显示器就是输出设备,输出用户需要的图像信息。对于java来说,I/O就是输入输出问题,java将它抽象成流对象来解决。

其通常根据2方面划分:

1.根据IO流在java中的输入输出角度分为输入流和输出流

2.根据IO流在java中的数据的角度分为字节流和字符流:

字符流;文本,我们能用文本打开后读的懂的都可以认为是字符流。比如:文章,java文件等等

字节流:二进制的数据,这种数据一般用文本打开我们读不懂。比如,图片文件,mp3文件,等等。

字符流写一个hello文件:


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

public class IOTest {
    public static void main(String[] args) {
        //创建一个文件
        File file = new File("test.txt");
        Writer writer = null;//字符流的输出流的类为Write,默认为空

        try {
            //IO流是需要关闭的,如果不这样设计就会不能关闭资源
            writer = new FileWriter(file);
            writer.write("Hello,World");

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //判断writer不是空防止空指针异常
            if(writer != null) {
                try {
                    //在关闭前会做flush的事情
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

读取这个文件

import java.io.*;

public class ReaderDemo {


    public static void main(String[] args) {
        File file = new File("c.txt");
        //字符流的输入流的类名为Reader
        Reader reader = null;

        try {
            //创建出入流对象FileReader
            reader = new FileReader(file);
            //读取数据, 读取的字符被转换成了ascii码,单个字符读取
            int c = reader.read();
            System.out.println((char)c);
             c = reader.read();
            System.out.println((char)c);
            //如果读取到的是-1后面就没有字符了
            c = reader.read();
            System.out.println(c);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            if(reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}

字符流创建输入输出对象:


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class IOByteInputDemo {


    public static void main(String[] args) {

        //创建字符输入流的对象
        InputStream in = null;

        try {
            in = new FileInputStream("a.txt");
            int r = in.read();
            System.out.println((char)r);


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
import java.io.*;
import java.util.stream.Stream;

public class IOByteDemo {

    public static void main(String[] args) {
        //创建字节输出流
        OutputStream out = null;
        try {
            out = new FileOutputStream(new File("a.txt"));
            //字节流不需要flush
            out.write(98);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(out != null){
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值