2019-08-15 Day14 java学习8

File

  • 文件的相关操作
  • 是否存在 创建文件 写入数据 读取内容

技术的使用

        String path = "D:\\OS\\***************";
        File file = new File(path.concat("/1.txt"));
        if (file.exists() == false) {
            //不存在就创建
            file.createNewFile();}//创建文件夹

知识点补充

concat

concat() 方法用于连接两个或多个数组。
该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本。
语法

arrayObject.concat(arrayX,arrayX,......,arrayX)

————————————————————————————

I / O 流

流: 统一管理数据的写入和读取
流的方法:参考的是自己的内存空间
输出流:从内存空间将数据写到外部设备(磁盘\硬盘\光盘) 开发者只需要将内存里面的数据写到流里面
输入流:将外部数据写到内存中 从流里面读取数据

输出流:OutputStream字节流 Writer字符流
输入流:InputStream字节流 Reader字符流

I/O流对象 不属于内存对象 需要自己关闭

  • OutputStream和InPutStream都是抽象类 不能直接使用
  • FileOutputStream/FileInputStream
  • ObjectOutputStream/ObjectInputStream
  • FileWriter/FileReader

技术的使用

向文件写入数据
字节流
 //1.创建文件输出流对象
FileOutputStream fos = new FileOutputStream(file);
//此处file为上文创建的file,读者使用时需自行创建
//2.调用write方法写入
byte[] text = {'1','2','3','4'};
fos.write(text);
//操作完毕需要关闭Stream对象
fos.close();
字符流
FileWriter fw = new FileWriter(file);
char [] name = {'安','卓','开','发'};
fw.write(name);
fw.close();
读取内容
FileInputStream fis = new FileInputStream(file);
byte[] name1 = new byte[12];
int count = fis.read(name1);
fis.read(name1);
fis.close();
System.out.println(count+" "+new String(name1));
FileReader fr = new FileReader(file);
char[] book = new char[4];
count = fr.read(book);
fr.close();
向文件里面存一个对象
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\\********";

        //2.目标文件的路径
        String desPath = "D:\\OS\\*******";

        //3.图片 字节
        FileInputStream fis = new FileInputStream(sourcePath);
        FileOutputStream fos = new FileOutputStream(desPath);

        byte[] in = new byte[1024];
        int count = 0;
        while ((count = fis.read(in)) != -1){
            fos.write(in,0,count);
        }
//        while (true) {
//            int count = fis.read(in);
//            if(count != -1){
//                //读取到内容了
//                //将这一次读取的内容写入目标文件
//                fos.write(in,0,count);
//            }else{
//                break;
//            }
//        }

        fis.close();
        fos.close();

        //为了提高效率用 Buffered
        String sourcePath = "C:\\Users\\*******";
        String desPath = "D:\\OS\\*********";
        InputStream is = new FileInputStream(sourcePath);
        BufferedInputStream bis = new BufferedInputStream(is);
        OutputStream os = new FileOutputStream(desPath);
        BufferedOutputStream bos = new BufferedOutputStream(os);
        byte[] in = new byte[1024];
        int count = 0;
        long start = System.currentTimeMillis();
        while ((count = bis.read(in)) != -1){
            bos.write(in,0,count);
        }
        bis.close();
        bos.close();

        long end = System.currentTimeMillis();
        System.out.println(end-start);

知识点补充

序列化 serializable
  • 保存的对象必须实现Serializable接口
  • 如果对象内部还有属性变量是其他类的对象,这个类也必须实现Serializable接口
    ———————————————————————————

随笔

学习的进程又拉进了一大步,虽然进步的很慢,但每天都有所收获。即使每天上课都要从可怕的太阳底下走过,经过叫个不停,实属噪音的虫群,每天的进步也让人觉得欣喜。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。