aspose file tools
The moose likes Beginning Java and the fly likes Files--simple doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Files--simple doubt" Watch "Files--simple doubt" New topic
Author

Files--simple doubt

Jenny raj
Ranch Hand

Joined: May 19, 2005
Posts: 57
String dirName="c:/java";
File f=new File(dirName,"Work.txt");


what does the above line do?
does it create a new file name work.txt in a specified directory c:/java
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
The way to find your answer is to look up the Javadocs for the class File, find the constructor that takes two strings as its parameters and then read the descriptive text.Javadocs link
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24061
    
  13

The File class probably should be named "Path" or "FilePath". Creating a java.io.File doesn't create a file -- it creates a path that could refer to a file. Yes, this File object will refer to the file "C:\java\work.txt". The file doesn't necessarily exist unless you force it to exist -- for example, by opening and closing a FileOutputStream with it (which would create a zero-length file

FileOutputStream os = new FileOutputStream(f);
os.close();

Of course, this would destroy the file if it already existed; you could test for this using f.exists().
[ June 27, 2006: Message edited by: Ernest Friedman-Hill ]

[Jess in Action][AskingGoodQuestions]
Jenny raj
Ranch Hand

Joined: May 19, 2005
Posts: 57
Hi ernest
your explanation is very clear,thank u
Vladimir Nesov
Greenhorn

Joined: Jun 18, 2006
Posts: 20
Note that the whole point of File is abstraction of system-specific path naming rules, so don't do things like new File("C:\my_folder"). Such things should be outside the code.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Files--simple doubt
 
Similar Threads
why it throws NullPointerException
Creating directories
How to get parent dir in shell
problrm in creation of file and dircectory
need help with the mkdirs() method of File class ?