Java入门—输入输出流

File类的使用

文件是:文件可认为是相关记录或放在一起的数据的集合。
Java中,使用java.io.File类对文件进行操作

public class FileDemo {
    public static void main(String[] args) {
        String path = "E:\\pdd";
        File f = new File(path);
        //判断是文件还是目录
        System.out.println(f.isFile());
        System.out.println(f.isDirectory());
    }
}
image.png

对于File类还有其他很多方法的使用,建议在使用时进行查询文档。

字节流

  • 字节输入流 :InputStream (读)
  • 字节输出流 :OutputStream (写)


    image.png
image.png

FileInputStream
  • 读取诸如图像数据的原始字节流,如图片,文件中的字节。


    image.png
image.png
public class FileDemo {
    public static void main(String[] args) {
        String path = "";

        // FileInputStream,
        try {
            FileInputStream fs = new FileInputStream(path);
            try {
                
                int n = fs.read();
                System.out.print((char)n);
                fs.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

代码中注意异常的继承关系,外层异常不能为内层异常的子类。

FileOutputStream

将数据写入文件中。


image.png
image.png
缓冲流
  • BufferedInputStream
  • BufferedOutputStream
    缓冲流,当缓冲区满了,提交,减少频繁写入操作。
public class FileDemo {
    public static void main(String[] args) {
        String path = "";
        try {
            FileOutputStream fos = new FileOutputStream(path);
            BufferedOutputStream bos = new BufferedOutputStream(fos);

            FileInputStream fis = new FileInputStream(path);
            BufferedInputStream bis = new BufferedInputStream(fis);

            bos.write(50);
            bos.write('a');
            //提交数据到文件
            bos.flush();
            bos.close();

            System.out.println(bis.read());
            bis.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

字符流

  • 字符输入流 : Reader
  • 字符输出流 : Writer
字节字符转换流
  • InputStreamReader 输入
  • OutputStreamWriter 输出

读写数据时保持编码一致。

对象的序列化和反序列化

对象序列化步骤:

  1. 创建一个类继承Serializable接口
  2. 创建对象
  3. 处理对象(写文件,发http,入库等)
  4. 读取对线
  • ObjectInputStream 对象输入流
  • ObjectOutputStream 对象输出流

示例:
新建一个Goods.java文件,定义一个Goods类并继承Serializable接口

public class Goods implements Serializable{
    private String goodsId;
    private String goodsName;
    private double price;

    @Override
    public String toString() {
        return "Goods{" +
                "goodsId='" + goodsId + '\'' +
                ", goodsName='" + goodsName + '\'' +
                ", price=" + price +
                '}';
    }

    public Goods(String goodsId, String goodsName, double price){
        this.goodsId = goodsId;
        this.goodsName = goodsName;
        this.price = price;
    }

    public String getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(String goodsId) {
        this.goodsId = goodsId;
    }

    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}

再建一个测试入口文件名为GoodsTest.java

public class GoodsTest {
    public static void main(String[] args) {
        String path = "1.txt";
        Goods gd = new Goods("001", "allen", 6.0);

        try {
            FileOutputStream fos = new FileOutputStream(path);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(gd);
            oos.writeBoolean(true);
            oos.flush();
            oos.close();
            System.out.println("-----------------------------------");
            FileInputStream fis = new FileInputStream(path);
            ObjectInputStream ois = new ObjectInputStream(fis);
            try {
                Goods gt = (Goods)ois.readObject();
                boolean b = ois.readBoolean();
                System.out.println(gt);
                System.out.println(b);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            ois.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

对名为1.txt的文件进行类的存储和读取。

注:养成看官方文档的习惯,因为毕竟有些方法用的频率很少,当你使用时去查阅即可,文章中关于类的方法部分没有详情,请查阅官方手册,手册中写的很清楚。

如果文章对你有帮助记得点赞~,关注作者第一时间获得最新更新~~。
祝好~~~

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 概述 java.io 包几乎包含了所有操作输入、输出需要的类。所有这些流类代表了输入源和输出目标。java.io ...
    Steven1997阅读 9,402评论 1 25
  • 明天呢,是中国的传统节日,也是一个虐狗的日子,七夕情人节!我想这个,让中国更多男男女女期待的日子,或是单身狗追逐了...
    虚兔阅读 514评论 0 2
  • (4) 2012年,高考还是文理分科,也是从1977年恢复高考实行文理分科以来最后第二个年头,我就是那年上的大学,...
    叶灵龙阅读 303评论 0 0
  • 有时候我真的很讨厌现在的自己!感觉我在用力的表演,向世人展示,让所有人都觉得我多么幸福。 爱情已经苟延残喘,几乎随...
    十一月_7b49阅读 185评论 0 0
  • 曾经非常热爱文字的优美,相信书中可以看到黄金屋颜如玉,也爱让自己的感受在笔尖流淌 也仍在享受他人文字的美和救赎,感...
    城门立雪阅读 214评论 0 0

友情链接更多精彩内容