We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Copying Files on harddisk
Page Index Toggle Pages: 1
Copying Files on harddisk (Read 1630 times)
Copying Files on harddisk
May 6th, 2005, 7:43pm
 

Hi guys !

Fantastic new release the BETA.

Now, I need to copy a file from a harddisk into my applet folder in order to display a new image on the stage.

I am using this code to copy:

   JCopy cp = new JCopy();
   cp.copyFile("rhino.jpg", "AAAAAA.jpg");

Where I am putting the following code into another tab.
(i've also tried to put a compiled .class in the folder)
import java.io.*;

public class JCopy{

 JCopy() {}

 public void copyFile(File in, File out) throws Exception {
   FileInputStream fis  = new FileInputStream(in);
   FileOutputStream fos = new FileOutputStream(out);
   byte[] buf = new byte[1024];
   int i = 0;
   while((i=fis.read(buf))!=-1) {
     fos.write(buf, 0, i);
     }
   fis.close();
   fos.close();
   }
 }


But to no luck. I get error messages such as:

/Users/njetti/Library/Processing/build/Temporary_7794_5021.java:112:6:112:85: Semantic Error: No applicable overload for a method with signature "copyFile(java.lang.String, java.lang.String)" was found in type "Temporary_7794_5021$JCopy". Perhaps you wanted the overloaded version "void copyFile(java.io.File in, java.io.File out) throws java.lang.Exception;" instead?


Any clues how to solve this problem?

Or better: what's the best way of copying files from one location on the HD into the applet folder, so I can use it in the program?

cheers
njetti
Re: Copying Files on harddisk
Reply #1 - May 6th, 2005, 8:31pm
 
there's a bug in your code because you're trying to use a File object, instead of a String to copy the file.

for the input file, you need it to be a path to whatever file you want to add, and make it into a file object.

File infile = new File("path/to/the/file.jpg");

if you're on a mac it looks something like:
new File("/Users/njetti/Desktop/blah.jpg");
or on windows:
new File("c:/Documents and Settings/njetti/Desktop/blah.jpg");

and for the output file, if you want it to go into the applet folder, use the internal function savePath() to get the path to the sketch folder. so to put something into the sketch folder:

File outfile = new File(savePath("aaaa.jpg"));

or if you want it into the data folder of the sketch:

File outfile = new File(savePath("data/aaaaa.jpg"));

then use your copy function as you wrote it. though you don't need to put it into a separate class.
Re: Copying Files on harddisk
Reply #2 - May 7th, 2005, 1:10am
 
Thanks Fry,

Good explanations. I tried them but now I am getting this:


/Users/njetti/Library/Processing/build/Temporary_7269_6121.java:115:6:115:33: Semantic Error: The method "void copyFile(java.io.File in, java.io.File out) throws java.lang.Exception;" can throw the checked exception "java.lang.Exception", so its invocation must be enclosed in a try statement that catches the exception, or else this method must be declared to throw the exception.

I guess I am into Java-land now and it's not a Processing-esque problem, but I'd be greatful if someone could give me a tip on this one. That's how one learns and Processing is THE FUN WAY of learning Java.

Basically, where should I put the try statement?
And am I not declaring it to throw the exception?

Cheers
njetti
Re: Copying Files on harddisk
Reply #3 - May 7th, 2005, 1:17am
 
nope ... it's to catch the exception (and maybe recover from it):
Code:

try {
// your function goes here
} catch (Exception e) {
println( "Error occured at ... " );
e.printStackTrace();
} finally {
// what to do when finished trying and catching ...
}


here's more info:
http://www.janeg.ca/scjp/flow/try.html

F
Re: Copying Files on harddisk
Reply #4 - May 7th, 2005, 7:21pm
 
thanks Fry and fjen

All problems solved !!!
Page Index Toggle Pages: 1