0

Error occur when copy file to another location

`public void Copy() {
        try {

            String Date = new SimpleDateFormat("dd-M-yyyy hh:mm:ss").format(new Date());

            File sourceFile = new File("D:\\DOFN Materials\\App\\LMSystem\\LMSystem.sqlite"); // db path

            File destinationFile = new File(" D:\\DOFN Materials\\App\\LMSystem\\Copy\\back"+Date+"_"+"LMSystem");
            
            Files.copy(sourceFile.toPath(), destinationFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
            System.out.println("file is copy");
            JOptionPane.showMessageDialog(null, Date + "  " + "  Copy Successfully !!");
        } catch (IOException ex) {
            Logger.getLogger(About.class.getName()).log(Level.SEVERE, null, ex);
        }
    }



Exception in thread "AWT-EventQueue-0" java.nio.file.InvalidPathException: Illegal char <:> at index 2: D:\DOFN Materials\App\LMSystem\Copy\back14-11-2022 12:19:06_LMSystem at java.base/sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182) at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153) at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77) at java.base/sun.nio.fs.WindowsPath.parse(WindowsPath.java:92) at java.base/sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:232) at java.base/java.io.File.toPath(File.java:2387) at Home.Copy(Home.java:47) at Home.jButton6ActionPerformed(Home.java:472) at Home.access$500(Home.java:16) at Home$6.actionPerformed(Home.java:252) at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972) at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313) at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405) at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262) at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279) at com.jtattoo.plaf.BaseButtonListener.mouseReleased(BaseButtonListener.java:60) at java.desktop/java.awt.Component.processMouseEvent(Component.java:6626) at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3389) at java.desktop/java.awt.Component.processEvent(Component.java:6391) at java.desktop/java.awt.Container.processEvent(Container.java:2266) at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5001) at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324) at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833) at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948) at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4575) at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516) at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310) at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780) at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833) at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716) at java.base/java.security.AccessController.doPrivileged(AccessController.java:399) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97) at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:746) at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:744) at java.base/java.security.AccessController.doPrivileged(AccessController.java:399) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:743) at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203) at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124) at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)




How to prevent that Error
  • It seems pretty obvious to me, but your file name can't contain the `:` character. You might want to have a look at [What characters are forbidden in Windows and Linux directory names?](https://stackoverflow.com/questions/1976007/what-characters-are-forbidden-in-windows-and-linux-directory-names) – MadProgrammer Nov 14 '22 at 03:17
  • There’s an extra space at the start of the filename: `new File(" D:`. I would guess that this is then treated as a relative path rather than an absolute path. Try deleting the space. – Tim Moore Nov 14 '22 at 03:32

1 Answers1

0

You are mixing old and new API and this isn't particularly smooth. java.io.File is old API; you should no longer be using this. For that matter, so is SimpleDateFormat (use all the stuff in the java.time package; do not use any time stuff from java.util - no Date, no Calendar, no SimpleDateFormat).

Path, Paths, and Files is new API - Files.copy is new API.

The replacement for new File() is Path p = Paths.get("D:\\....").

In addition, make sure your paths are actually clean. I think there are some extra spaces in your paths - at the command line, spaces are trimmed out, in program code, they definitely aren't - space is itself a valid character in file names, after all.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • All good advice. I would add: A) Use forward slashes in your paths: it will make your code less ugly B) Don't have spaces in paths: it makes scripting admin problematic and is anyway unnecessary C) Use ISO datetime formats: your files will then sort correctly. *Yes* you will need to substitute another character for the colon, as you found, so something like `String timestamp = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'hh-mm-ss").format(LocalDateTime.now());` might fit and is easily turned back into the 'real' ISO one. D) Add the file extension, else it will likely be unusable in Windows – g00se Nov 14 '22 at 11:57
  • Filenames primarily exist for the benefit of humans. "Do not put spaces in them" is some ancient DOS/windows level chicanery nobody should accept. The machine is the tool and should adapt to the needs of the operator; the reverse is ridiculous. – rzwitserloot Nov 14 '22 at 12:11
  • Irrespective of OS, it creates problems for sysadmins. This 'user' is clearly not a regular user so paths to backups really don't need to contain spaces. This same user might be glad of this later when someone asks them to do a sysadmin task which they're not used to on the backups – g00se Nov 14 '22 at 12:16