Results 1 to 8 of 8
  1. #1
    leric is offline Member
    Join Date
    Jul 2009
    Posts
    4
    Rep Power
    0

    Default Copy a file to a folder.

    Hi, I am new to java and i have search through the forum/google and find no appropriate solution or that i do not understand.

    I am creating a program which will save a picture selected by the user and store it into a folder which i want.

    Source File - the user will select it.
    Destination Folder - prefix but the program.

    Here is the current code of my program. I have this problem:

    java.io.FileNotFoundException: C:\Documents and Settings\Administrator\Desktop\Images (Access is denied)

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import javax.swing.SwingUtilities;
    import javax.swing.filechooser.*;
    import java.sql.*;
    import java.nio.channels.*;

    public class MainFrame extends JFrame implements ActionListener {

    JLabel lblBanner;
    JButton btnChoose, btnExit, btnRandom;
    JPanel panel1, panel2;
    final JFileChooser fc;
    Icon ImgBanner;

    Connection connection;
    Statement sql;
    ResultSet resultSet;

    public MainFrame() {

    //Instantiate components
    ImgBanner = new ImageIcon ("Images/nophoto.jpg");
    lblBanner = new JLabel (ImgBanner);

    btnChoose = new JButton ("Choose Image");
    btnExit = new JButton ("Exit");
    btnRandom = new JButton ("Random");
    panel1 = new JPanel();
    panel2 = new JPanel();

    fc = new JFileChooser();

    //Add components to container
    Container main = getContentPane();
    main.setLayout (new GridLayout(0,1));

    main.add(panel1);
    main.add(panel2);

    panel1.setLayout(new FlowLayout());
    panel1.add(lblBanner);

    panel2.setLayout(new FlowLayout());
    panel2.add(btnChoose);
    panel2.add(btnExit);
    panel2.add(btnRandom);

    //Add actionListener
    btnChoose.addActionListener(this);
    btnExit.addActionListener(this);
    btnRandom.addActionListener(this);

    setSize (620,510); //set size of container

    /// initialize db ///
    try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    }
    catch(ClassNotFoundException exception) {
    JOptionPane.showMessageDialog( null,exception.getMessage() );
    }

    try {
    connection = DriverManager.getConnection("jdbc:odbc:Driver={Mic rosoft Access Driver (*.mdb)};DBQ=C:/Documents and Settings/Administrator/Desktop/TestImage/db1.mdb;","","");

    sql = connection.createStatement();

    }
    catch(SQLException exception){
    JOptionPane.showMessageDialog( null,exception.getMessage() );
    }
    /// end db init ///
    }

    public void actionPerformed(ActionEvent e) {

    if (e.getSource() == btnExit)
    System.exit(0);

    if (e.getSource() == btnChoose) {

    int returnVal = fc.showOpenDialog(MainFrame.this);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
    File source = fc.getSelectedFile();
    String path = ""+source;
    ImgBanner = new ImageIcon (path);
    lblBanner.setIcon(ImgBanner);

    String dest = "C:/Documents and Settings/Administrator/Desktop/Images";

    File destination = new File(dest);
    source = new File(path);

    JOptionPane.showMessageDialog(null,path);
    JOptionPane.showMessageDialog(null,dest);

    try {
    copyFile(source, destination);
    JOptionPane.showMessageDialog(null,"Complete");
    } catch (IOException exception){
    JOptionPane.showMessageDialog(null,exception);
    }


    /*
    //execute save to db
    try {
    resultSet = sql.executeQuery("SELECT COUNT(*)FROM MyOLETest");

    int counter = 0 ;

    if (resultSet.next()) {
    counter = resultSet.getInt(1);
    } else {
    System.out.println("error: could not get the record counts");
    }

    counter +=1;

    sql.executeUpdate(" INSERT INTO MyOLETest (ID, Picture) VALUES " + "('" + counter + "','" + ImgBanner + "') ");
    JOptionPane.showMessageDialog(null,"Add successful");

    } catch(SQLException exception) {
    JOptionPane.showMessageDialog(null,exception.getMe ssage());
    JOptionPane.showMessageDialog(null,path);
    }*/

    fc.setSelectedFile(null);
    }

    }


    }




    public static void copyFile(File srcFile, File destFile) throws IOException {

    InputStream oInStream = new FileInputStream(srcFile);
    OutputStream oOutStream = new FileOutputStream(destFile);

    // Transfer bytes from in to out
    byte[] oBytes = new byte[1024];
    int nLength;
    BufferedInputStream oBuffInputStream = new BufferedInputStream( oInStream );

    while ((nLength = oBuffInputStream.read(oBytes)) > 0) {
    oOutStream.write(oBytes, 0, nLength);
    }

    oInStream.close();
    oOutStream.close();
    }

    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Two things you have to check.

    1. Is that path is correct?
    2. Is that location is accessible, means attributes.

  3. #3
    leric is offline Member
    Join Date
    Jul 2009
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Two things you have to check.

    1. Is that path is correct?
    2. Is that location is accessible, means attributes.
    1. Correct since i copied it from the address path itself.
    2. The folder is just created without any modification. From a normal window XP, right click --> new ---> folder.

    It seems like the code i have is for writing copying content from one file to another but not copying the entire file (which is an image that I intend to copy) to the folder the system instructed (C:\Documents and Settings\Administrator\Desktop\Images) <---- is a folder.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Did you debug your code? From which code segment exactly the exception is thrown. I'm lazy to add your code in a main method lol. ;)

  5. #5
    leric is offline Member
    Join Date
    Jul 2009
    Posts
    4
    Rep Power
    0

    Default

    the exception was thrown while trying to run copyFile since the destination cannot be access or not found.

    below is the exception captured:

    java.io.FileNotFoundException: C:\Documents and Settings\Administrator\Desktop\Images (Access is denied)

    try {
    copyFile(source, destination);
    JOptionPane.showMessageDialog(null,"Complete");
    } catch (IOException exception){
    JOptionPane.showMessageDialog(null,exception);
    }

    Hope this can be solved asap. Don't have much time left. TY!

  6. #6
    leric is offline Member
    Join Date
    Jul 2009
    Posts
    4
    Rep Power
    0

    Default

    problem no longer exist. Thanks for your time. I used alternative way by saving the image in the system and create a new file for it rather than copy the file over.

    here is the solution if anyone needs it.

    try {
    File inputFile = fc.getSelectedFile();
    BufferedImage input = ImageIO.read(inputFile);
    File outputFile = new File("Images/image.png");
    ImageIO.write(input, "PNG", outputFile);

    } catch (IOException abc) {
    System.out.println(abc);
    }

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by leric View Post
    the exception was thrown while trying to run copyFile since the destination cannot be access or not found.

    below is the exception captured:

    java.io.FileNotFoundException: C:\Documents and Settings\Administrator\Desktop\Images (Access is denied)

    try {
    copyFile(source, destination);
    JOptionPane.showMessageDialog(null,"Complete");
    } catch (IOException exception){
    JOptionPane.showMessageDialog(null,exception);
    }

    Hope this can be solved asap. Don't have much time left. TY!
    Nice to see that you've solved the problem.

    I just want to add one thing here. The code segment you've post here not thrown any exception. The exceptions comes from the copyFile() method you used there.

    Better to identify the exact code segment next time lol. It's easy to you, to find the solution so easily. Debug the code, and make sense to you. Good luck :)

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. How to revoke permission to save or copy a PDF file in java
    By priyanka.dandekar in forum Advanced Java
    Replies: 11
    Last Post: 10-05-2008, 06:06 PM
  2. Replies: 1
    Last Post: 06-23-2008, 06:46 AM
  3. How can i copy a folder from one place to another..
    By rajeshgubba in forum New To Java
    Replies: 4
    Last Post: 06-14-2008, 02:21 AM
  4. java file copy
    By hknyo in forum New To Java
    Replies: 1
    Last Post: 06-12-2008, 04:42 AM
  5. copy image/imageicon into a file on disk
    By archanajathan in forum Advanced Java
    Replies: 2
    Last Post: 11-22-2007, 06:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •