• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

File constructor

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
File directory = new File(�d�);
File file = new File(directory,�f�);
Can you explain this statements.
Thanks.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lets take an example. Suppose your program is in the directory

/home/siddharth/java

then

File directory = new File("d");

here directory will point to the path

/home/siddharth/java/d

this d can be a file or directory. Now the second statement

File file = new File(directory,"f");

Now this object named file will point to the path

/home/siddharth/java/d/f

so basically if you provide two parameters to the file constructor, then the first parameter points to the directory inside which the second parameter will point to...
 
siddharth das
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if I write the following statement
File file = new File(directory,"h.txt");
instead of
File file = new File(directory,"f");

then what will happen?
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
then file = new File(directory,"h.txt"); will point to h.txt inside the directory pointed by directory object...
 
siddharth das
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But I do not find any text file is created.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Dear,

You need to add following statement also as such new File will not create physical file for you.

file.createNewFile() if this is true then your file is created else not.
 
siddharth das
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public static void main(String[] args) {
File p = new File("a");
File f = new File(p,"h.txt");
System.out.println(f.getAbsolutePath());

try

{
f.createNewFile();
}
catch(Exception e){System.out.println("test");}



}

In this case,there is no text file is created.But exception is thrown.
Output is test
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does your directory "a" exist before the creation of the file?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to create dir.

File p = new File("a");
p.mkdir();
File f = new File(p,"h.txt");
System.out.println(f.getAbsolutePath());

try

{
f.createNewFile();
}
catch(Exception e){System.out.println("test");}



}
 
That new kid is a freak. Show him this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic