PatternSyntaxException字符串拆分Java

时间:2019-01-24 03:08:26

标签: java string split patternsyntaxexception

我有这个代码。...

private static final String FS = System.getProperty("file.separator");

private void addPathToTreeFiles(String canonicalFolder, String canonicalFilename) throws Exception {
  String filename = null;
  String canonicalPaths = null;
  try {
    //...
    int lastFS = canonicalFilename.lastIndexOf(FS);
    filename = canonicalFilename.substring(lastFS + 1);
    canonicalPaths = canonicalFilename.substring(0, lastFS + 1).substring(canonicalFolder.length());
    if (canonicalPaths.startsWith(FS)) {
        canonicalPaths = canonicalPaths.substring(FS.length());
    }
    String[] recursivePaths = canonicalPaths.split(FS); //EXCEPTION HERE!
    //...
  } catch (Exception e) {
    System.out.println ("addPathToTreeFiles.canonicalFolder:" + canonicalFolder);
    System.out.println ("addPathToTreeFiles.canonicalFilename:" + canonicalFilename);
    System.out.println ("addPathToTreeFiles.filename:" + filename);
    System.out.println ("addPathToTreeFiles.canonicalPaths:" + canonicalPaths);
    throw e;
  }
}

该行中引发异常

String[] recursivePaths = canonicalPaths.split(FS);

提交的数据为:

addPathToTreeFiles.canonicalFolder:C:\Users\joseluisbz
addPathToTreeFiles.canonicalFilename:C:\Users\joseluisbz\AppData\Local\Packages\Microsoft.Windows.Cortana_cw5n1h2txyewy\LocalState\DeviceSearchCache\AppCache131927693329784900.txt
addPathToTreeFiles.filename:AppCache131927693329784900.txt
addPathToTreeFiles.canonicalPaths:AppData\Local\Packages\Microsoft.Windows.Cortana_cw5n1h2txyewy\LocalState\DeviceSearchCache\

Jan 23, 2019 9:41:12 PM org.joseluisbz.filefinder.OneSearch$SwingWorkerSearcher process
SEVERE: null
java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
 ^
        at java.util.regex.Pattern.error(Unknown Source)
        at java.util.regex.Pattern.compile(Unknown Source)
        at java.util.regex.Pattern.<init>(Unknown Source)
        at java.util.regex.Pattern.compile(Unknown Source)
        at java.lang.String.split(Unknown Source)
        at java.lang.String.split(Unknown Source)
        at org.joseluisbz.filefinder.OneSearch.addPathToTreeFiles(OneSearch.java:692)

具有此值:

AppData\Local\Packages\Microsoft.Windows.Cortana_cw5n1h2txyewy\LocalState\DeviceSearchCache\

为什么要引发异常? 如何避免呢?

0 个答案:

没有答案