文件复制访问被拒绝

时间:2014-11-21 12:18:55

标签: java file

这里我试图将文件复制到另一个位置。我的源地址为

F:\C#\Studies\OS\ch4.ppt

我想将ch3.ppt复制到用户选择.ie。的另一个位置,从JFilechooser获取目标地址(用户想要复制的地方)。我在这里获得访问被拒绝错误

    {
        //Getting the value of fileLocationSourceDrive from table.
        //fileLocationSourceDrive contains F:\C#\Studies\OS\ch4.ppt

        JFileChooser location = new JFileChooser();
        location.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        location.showSaveDialog(null);
        String fileLocationDestination = location.getSelectedFile().getAbsolutePath().toString();
        copyFile(fileLocationSourceDrive, fileLocationDestination);
    }


    public static void copyFile(String sourceFileName, String destionFileName) {
        try {
            System.out.println("Reading..." + sourceFileName);
            File sourceFile = new File(sourceFileName);
            File destinationFile = new File(destionFileName);
            InputStream in = new FileInputStream(sourceFile);
            JOptionPane.showMessageDialog(null, destinationFile);
            OutputStream out = new FileOutputStream(destinationFile);
            //OutputStream out = new FileOutputStream("C:\\Users\\Sagar Ch\\Documents\\New folder");

            byte[] buffer = new byte[1024];
            int length;
            while ((length = in.read(buffer)) > 0) {
                out.write(buffer, 0, length);
            }
            in.close();
            out.close();
            System.out.println("Copied: " + sourceFileName);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

这里是错误块(获取访问被拒绝)

Reading...F:\C#\Studies\OS\ch4.ppt
java.io.FileNotFoundException: C:\Users\Sagar Ch\Documents\New folder (Access is denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at main.copyFile(main.java:1277)
at main$22.actionPerformed(main.java:1127)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

1 个答案:

答案 0 :(得分:3)

我认为问题在于您正在尝试将文件(将其复制)写入C:\Users\Sagar Ch\Documents\New folder的路径,而这不是“文件”,即现有文件夹。您应该将ch4.ppt复制到C:\Users\Sagar Ch\Documents\New folder\ch4.ppt