Java 使用(inputStrem,OutputStrem)

Java 使用(inputStrem,OutputStrem),java,Java,我想,如果你试图复制到目录,会显示一条消息,程序会关闭。对于文件,显示文件大小和上次修改的时间。我不太清楚,如何显示文件大小,以及上次修改的文件。 徖 import java.io.*; public class KopeeriFail { private static void kopeeri(String start, String end) throws Exception { InputStream sisse = new FileInputStream(st

我想,如果你试图复制到目录,会显示一条消息,程序会关闭。对于文件,显示文件大小和上次修改的时间。我不太清楚,如何显示文件大小,以及上次修改的文件。 徖

import java.io.*;

public class KopeeriFail {

    private static void kopeeri(String start, String end) throws Exception {
        InputStream sisse = new FileInputStream(start);
        OutputStream välja = new FileOutputStream(end);
        byte[] puhver = new byte[1024];
        int loetud = sisse.read(puhver);
        while (loetud > 0) {
            välja.write(puhver, 0, loetud);
            loetud = sisse.read(puhver);
        }
        sisse.close();
        välja.close();
    }

    public static void main(String[] args) throws Exception {
        if (args.length != 1) {
            System.out.println("Did you gave name to the file");
            System.exit(1);
        }
        kopeeri(args[0], args[0] + ".copy");
    }
}

您可以轻松获取存储大小和上次修改时间戳的
BasicFileAttributes

public static void main(String[] args) throws IOException {
    if (args.length != 1) {
        System.err.println("Specify file name");
        return;
    }

    Path initial = Paths.get(args[0]);
    if (!Files.exists(initial)){
        System.err.println("Path is not exist");
        return;
    }

    if (Files.isDirectory(initial)) {
        System.err.println("Path is directory");
        return;
    }

    BasicFileAttributes attributes = Files.
            readAttributes(initial, BasicFileAttributes.class);
    System.out.println("Size is " + attributes.size() + " bytes");
    System.out.println("Last modified time " + attributes.lastModifiedTime());

    Files.copy(initial, initial.getParent()
            .resolve(initial.getFileName().toString() + ".copy"));
}
希望有帮助

部分重复-