User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 391,579 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,716 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 4008 | Replies: 7
Reply
Join Date: Jun 2004
Posts: 604
Reputation: freesoft_2000 is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 6
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Help casting error

  #1  
Oct 4th, 2004
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 freesoft_2000@yahoo.com

Thank you for your patience

Yours Sincerely

Richard West
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2004
Location: Hanover
Posts: 152
Reputation: cosi is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 1
cosi's Avatar
cosi cosi is offline Offline
Junior Poster

Re: casting error

  #2  
Oct 5th, 2004
Whoa... long convoluted question ;-). I hope I understand what you are asking. You simply want to copy the array of files to a particular directory?

If you simply want to move the file, try renameTo.

If you want to copy the file, see here.

Hope this helps.


Ed

Originally Posted by freesoft_2000
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 freesoft_2000@yahoo.com

Thank you for your patience

Yours Sincerely

Richard West
In a world without walls or fences,
What use are Windows and Gates.
Reply With Quote  
Join Date: Aug 2004
Location: Hanover
Posts: 152
Reputation: cosi is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 1
cosi's Avatar
cosi cosi is offline Offline
Junior Poster

Re: casting error

  #3  
Oct 5th, 2004
Originally Posted by freesoft_2000
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;

This is where your cast problem is. You should never cast an object to a type that it does not inherit or implement. Instead you should just create a new file object.

f = new File(l); // where l is the string containing the path of your new file


Ed
In a world without walls or fences,
What use are Windows and Gates.
Reply With Quote  
Join Date: Aug 2004
Location: Hanover
Posts: 152
Reputation: cosi is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 1
cosi's Avatar
cosi cosi is offline Offline
Junior Poster

Re: casting error

  #4  
Oct 5th, 2004
Ok. Sorry last post. I just noticed this problem of yours. Using the "\\" is very bad. That is a platform dependent file separator. Instead consider using File.pathSeparatorChar or File.pathSeparator.


Ed

Originally Posted by freesoft_2000
j = m.lastIndexOf("\\");
w = m.substring(j+1);
l = (k + "\\" + w);
In a world without walls or fences,
What use are Windows and Gates.
Reply With Quote  
Join Date: Jun 2004
Posts: 604
Reputation: freesoft_2000 is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 6
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Re: casting error

  #5  
Oct 6th, 2004
hi everyone,
That is exactly what i wanted cosi but instead of copying files i want to copy folders from one location to another. I have resolved the cast exception problem and changed it to File f = new File(l); but the exception of that the file is not found is still there.

java.io.FileNotFoundException: D:\Sample_pics (Access is denied)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)

The folder does exist on that specified path but i do not know why the above exception always occurs. i am trying to copy the folders only and not files and i also do not understand why they say the folder is "Access denied" when its not.

I hope you can help me with this problem cosi

My e-mail is freesoft_2000@yahoo.com

Thank You

Yours Sincerely

Richard West
Reply With Quote  
Join Date: Jun 2004
Posts: 604
Reputation: freesoft_2000 is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 6
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Re: casting error

  #6  
Oct 6th, 2004
hi everyone,
Sorry one more thing is that the jvm say that the line
//k is declared a string
k = (String)List2.getSelectedValue();
is a casting error but i don't think this is so.
Am i right or wrong cosi please tell me

Yours Sincerely

Richard west
Reply With Quote  
Join Date: Aug 2004
Location: Hanover
Posts: 152
Reputation: cosi is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 1
cosi's Avatar
cosi cosi is offline Offline
Junior Poster

Re: casting error

  #7  
Oct 8th, 2004
The problem here is that you are calling FileInputStream.open() on a directory (D:\Sample_pics). Append a filename to the end.


Ed

Originally Posted by freesoft_2000
hi everyone,
That is exactly what i wanted cosi but instead of copying files i want to copy folders from one location to another. I have resolved the cast exception problem and changed it to File f = new File(l); but the exception of that the file is not found is still there.

java.io.FileNotFoundException: D:\Sample_pics (Access is denied)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
In a world without walls or fences,
What use are Windows and Gates.
Reply With Quote  
Join Date: Aug 2004
Location: Hanover
Posts: 152
Reputation: cosi is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 1
cosi's Avatar
cosi cosi is offline Offline
Junior Poster

Re: casting error

  #8  
Oct 8th, 2004
I assume you are talking about a JList that you have populated with Strings?

Try a System.out.println(List2.getSelectedValue()). This will provide you with useful debugging information by printing exactly what kind of type it is you are dealing with in your list. It is very possible that you have a logical error and did not populate the List with Strings.


Ed

Originally Posted by freesoft_2000
hi everyone,
Sorry one more thing is that the jvm say that the line
//k is declared a string
k = (String)List2.getSelectedValue();
is a casting error but i don't think this is so.
Am i right or wrong cosi please tell me

Yours Sincerely

Richard west
In a world without walls or fences,
What use are Windows and Gates.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 10:50 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC