1. 程式人生 > 其它 >java使用IO流複製檔案

java使用IO流複製檔案

java使用IO流複製檔案

IDEA預設的檔案位置是當前專案的根目錄

import com.sun.imageio.spi.InputStreamImageInputStreamSpi;

import java.io.*;

public class IOTestMain {
    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {
            fis = new FileInputStream("st.xlsx");
            fos = new FileOutputStream("aaaa.xlsx");
            byte[] bytes = new byte[1024 * 1024];
            int readCount = 0;
            while ((readCount = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, readCount);
            }
            fos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}