美文网首页
DAY14Java语言入门基础教学8 文件

DAY14Java语言入门基础教学8 文件

作者: 辞cccc | 来源:发表于2019-08-15 21:07 被阅读0次

    创建文件

            //G:\javastudy\day1\src\main\java\day8
            String path ="G:/javastudy/day1/src/main/java/day8";
            File file = new File(path.concat("/1.text"));
            if(file.exists() == false){
                    file.createNewFile();
            }
    

    读取文件的内容

    I/O流
    流 统一管理数据的写入和读取
    流的方向 参考的是自己的内存空间

    1. 输出流
    • 从内存空间将数据写到外部设备
    • 开发者只需要将内存里面的数据写到流里面
    • OutputStream字节流Writer字符流
    1. 输入流
    • 将外部数据写到内存中
    • 从流里面读出数据
    • InputStream字节流Reader字符流
      PS
    1. I/O流对象不属于内存对象 需要自己关闭
    2. OutputStream和InputStream 都是抽象类 不能直接使用
      //FileOutputStream/FileInputStream
      //ObjectOutoutStream/ObjectInputStream
      //FileWriter/FileReader

    向文件写入数据

            //1.创建文件输出流对象
            FileOutputStream fos = new FileOutputStream(file);
    
            //2.调用write方法写入
            byte[] text = {'1','2','3','4'};
            fos.write(text);
    
            //3.操作完毕需要关闭stream对象
            fos.close();
    
            //向文件写入数据-字符流
            FileWriter fw = new FileWriter(file);
            char[] name = {'安','卓'};
            fw.write(name);
    
            fw.close();
    

    读取内容

            FileInputStream fis = new FileInputStream(file);
    
            byte[] name = new byte[12];
            int count = fis.read(name);
            fis.close();
            System.out.println(count+" "+new String(name));
            FileReader fr = new FileReader(file);
            char[] book = new char[4];
            count = fr.read(book);
            fr.close();
            System.out.println(count+" "+new String(book));
    

    向文件里面存一个对象

    1. 序列化 serializable
    2. 保存的对象必须实现Serializable接口
    3. 如果对象内部还有属性变量是其他类的对象 这个类也必须实现Serializable接口
            Person xw = new Person();
            xw.name = "小王";
            xw.age = 20;
    
            OutputStream os = new FileOutputStream(file);
            ObjectOutputStream oos = new ObjectOutputStream(os);
            oos.writeObject(xw);
            oos.close();
    

    从文件里面读取一个对象

            InputStream is = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(is);
            Person xw = (Person) ois.readObject();
    
            System.out.println(xw.name+" "+xw.age);
    
            ois.close();
    

    将一个文件copy到另外一个位置

            //1.源文件的路径
            String sourcePath = "C:\\Users\\hp\\Desktop\\TIM图片20190811082728.jpg";
            //2.目标文件的路径
            String desPath = "G:\\javastudy\\day1\\src\\main\\java\\day8\\1.jpg";
            //3. 图片 字节
            FileInputStream fis = new FileInputStream(sourcePath);
            FileOutputStream fos = new FileOutputStream(desPath);
            byte[] in =new byte[1024];
            while(true){
                int count = fis.read(in);
                if(count!= -1){
                    //读取到内容了
                    //将这一次读取的内容写入目标文件
                    fos.write(in,0,count);
                }else {
                    break;
                }
            }
            fis.close();
            fos.close();
    

    体会

    听课的时候感觉挺简单,但是自己实操还是不太行,拿到一个程序还是有点无从下手的感觉,尤其是关于主程序如何写这方面

    相关文章

      网友评论

          本文标题:DAY14Java语言入门基础教学8 文件

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