Search code examples
javastring-parsing

Parsing File name of a doc file of java


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?


Solution

  • 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 _