用Java解析文件

用Java解析文件,java,dropbox-api,text-parsing,Java,Dropbox Api,Text Parsing,我正在使用dropbox api。当我要求api从我的帐户获取文件列表时,它会提供我所知道的没有任何特定格式的元数据: magnum-opus.txt:File(“/magnum-opus.txt”,iconName=“page\u white\u text”,mightHaveThumbnail=false,numBytes=17734,humanSize=“17.3kb”,lastModified=“2014/01/27 05:19:30 UTC”,clientMtime=“2014/01/

我正在使用dropbox api。当我要求api从我的帐户获取文件列表时,它会提供我所知道的没有任何特定格式的元数据:

magnum-opus.txt:File(“/magnum-opus.txt”,iconName=“page\u white\u text”,mightHaveThumbnail=false,numBytes=17734,humanSize=“17.3kb”,lastModified=“2014/01/27 05:19:30 UTC”,clientMtime=“2014/01/27 05:19:30 UTC”,rev=“11e6c9e5e”)

由于我对字符串不太在行,我希望有人能帮我找到一种方法,至少从上面提取文件名(“/magnum opus.txt”)和“lastmodified”和“humansize”位。请并且谢谢你:)

只要弹一串

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 + "]");        
    }
}
编辑:


首先删除字符串中直到
文件(

然后删除最后一个字符“(”

然后,您可以执行拆分以获取信息

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);
  }
}
HashMap results=newhashmap();
字符串[]arr=String.split(“,”);
用于(字符串s:arr){
s=s。替换(“\”,“);
s=s.修剪();
如果(s)包含(“=”){
字符串[]tmp=s.split(“”);
结果:put(tmp[0],tmp[1]);
}
否则{
结果.put(“文件名”,s);
}
}

上面应该会生成该字符串的键和值的哈希映射。

假设您使用的是官方Java SDK中的一个元数据方法(like),听起来您正在执行toString并尝试手动解析,这是不必要的。此元数据方法返回(您可以将其转换为使用。然后您可以直接访问该对象上所需的信息。例如:

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

java还是C++?请选择ONE。我只是需要一个方法:)也许你应该尝试用字符串来改善。这是一个很糟糕的借口。有没有办法来选择两个答案,似乎不公平地只选一个,这都是解决我问题的好方法。有没有办法提取“/MAGNUMO OPUS.TXT?”“还有?添加了几行^ _^非常感谢。非常感谢。mahn!它在这行上给了我一个错误:'String[]arr=theString.split(',');'它说没有找到适合split(char)的方法”@Javed使用双引号。@Javed是的,我错了,单引号意味着字符类型。双引号是字符串谢谢这帮了大忙!!哇,我不知道我怎么会错过这个,再次感谢!!@Greg
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\\(","");
theString = theString.replaceAll("\\)","");
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);
  }
}
    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);