简体   繁体   中英

getting file metadata fields

im using this code to get a specific user defined metadata in a image file:

File file = new File("C:\\image.jpg");
        Path path = FileSystems.getDefault().getPath(file.getPath(), "");
        BasicFileAttributes attr = null;
        try {
            attr = Files.readAttributes(path, BasicFileAttributes.class);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        UserDefinedFileAttributeView view = Files
        .getFileAttributeView(path,UserDefinedFileAttributeView.class);
        String name = "UserDefinedField";

        ByteBuffer buf = null;
        try {
            buf = ByteBuffer.allocate(view.size(name));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            view.read(name, buf);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        buf.flip();
        String value = Charset.defaultCharset().decode(buf).toString();
        System.out.println(value);

but i have to know the field, is there a way to LIST ALL OF the metadata fields?!

You can retrieve all UserDefinedFileAttributes of the file using list() like

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.UserDefinedFileAttributeView;
import java.util.List;

public class ReadData {

    public static void main(String[] args) throws IOException {
        Path path = Paths.get("someImage.jpg").toAbsolutePath();

        UserDefinedFileAttributeView fileAttributeView = Files.getFileAttributeView(path, UserDefinedFileAttributeView.class);
        List<String> allAttrs = fileAttributeView.list();

        for (String att : allAttrs) {
            System.out.println("att = " + att);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM