File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes  Assigning string to  variable type File. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark " Assigning string to  variable type File." Watch " Assigning string to  variable type File." New topic
Author

Assigning string to variable type File.

Siva kandasamy
Ranch Hand

Joined: Dec 31, 2002
Posts: 139
Hi there,
Look at the code below.

Variable "file" is instance of type "File".

How could I able to assign string output to variable "file".
with no exception. ?
ie. file = file.getAbsoluteFile();

thanks
siva


Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Hi Siva,

I'm not sure exactly what you're trying to do or why, but consider this:

You can simplify that to:

Does that help? Maybe you'd find the I/O section of Sun's Java Tutorial interesting.

Jules
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
To answer your question as I literally understand it, "You can't."

A "String" object is not a "File" object. Since "file" is a reference of type "File," it can only be used to refer to objects of type "File."


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
or, for bonus fun, you could simplify it to



I've heard it takes forever to grow a woman from the ground
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Siva kandasamy:
How could I able to assign string output to variable "file".
with no exception. ?
ie. file = file.getAbsoluteFile();


getAbsoluteFile() has File as return type, not String. That's why it is working.

The conversion to a string is happening in the println method. Take a look at the source of PrintStream.println(Object) (the source is coming with the JDK, in a zip or jar file). You will see that

System.out.println(file);

is equivalent to

System.out.println(String.valueOf(file));

which will print "null" if file is null, file.toString() otherwise.

Did that help?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Siva kandasamy
Ranch Hand

Joined: Dec 31, 2002
Posts: 139
Hi
Thanks every one.

Ilja,
Thanks for answering me. Exactly.

-siva
[ September 07, 2004: Message edited by: Siva kandasamy ]
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Assigning string to variable type File.
 
Similar Threads
Listing all in a root (39 Line working code)
Problem download file.
Corrupt PDF Files
java File question
getting last piece of string