Parsing File name of a doc file of java

非 Y 不嫁゛ 提交于 2019-12-11 09:17:49

问题


I want to parse the file names of multiple doc files (MS office) using java. How should I go about doing this?

I was able to find an API on extracting info from the doc itself, but I can't find information on the file name itself.

So say I have a doc file XX_232312_22, I want to just parse the file name (ie 232312 part).

EDIT: What would we do if we need to parse more than just one file? For instance, all 1000 files in one directory?


回答1:


String[] parts = filename.split("-");
parts[0] // part before dash
parts[1] // part after dash

You can look up String.split in the java docs: http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29

EDIT:

OP changed the format of the filename to XX_filename__00.

It would then be

String[] parts = filename.split("_");
parts[0] // part before first _
parts[1] // part between two _
parts[2] // part after second _



回答2:


This should work for you.

fileName.split("-")[0]



来源:https://stackoverflow.com/questions/16922509/parsing-file-name-of-a-doc-file-of-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!