• Post Reply Bookmark Topic Watch Topic
  • New Topic

File question  RSS feed

 
jarod weber
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys ive just started learning java and was wondering if we can send a file as a parameter to a method...for ex if i have a file named enduser.xml and i want to use it in my application...can i do something like this
class PlayFile
{
public void playWithFile(File file)
{// do some stuff with the file
}

public static void main (String args[])
{
PlayFile ex = new PlayFile();
ex.playWithFile(enduser.xml);
}
}
q1:can we consider file as an argument type for a method?
q2 oes the enduser.xml file need to be in a specific folder ?
q3:should i give the path of the file as a string instead?
please let me know
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> public void playWithFile(File file)
Yes that will work. There is a File class. You need to read its doc.

> ex.playWithFile(enduser.xml);

If enduser.xml is the name of a file, that probably won't work. The parameter for the playWithFile() is an object of type File. Your example doesn't show what enduser.xml is. If its the name of a file, then you need to use the File constructor to create a File object.

I'd suggest you read some about how to use objects and Strings and how to call methods before going too much farther.
 
pals meh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey norm thanx for your input
theres still something im not clear on.
If we assume that enduser.xml IS the name of an xml file in some folder on my machine..then can we just send this file as a parameter to any method??
example : playwithfile(enduser.xml)/* I know that the method signature doesnt match with the one i have in the example but i wanna know if theres a way in which a file on my system can be sent as a parameter to some method without having created a file object for that file??*/
or do we have to create a file object from that file(enduser.xml) and then send that file object as a parameter to some method.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somewhere you will need to convert the filename (a String object) into a File object, via "new File(filename)". Where you do that is a matter of how you want your API to look like. File objects do not have a large overhead, so you shouldn't be afraid of creating them as needed.
[ October 24, 2005: Message edited by: Ulf Dittmer ]
 
jarod weber
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx a lot for your help
:-)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!