user2582318
user2582318

Reputation: 1647

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?!

Upvotes: 1

Views: 2469

Answers (1)

sol4me
sol4me

Reputation: 15718

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);
        }
    }
}

Upvotes: 2

Related Questions