Dinup Kandel
Dinup Kandel

Reputation: 2505

Problem with file transfering in java

i have method

protected String browsesFile() {        
            String url = null;
            FileDialog dialog = new FileDialog(PlatformUI.getWorkbench()
                    .getActiveWorkbenchWindow().getShell(), SWT.NULL);
            // set the filter options
            dialog.setFilterExtensions(new String[] { "*.jpg", "*.jpeg", "*.png" });
            String path = dialog.open();
            if (path != null) {
                File file = new File(path);
                if (file.isFile())
                    url = file.toString();
                else
                    url = file.list().toString();
            }
            return url;
        }// end of method browseFile()

It will bring the url of the file . I call it as text.setText(browsesFile());. This will bring the url of image that i choose. I want that image to be transfer into G:\myImage. For transferring i did the following.

    public static void copyFile(File sourceFile, File destFile) throws IOException {
     if(!destFile.exists()) {
      destFile.createNewFile();
     }

     FileChannel source = null;
     FileChannel destination = null;
     try {
      source = new FileInputStream(sourceFile).getChannel();
      destination = new FileOutputStream(destFile).getChannel();
      destination.transferFrom(source, 0, source.size());
     }
     finally {
      if(source != null) {
       source.close();
      }
      if(destination != null) {
       destination.close();
      }
}}

I send By Using the function as

File source = new File(text.getText());         
    String url ="G:\\myImage";
    File dest = new File(url);
try {
    copyFile(source, dest);
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    }

i get the error message as

java.io.FileNotFoundException: G:\myImage (Access is denied)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)

what could be the possible reasong for this? i am using windows 7

Upvotes: 0

Views: 1007

Answers (1)

Captain Giraffe
Captain Giraffe

Reputation: 14705

You are using a directory name as your destination thats the source of your error.

You can easily fix this by adding the source filename to the destination by

File source = new File(text.getText());         
String url ="G:\\myImage";
File dest = new File(url, source.getName() );

Upvotes: 2

Related Questions