[英]Parsing a File in Java
im using the dropbox api. 即时通讯使用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: 当我要求api从我的帐户获取文件列表时,它会为我提供此元数据,该元数据不是我所知道的任何特定格式:
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. 由于我对字符串的使用不太好,所以我希望有人能帮助我找到一种方法,至少从上面提取文件名(“ /magnum-opus.txt”)以及“ lastmodified”和“ humansize”位。 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. 假设您使用的是官方Java SDK中的元数据方法之一(例如getMetadata ),这听起来像是您正在做toString并尝试手动解析它,这是不必要的。 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. Eg: 此元数据方法返回一个DbxEntry (您可以使用asFile将其转换为DbxEntry.File 。然后,您可以直接访问所需的关于该对象的信息。例如:
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(
首先删除字符串的一部分,直到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. 上面的代码应该生成一个HashMap键,该键是该字符串的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.