Results 1 to 12 of 12
  1. #1
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default Accessing metadata of files

    Hi!

    I'm developing a simple file manager. So, I have two components: JTree & JTable. When I open some folder in JTree, then JTable is updated according to the content of that folder. JTable contains 3 columns: file_type, file_name, file_bytes. The code works correctly.

    Now I need to add some more columns to describe each file in JTable, e.g. file_author, file_description, file_link. How could I assign/read these data to/from files? Should I use metadata?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by LianaN View Post
    Now I need to add some more columns to describe each file in JTable, e.g. file_author, file_description, file_link. How could I assign/read these data to/from files? Should I use metadata?
    Java 1.6 and earlier can't do that; you have to wait until Java 7 sees the light.

    kind regards,

    Jos

  3. #3
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default

    Thank you, it's a pity:(

    Is it possible to solve this problem without metadata? Maybe, I could add some tags "@file_description:..." at the top of the file? In this case, I don't know how to deal with "pdf" files...

  4. #4
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default

    For instance, here Managing Metadata (File and File Store Attributes) (The Java™ Tutorials > Essential Classes > Basic I/O) I see some information about metadata. Could I use it in my case?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by LianaN View Post
    For instance, here Managing Metadata (File and File Store Attributes) (The Java™ Tutorials > Essential Classes > Basic I/O) I see some information about metadata. Could I use it in my case?
    That is a preliminary tutorial of the NIO2 package, part of Java 7 only.

    kind regards,

    Jos

  6. #6
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default

    But what is this: Java Platform, Standard Edition 7 Binary Snapshot Releases? Isn't it JDK 7 installation? I've just installed Java SE Development Kit 7, will try it.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by LianaN View Post
    But what is this: Java Platform, Standard Edition 7 Binary Snapshot Releases? Isn't it JDK 7 installation? I've just installed Java SE Development Kit 7, will try it.
    Yep, that's a preliminary Java 7 installation. No warranties, no nothing, it's just a snapshot and can change overnight.

    kind regards,

    Jos

  8. #8
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default

    Ok, thank you. Eh...So, my problem is unsolveable now.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    How could I assign/read these data to/from files?
    If this is Windows and the file system is NTFS, you can use alternate streams. Example:
    Java Code:
    import java.io.*;
    
    public class AlternateStreamTest {
    
      public static void main(String[] args) {
        File outFile = new File("e:/java/outTest.txt:alternate");
        FileOutputStream fos = null;
        try {
          fos = new FileOutputStream(outFile);
          fos.write("The quick brown fox jumps over the lazy dog".getBytes());
        } catch (IOException ioe) {
          ioe.printStackTrace();
        } finally {
          if (fos != null) {
            try {
              fos.close();
            } catch (IOException ex) {
              ex.printStackTrace();
            }
          }
        }
    
        File inFile = new File("e:/java/inTest.txt");
        outFile.renameTo(inFile);
        
        inFile = new File("e:/java/outTest.txt:alternate");
        FileInputStream fis = null;
        try {
          fis = new FileInputStream(inFile);
          byte[] b = new byte[1024];
          while (fis.available() > 0) {
            fis.read(b);
            System.out.println(new String(b));
          }
        } catch (IOException ioe) {
          ioe.printStackTrace();
        } finally {
          if (fis != null) {
            try {
              fis.close();
            } catch (IOException ex) {
              ex.printStackTrace();
            }
          }
        }
      }
    }
    Otherwise you can save a companion file e.g. for myPDF.pdf you can save a file myPDF.pdf.txt with the metadata. This is not foolproof as it's theoretically possible that a myPdf.pdf.txt text file already exists. Using a long and obscure extension e.g.myPDF.pdf.myMetadata makes the chances of this happening astronomically low.

    Note: in the example above, the alternate stream cannot be viewed in the usual editor (NotePad). Google "NTFS Alternate Streams" for more information.

    db

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    I didn't know that and I find it a strange feature; I guess an 'alternate stream' can't have another alternate stream, can it?

    kind regards,

    Jos

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    No, only a physical file can have an alternate stream. But a little juggling showed that the alternate stream can be a file that itself has an alternate stream.

    This is going to be hard to read :-\
    Java Code:
    import java.io.*;
    
    public class AlternateStreamTest {
    
      public static void main(String[] args) throws IOException {
        File fileOstreamA = new File("e:/java/outA.txt:alt");
        FileOutputStream fos = null;
        try {
          fos = new FileOutputStream(fileOstreamA);
          fos.write("The quick brown fox jumps over the lazy dog".getBytes());
        } finally {
          if (fos != null) {
            try {
              fos.close();
            } catch (IOException ex) {
              ex.printStackTrace();
            }
          }
        }
    
        File fileO = new File("e:/java/outA.txt");
        File fileO2 = new File("e:/java/outS.txt");
    
        File fileO2streamS = new File("e:/java/outS.txt:sub");
        fileO.renameTo(fileO2streamS);
    
        File fileI2 = new File("e:/java/inS.txt");
        fileO2.renameTo(fileI2);
    
        File fileI2streamS = new File("e:/java/inS.txt:sub");
        File fileI = new File("e:/java/outA.txt");
        fileI2streamS.renameTo(fileI);
    
        File fileIstreamA = new File("e:/java/outA.txt:alt");
    
        FileInputStream fis = null;
        try {
          fis = new FileInputStream(fileIstreamA);
          byte[] b = new byte[1024];
          while (fis.available() > 0) {
            fis.read(b);
            System.out.println(new String(b));
          }
        } finally {
          if (fis != null) {
            try {
              fis.close();
            } catch (IOException ex) {
              ex.printStackTrace();
            }
          }
        }
      }
    }
    db

  12. #12
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default

    Thanks a lot!

    I plan to run my application at different operating systems. Therefore, I've implemented the second solution - I'm creating metadata.txt files for each file in JTree.

Similar Threads

  1. Randomly accessing files
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-12-2007, 11:46 AM
  2. Accessing index.dat files
    By vissu007 in forum Networking
    Replies: 1
    Last Post: 07-01-2007, 05:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •