문제

im using the dropbox api. When i ask the api to get file listings from my account, it gives me this meta data which isn't in any particular format that i know of:

magnum-opus.txt: File("/magnum-opus.txt", iconName="page_white_text", mightHaveThumbnail=false, numBytes=17734, humanSize="17.3 KB", lastModified="2014/01/27 05:19:30 UTC", clientMtime="2014/01/27 05:19:30 UTC", rev="11e6c9e5e")

Since im not that good with strings i was hoping if someone could help me find a way to extract at least the filename ("/magnum-opus.txt"), and the 'lastmodified' and 'humansize' bits from above. Please and thank you :)

도움이 되었습니까?

해결책

Assuming you're using one of the metadata methods in the official Java SDK (like getMetadata) it sounds like you're doing toString and trying to manually parse this, which is unnecessary. This metadata method returns a DbxEntry (which you can convert to DbxEntry.File using asFile. You can then directly access the information you want on that object. E.g.:

    DbxEntry.File fileInfo;
    try {
        fileInfo = dbxClient.getMetadata("/test.txt").asFile();
    }
    catch (DbxException ex) {
        ex.printStackTrace();
        System.err.println("Error in getMetadata(): " + ex.getMessage());
        System.exit(1); return;
    }
    System.out.println("fileInfo name: " + fileInfo.name);
    System.out.println("fileInfo lastModified: " + fileInfo.lastModified);
    System.out.println("fileInfo humanSize: " + fileInfo.humanSize);

다른 팁

Just String-playing...

String in = "magnum-opus.txt: File(\"/magnum-opus.txt\", iconName=\"page_white_text\", mightHaveThumbnail=false, numBytes=17734, humanSize=\"17.3 KB\", lastModified=\"2014/01/27 05:19:30 UTC\", clientMtime=\"2014/01/27 05:19:30 UTC\", rev=\"11e6c9e5e\")";
String insideParenth = in.substring(in.indexOf("(")+1, in.length()-1);
StringTokenizer tokenizer = new StringTokenizer(insideParenth, ", ");
while (tokenizer.hasMoreTokens()) {
    Map<String,String> properties = new HashMap<String,String>();
    String token = tokenizer.nextToken();
    if(token.contains("=")){
        token = token.replaceAll("\"", "");
        String left= token.substring(0, token.indexOf("="));
        String right=token.substring(token.indexOf("=")+1);
        properties.put(left,right );
        System.out.println("left:["+left+"] and right=[" + right + "]");        
    }
}

Output is:

left:[iconName] and right=[page_white_text]
left:[mightHaveThumbnail] and right=[false]
left:[numBytes] and right=[17734]
left:[humanSize] and right=[17.3]
left:[lastModified] and right=[2014/01/27]
left:[clientMtime] and right=[2014/01/27]
left:[rev] and right=[11e6c9e5e]

EDIT:

String titleWithPath, titleWithoutPath;

insideParenth = in.substring(in.indexOf("(")+1, in.length()-1); 
titleWithPath = insideParenth.substring(insideParenth.indexOf("\"")+1, insideParenth.indexOf(",")‌​-1); 
System.out.println(titleWithPath); //Prints out: "/magnum-opus.txt" 

String titleWithoutPath = insideParenth.substring(insideParenth.indexOf("/")+1, insideParenth.indexOf(",")-1 ); 
System.out.println(titleWithoutPath); //Prints out: "magnum-opus.txt"

First remove the part of the string up to the File(

String theString = "magnum-opus.txt: File("/magnum-opus.txt", iconName="page_white_text", mightHaveThumbnail=false, numBytes=17734, humanSize="17.3 KB", lastModified="2014/01/27 05:19:30 UTC", clientMtime="2014/01/27 05:19:30 UTC", rev="11e6c9e5e")";

theString = theString.replaceAll("^.*.File\\(","");

then remove the last character the "(".

theString = theString.replaceAll("\\)","");

Then you could just perform splits to get the information

HashMap<String,String> results = new HashMap<String,String>();
String[] arr = theString.split(",");
for(String s : arr) {
  s = s.replace("\"","");
  s = s.trim();
  if(s.contains("=")) {
     String[] tmp = s.split("=");
     results.put(tmp[0],tmp[1]);
  }
  else {
    results.put("filename",s);
  }
}

The above should produce a HashMap of key, values from that string.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top