美文网首页自学Java入门与进阶
Java入门—输入输出流

Java入门—输入输出流

作者: kakarotto | 来源:发表于2018-11-13 18:31 被阅读8次

    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的文件进行类的存储和读取。

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

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

    相关文章

      网友评论

        本文标题:Java入门—输入输出流

        本文链接:https://www.haomeiwen.com/subject/txqlfqtx.html