Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Is there a File.copyTo() ?

Posted on 2003-02-25
8
Medium Priority
?
958 Views
Last Modified: 2008-01-16
I need to copy a source file (which as an added difficulty is a jar file) to a certain destination.

I can move it with File.renameTo() but what I actually need is to copy it eg a File.copyTo() method.

I wrote my own but it did not work even tho' the file sizes are the same, I get a "class not found" when I try to use the copied jar file. I assume that's java security for you, so I think I need a "java approved" method of copying a jar file.

Any ideas?
Jim
0
Comment
Question by:binaryjim
  • 3
  • 3
  • 2
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8016840
Can you show your code?
0
 
LVL 3

Accepted Solution

by:
schybert earned 150 total points
ID: 8017069
I've used something like this without running into any problems...

private static void copy(File src, File target) throws IOException {
     
    FileInputStream from = null;
    FileOutputStream to = null;
    try {
         
        from = new FileInputStream(src);
        to = new FileOutputStream(target);
        byte[] buffer = new byte[4096];
        int bytesRead;

        while ((bytesRead = from.read(buffer)) != -1) {
            to.write(buffer, 0, bytesRead);
        }
       
    }
    finally {
        try {
            from.close();
        }
        catch (IOException ie) {
        }
        try {
            to.close();
        }
        catch (IOException ie) {
        }
    }
}
0
 
LVL 3

Expert Comment

by:schybert
ID: 8017080
Ooops, might want to make that a public method :-)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:binaryjim
ID: 8017147
Thanks, must've been my sloppy code. I did think tho' that you were not allowed to re-create/tamper with jar files. How the JVM could tell I'd no idea.
So why does File not have a File.copyTo() method then?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8017236
So you're not interested why yours didn't work then?
0
 

Author Comment

by:binaryjim
ID: 8017563
Not really, more interested to know why there is no File.copyTo() method. The code was a bit dirty, tho' it re-created the file in the correct place and was the correct size:
  while(true){
     int nextByte = inpstream.read();
     if (nextByte==-1)
       break;
     outstream.write(nextByte);
 }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8017621
Not sure why there is no copy method.

There's nothing to stop that code you posted working as it stands.

>>it re-created the file in the correct place and was the correct size

Then your code worked already.
0
 

Author Comment

by:binaryjim
ID: 8017947
Ok, thanks for that :-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question