zoukankan      html  css  js  c++  java
  • java,io流

    java里面的io流是操作文件读写的一门技术。

    I:in 指的是输入流,读取文件内容到流对象

    O:out 指的是输出流,写文件内容到流对象

    流指的是对象。

    字节流常用api:

    copy文件的dome如下:

    package com.mg.java.maven.day01;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    /**
     * io流
     * 
     * @author admin
     *
     */
    public class FileCopy {
        public static void main(String[] args) throws IOException {
            String filepath = "C:\Users\admin\Desktop\装饰器.png";
            String topath = "C:\Users\admin\Desktop\装饰器2.png";
            FileCopy file = new FileCopy();
            file.fileCopy(filepath, topath);
    //        fileCopy(filepath, topath);
        }
    
        public void fileCopy(String filepath, String topath) throws FileNotFoundException, IOException {
            InputStream inputStream = new FileInputStream(new File(filepath));
            OutputStream outputStream = new FileOutputStream(new File(topath));
            int size = 0;
            while ((size = inputStream.read()) != -1) {
                outputStream.write(size);
            }
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
            System.out.println("success");
        }
    }

    静态方法调用不需要创建对象

    package com.mg.java.maven.day01;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    /**
     * io流
     * 
     * @author admin
     *
     */
    public class FileCopy02 {
        public static void main(String[] args) throws IOException {
            String filepath = "C:\Users\admin\Desktop\装饰器.png";
            String topath = "C:\Users\admin\Desktop\装饰器2.png";
            FileCopy02.fileCopy("C:\Users\admin\Desktop\装饰器.png", "C:\Users\admin\Desktop\装饰器2.png");
        }
    
        public static void fileCopy(String filepath, String topath) throws FileNotFoundException, IOException {
            InputStream inputStream = new FileInputStream(new File(filepath));
            OutputStream outputStream = new FileOutputStream(new File(topath));
            int size = 0;
            while ((size = inputStream.read()) != -1) {
                outputStream.write(size);
            }
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
            System.out.println("success");
        }
    }
  • 相关阅读:
    C#内建接口:IComparable
    C#内建接口:IEnumerable
    WPF中使用资源
    WPF中的触发器(Trigger)
    一文详解 | 开放搜索兼容Elasticsearch做召回引擎
    阿里云李飞飞:中国数据库的时与势
    如何构建流量无损的在线应用架构 | 专题开篇
    如何构建一个流量无损的在线应用架构 | 专题中篇
    多任务学习模型之ESMM介绍与实现
    云原生时代的运维体系进化
  • 原文地址:https://www.cnblogs.com/xiamaojjie/p/12210271.html
Copyright © 2011-2022 走看看