Example usage for java.nio.file Files size

List of usage examples for java.nio.file Files size

Introduction

In this page you can find the example usage for java.nio.file Files size.

Prototype

public static long size(Path path) throws IOException 

Source Link

Document

Returns the size of a file (in bytes).

Usage

From source file:Test.java

public static void main(String[] args) throws Exception {
    Path zip = Paths.get("/usr/bin/zip");
    System.out.println(Files.size(zip));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Path path = FileSystems.getDefault().getPath("/home/docs/users.txt");
    System.out.println(Files.size(path));
}

From source file:Test.java

public static void main(String[] args) throws Exception {
    Path path = FileSystems.getDefault().getPath("./file2.log");
    System.out.println("File Size:" + Files.size(path));
    System.out.println("Is Directory:" + Files.isDirectory(path));
    System.out.println("Is Regular File:" + Files.isRegularFile(path));
    System.out.println("Is Symbolic Link:" + Files.isSymbolicLink(path));
    System.out.println("Is Hidden:" + Files.isHidden(path));
    System.out.println("Last Modified Time:" + Files.getLastModifiedTime(path));
    System.out.println("Owner:" + Files.getOwner(path));

    DosFileAttributeView view = Files.getFileAttributeView(path, DosFileAttributeView.class);
    System.out.println("Archive  :" + view.readAttributes().isArchive());
    System.out.println("Hidden   :" + view.readAttributes().isHidden());
    System.out.println("Read-only:" + view.readAttributes().isReadOnly());
    System.out.println("System   :" + view.readAttributes().isSystem());

    view.setHidden(false);//from  ww w . ja v  a 2s  . c om
}

From source file:Test.java

private static void displayFileAttributes(Path path) throws Exception {
    String format = "Exists: %s %n" + "notExists: %s %n" + "Directory: %s %n" + "Regular: %s %n"
            + "Executable: %s %n" + "Readable: %s %n" + "Writable: %s %n" + "Hidden: %s %n" + "Symbolic: %s %n"
            + "Last Modified Date: %s %n" + "Size: %s %n";

    System.out.printf(format, Files.exists(path, LinkOption.NOFOLLOW_LINKS),
            Files.notExists(path, LinkOption.NOFOLLOW_LINKS),
            Files.isDirectory(path, LinkOption.NOFOLLOW_LINKS),
            Files.isRegularFile(path, LinkOption.NOFOLLOW_LINKS), Files.isExecutable(path),
            Files.isReadable(path), Files.isWritable(path), Files.isHidden(path), Files.isSymbolicLink(path),
            Files.getLastModifiedTime(path, LinkOption.NOFOLLOW_LINKS), Files.size(path));
}

From source file:com.liferay.sync.engine.util.FileUtil.java

public static String getChecksum(Path filePath) throws IOException {
    if (!Files.exists(filePath) || (Files.size(filePath) > PropsValues.SYNC_FILE_CHECKSUM_THRESHOLD_SIZE)) {

        return "";
    }/*from   ww w . j a  v a 2  s  .  c  o  m*/

    InputStream fileInputStream = null;

    try {
        fileInputStream = Files.newInputStream(filePath);

        byte[] bytes = DigestUtils.sha1(fileInputStream);

        return Base64.encodeBase64String(bytes);
    } finally {
        StreamUtil.cleanUp(fileInputStream);
    }
}

From source file:fi.jumi.launcher.daemon.DirBasedSteward.java

private static boolean sameSize(Path path, InputStream in) throws IOException {
    return Files.exists(path) && Files.size(path) == in.available();
}

From source file:net.sf.taverna.t2.renderers.RendererUtils.java

public static long getSizeInBytes(Path path) throws IOException {
    if (isValue(path))
        return Files.size(path);
    if (!isReference(path))
        throw new IllegalArgumentException("Path is not a value or reference");

    URL url = getReference(path).toURL();
    switch (url.getProtocol().toLowerCase()) {
    case "http":
    case "https":
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("HEAD");
        conn.connect();//from w w w.  ja  v a2s  . c om
        String contentLength = conn.getHeaderField("Content-Length");
        conn.disconnect();
        if (contentLength != null && !contentLength.isEmpty())
            return Long.parseLong(contentLength);
        return -1;
    case "file":
        return FileUtils.toFile(url).length();
    default:
        return -1;
    }
}

From source file:org.stem.tools.cli.Utils.java

/**
 * Get data from file/*from   w  w w.  j a v  a  2  s . com*/
 *
 * @param fileName file name to read commands from
 * @return byte[] binary data
 * @throws IOException
 */
public static byte[] readFromFile(String fileName, final int maxSize) throws IOException {
    Path filePath = Paths.get(fileName);
    if (!Files.exists(filePath) && !Files.isRegularFile(filePath)) {
        throw new FileNotFoundException("There is no file or it is not regular file.");
    }

    if (Files.size(filePath) > maxSize) {
        throw new IOException("File is too big");
    }
    byte[] blob = new byte[(int) Files.size(filePath)];

    try (FileInputStream fis = new FileInputStream(fileName)) {
        fis.read(blob);
        return blob;
    }
}

From source file:internal.diff.aws.service.AmazonS3ETagFileChecksumServiceImpl.java

@Override
public String calculateChecksum(Path file) throws IOException {

    long fileSize = Files.size(file);

    int parts = (int) (fileSize / S3_MULTIPART_SIZE_LIMIT_IN_BYTES);
    parts += fileSize % S3_MULTIPART_SIZE_LIMIT_IN_BYTES > 0 ? 1 : 0;

    ByteBuffer checksumBuffer = ByteBuffer.allocate(parts * 16);

    SeekableByteChannel byteChannel = Files.newByteChannel(file);

    for (int part = 0; part < parts; part++) {

        int partSizeInBytes;

        if (part < parts - 1 || fileSize % S3_MULTIPART_SIZE_LIMIT_IN_BYTES == 0) {
            partSizeInBytes = S3_MULTIPART_SIZE_LIMIT_IN_BYTES;
        } else {//from   w w w.  j a v  a2s . com
            partSizeInBytes = (int) (fileSize % S3_MULTIPART_SIZE_LIMIT_IN_BYTES);
        }

        ByteBuffer partBuffer = ByteBuffer.allocate(partSizeInBytes);

        boolean endOfFile;
        do {
            endOfFile = byteChannel.read(partBuffer) == -1;
        } while (!endOfFile && partBuffer.hasRemaining());

        checksumBuffer.put(DigestUtils.md5(partBuffer.array()));
    }

    if (parts > 1) {
        return DigestUtils.md5Hex(checksumBuffer.array()) + "-" + parts;
    } else {
        return Hex.encodeHexString(checksumBuffer.array());
    }
}

From source file:de.elomagic.carafile.client.CaraFileUtils.java

/**
 * Creates a meta file from the given file.
 *
 * @param path Path of the file//from   w  w w.j av  a  2  s. c  om
 * @param filename Real name of the file because it can differ from path parameter
 * @return
 * @throws IOException
 * @throws GeneralSecurityException
 */
public static final MetaData createMetaData(final Path path, final String filename)
        throws IOException, GeneralSecurityException {
    if (Files.notExists(path)) {
        throw new FileNotFoundException("File " + path.toString() + " not found!");
    }

    if (Files.isDirectory(path)) {
        throw new IllegalArgumentException("Not a file: " + path.toString());
    }

    MetaData md = new MetaData();
    md.setSize(Files.size(path));
    md.setFilename(filename);
    md.setCreationDate(new Date());
    md.setChunkSize(DEFAULT_PIECE_SIZE);

    try (InputStream in = Files.newInputStream(path, StandardOpenOption.READ);
            BufferedInputStream bin = new BufferedInputStream(in)) {
        MessageDigest mdComplete = MessageDigest.getInstance(MessageDigestAlgorithms.SHA_1);

        byte[] buffer = new byte[DEFAULT_PIECE_SIZE];
        int bytesRead;

        while ((bytesRead = bin.read(buffer)) > 0) {
            mdComplete.update(buffer, 0, bytesRead);

            ChunkData chunk = new ChunkData(
                    DigestUtils.sha1Hex(new ByteArrayInputStream(buffer, 0, bytesRead)));
            md.addChunk(chunk);
        }

        String sha1 = Hex.encodeHexString(mdComplete.digest());
        md.setId(sha1);
    }

    return md;
}