Results 1 to 8 of 8
Thread: Copy a file to a folder.
- 07-27-2009, 09:23 PM #1
Member
- Join Date
- Jul 2009
- Posts
- 4
- Rep Power
- 0
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();
}
}
- 07-28-2009, 04:45 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Two things you have to check.
1. Is that path is correct?
2. Is that location is accessible, means attributes.
- 07-28-2009, 09:20 AM #3
Member
- Join Date
- Jul 2009
- Posts
- 4
- Rep Power
- 0
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.
- 07-28-2009, 10:59 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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. ;)
- 07-28-2009, 01:56 PM #5
Member
- Join Date
- Jul 2009
- Posts
- 4
- Rep Power
- 0
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!
- 07-28-2009, 05:45 PM #6
Member
- Join Date
- Jul 2009
- Posts
- 4
- Rep Power
- 0
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);
}
- 07-29-2009, 05:10 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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 :)
- 07-29-2009, 05:11 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
If you've solve the problem, please mark the thread solved from the Thread Tools menu.
Similar Threads
-
How to revoke permission to save or copy a PDF file in java
By priyanka.dandekar in forum Advanced JavaReplies: 11Last Post: 10-05-2008, 06:06 PM -
Requesting help in copy paste of folder similar to windows.
By selvin_raj in forum Advanced JavaReplies: 1Last Post: 06-23-2008, 06:46 AM -
How can i copy a folder from one place to another..
By rajeshgubba in forum New To JavaReplies: 4Last Post: 06-14-2008, 02:21 AM -
java file copy
By hknyo in forum New To JavaReplies: 1Last Post: 06-12-2008, 04:42 AM -
copy image/imageicon into a file on disk
By archanajathan in forum Advanced JavaReplies: 2Last Post: 11-22-2007, 06:21 AM


LinkBack URL
About LinkBacks


Bookmarks