Hi everyone,
I am doing a simple program that the user is able to select whatever folders(not files) in the drive(through the argument of the folderpaste function) and is able to paste the folders (with their contents) in the the path "C:" but i think my main problem is that i have to cast a string into file object but there may also be other errors. The entire program compiles with no errors but when i print the stack trace i find the following errors
java.io.FileNotFoundException: D:Sample_pics (Access is denied)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
java.lang.ClassCastException
at JFileExplorer.folderpaste(JFileExplorer.java:285)
The files do exist and their access is not denied nor are the encrypted in any fashion.
I will only list the folderpaste function and not the entire program as the other functions in the program all work okay including the copying function.
The argument passed into the folderpaste function are the file names of the folders (including their previous path). Thus if the folder' s file name that was copied is "D:Sample_pictures"
my function will input stream that particular folder and cut the string to "Sample_pictures"
and then do some string manipulation until the string reads "C:Sample_pictures"
and then output stream the previously copied file to its new location.
I know it sounds complicated but please try to have a look at my function and tell me or show me what i am doing wrong.
If someone has any sample codings of how to paste files or folders from
from one place to the other please show it or e-mail it to me.
The function is as follows:
public void folderpaste (File[ ] file)
{
String k, w, m, l, p, m1;
int i=0, j, len;
byte[] buffer1 = new byte[2048];
k = ("C:");
File f;
for(i=0;i<file.length;i++)
{
try
{
FileInputStream in = new FileInputStream(file[i]);
m = file[i].toString();
System.out.println(m);
//The below command line is not wrong as in java
// "" is actually equal to ""
j = m.lastIndexOf("");
w = m.substring(j+1);
l = (k + "" + w);
//This is where i think the error occurs as i try to cast the string into an
//object and then recast that object into a file object
Object f1 = (Object)l;
f = (File)f1;
if(f.exists() == true)
{
m1 = f.toString();
p = (m1 + " copy " + i);
}
else
{
p = f.toString();
}
FileOutputStream out = new FileOutputStream(p);
while((len = in.read(buffer1)) != -1)
{
out.write(buffer1, 0, len);
}
in.close();
out.close();
}
catch(IOException e)
{
e.printStackTrace();
Label1.setText("An error pasting the file has occured");
}
}
}
My e-mail is [Snipped]
Thank you for your patience
Yours Sincerely
Richard West