Link to home
Start Free TrialLog in
Avatar of blossompark
blossomparkFlag for Ireland

asked on

copying fles in java

Hi,
 the code below takes a file (C:\\ANT\\Source\\Waterfall.jpg) and creates a byte file of it and stores it at c:\\ANT\\Destination\\file1.
Right now, the source file (Waterfall.jpg) must exist at c:\ANT\Source, also file1 need not exist at c:\ANT\Destination but it has to be in the argument of FileOutputStream or else I get a runtime error ;
Exception in thread "main" java.io.FileNotFoundException: c:\ANT\Destination (Access is denied)
        at java.io.FileOutputStream.open(Native Method)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:70)
        at CopyBytes.main(CopyBytes.java:17)
////////////////////////////////////
what i am trying to do is write an application that takes whatever is stored at c:\ANT\source converts it/them to corresponding byte files and store the byte files at c:\ANT\Destination.

any help appreciated.
thanks
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


public class CopyBytes {
    public static void main(String[] args) throws IOException {
        FileInputStream in = null;
        FileOutputStream out = null;
        try {
            in = new FileInputStream("C:\\ANT\\Source\\Waterfall.jpg");
            out = new FileOutputStream("c:\\ANT\\Destination\\file1");
            int c;

            while ((c = in.read()) != -1) {
                out.write(c);
            }

        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }
}

Open in new window

Avatar of CSecurity
CSecurity
Flag of Iran, Islamic Republic of image

What's wrong so? Source should exists, cause you want to open it and read it, destination should not exists because you want to write there. Which part is problem and what you want to fix?
Avatar of blossompark

ASKER

I do not want to specify the file names in either source or destination.
When  the program runs i want it to automatically look in C:\\ANT\\Source and convert any files it finds there into bytecode files.
I then want these bytecode files to be written to the c:\\ANT\\Destination folder...
ASKER CERTIFIED SOLUTION
Avatar of CSecurity
CSecurity
Flag of Iran, Islamic Republic of image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi CSecurity,
   thanks thats great...works a treat... been googling this all day and no joy!!
thanks again