Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Files--simple doubt  RSS feed

 
Jenny raj
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jenny raj
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ernest
your explanation is very clear,thank u
 
Vladimir Nesov
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!