• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

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

0
blossompark
Asked:
blossompark
  • 2
  • 2
1 Solution
 
CSecurityCommented:
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?
0
 
blossomparkAuthor Commented:
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...
0
 
CSecurityCommented:

File folder = new File("C:\\ANT\\Source\\");
    File[] listOfFiles = folder.listFiles();

    for (int i = 0; i < listOfFiles.length; i++) {
      if (listOfFiles[i].isFile()) {
	in = new FileInputStream("C:\\ANT\\Source\\" + listOfFiles[i].getName());
        out = new FileOutputStream("c:\\ANT\\Destination\\" + listOfFiles[i].getName());

/// NOW DO WHATEVER YOU WANT
/// LIKE WHAT YOU DID:

 int c;

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

Open in new window

0
 
blossomparkAuthor Commented:
Hi CSecurity,
   thanks thats great...works a treat... been googling this all day and no joy!!
thanks again
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now