?
Solved

Is there a File.copyTo() ?

Posted on 2003-02-25
8
Medium Priority
?
956 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month14 days, 12 hours left to enroll

771 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