-1

Hello I want to copy files without singles files or folders.

when i run the method i get a stack

public static void copyFiles() {
    String path = "C:\\luna\\trunk\\";
    List<File> files = (List<File>) FileUtils.listFiles(new File(path), new SuffixFileFilter("-4.7-SNAPSHOT.jar"), TrueFileFilter.INSTANCE);
    Pattern pattern = Pattern.compile("^(C:\\luna\\trunk\\lunaRelease\\target\\lib\\)");
    for (File file : files) {
        Matcher matcher = pattern.matcher(file.getName());
        if (file.getName().matches("warsztaty-4.7-SNAPSHOT.jar") || file.getName().matches("lunaRelease-4.7-SNAPSHOT.jar") || matcher.find() ) {
            System.out.println("Don't copy this file");
        } else {
            System.out.println(file.getAbsolutePath());
            String source = file.getAbsolutePath();
            File sourcedir = new File(source);
            File dest = new File("C:\\library");
            try {
                FileUtils.copyFileToDirectory(sourcedir, dest);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

This is stack when I run method.

"C:\Program Files\Java\jdk1.8.0_251\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence near index 5
^(C:\luna\trunk\lunaRelease\target\lib\)
     ^
    at java.util.regex.Pattern.error(Pattern.java:1969)
    at java.util.regex.Pattern.escape(Pattern.java:2485)
    at java.util.regex.Pattern.atom(Pattern.java:2212)
    at java.util.regex.Pattern.sequence(Pattern.java:2144)
    at java.util.regex.Pattern.expr(Pattern.java:2010)
    at java.util.regex.Pattern.group0(Pattern.java:2919)
    at java.util.regex.Pattern.sequence(Pattern.java:2065)
    at java.util.regex.Pattern.expr(Pattern.java:2010)
    at java.util.regex.Pattern.compile(Pattern.java:1702)
    at java.util.regex.Pattern.<init>(Pattern.java:1352)
    at java.util.regex.Pattern.compile(Pattern.java:1028)
    at CopyFiles.copyFiles(CopyFiles.java:23)
    at CopyFiles.main(CopyFiles.java:17)

Process finished with exit code 1

How can I fix it ?

4
  • 1
    Try replacing \\ with \\\\. Commented Oct 10, 2020 at 11:21
  • 1
    Well, error message is not wrong, there is no \l special sequence in regex syntax, like for instance \d or \w \s so that \ shouldn't probably be start of such sequence, so it should be escaped. Regex representing \ may look like \\ which can be written in string literal as "\\\\". BUT why are you using regex in the first place? Why matches("warsztaty-4.7-SNAPSHOT.jar") instead of equals("warsztaty-4.7-SNAPSHOT.jar") (same about `lunaRelease-4.7-SNAPSHOT.jar).
    – Pshemo
    Commented Oct 10, 2020 at 11:24
  • 1
    Also if your only goal with regex is to use its start of line/data ^ anchor then you can achieve similar effect with strData.startsWith(sequence)
    – Pshemo
    Commented Oct 10, 2020 at 11:24
  • I resolve problem like this : file.getAbsolutePath().startsWith("C:\\luna\\trunk\\lunaRelease\\target\\lib\\") Commented Oct 10, 2020 at 12:14

1 Answer 1

0

The new method , because I want to dont copy with path startwith : C:\luna\trunk\lunaRelease\target\lib\"

public static void copyFiles() {
    String path = "C:\\luna\\trunk\\";
    List<File> files = (List<File>) FileUtils.listFiles(new File(path), new SuffixFileFilter("-4.7-SNAPSHOT.jar"), TrueFileFilter.INSTANCE);
    for (File file : files) {
        if (file.getName().matches("warsztaty-4.7-SNAPSHOT.jar") || file.getName().matches("lunaRelease-4.7-SNAPSHOT.jar") || file.getAbsolutePath().startsWith("C:\\luna\\trunk\\lunaRelease\\target\\lib\\") ) {
            System.out.println("Don't copy this file");
        } else {
            System.out.println(file.getAbsolutePath());
            String source = file.getAbsolutePath();
            File sourcedir = new File(source);
            File dest = new File("C:\\library");
            try {
                FileUtils.copyFileToDirectory(sourcedir, dest);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Thank you for help.

2
  • 1
    Note: matcher also uses regex and in regex dot . represents any character so .matches("warsztaty-4.7-SNAPSHOT.jar") would also return true for .matches("warsztaty-4#7-SNAPSHOT#jar").
    – Pshemo
    Commented Oct 10, 2020 at 12:35
  • 1
    To check exact value use equals instead of matches.
    – Pshemo
    Commented Oct 10, 2020 at 12:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.