use Of printf()

Dear friends,

in one of the many programs i found that they are using printf in System.out.printf();

Is that possible to use printf() in java, how?, when should we use it?

The code is :

publicclass FileDemonstration

{

// display information about file user specifies

publicvoid analyzePath( String path )

{

// create File object based on user input

File name =new File( path );

if ( name.exists() )// if name exists, output information about it

{

// display file (or directory) information

System.out.printf(

"%s%s\n%s\n%s\n%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s",

name.getName()," exists",

( name.isFile() ?"is a file" :"is not a file" ),

( name.isDirectory() ?"is a directory" :

"is not a directory" ),

( name.isAbsolute() ?"is absolute path" :

"is not absolute path" ),"Last modified: ",

name.lastModified(),"Length: ", name.length(),

"Path: ", name.getPath(),"Absolute path: ",

name.getAbsolutePath(),"Parent: ", name.getParent() );

if ( name.isDirectory() )// output directory listing

{

String directory[] = name.list();

System.out.println("\n\nDirectory contents:\n" );

for ( String directoryName : directory )

System.out.printf("%s\n", directoryName );

}// end else

}// end outer if

else// not file or directory, output error message

{

System.out.printf("%s %s", path,"does not exist." );

}// end else

}// end method analyzePath

}// end class FileDemonstration

Thank you

[2969 byte] By [Zorama] at [2007-11-27 11:10:32]
# 1

When you want to use it! It's a feature added recently to keep the drooling C coders happy

georgemca at 2007-7-29 13:42:20 > top of Java-index,Java Essentials,Java Programming...
# 2

Do I need to import a package for using it... as it is related with C.

Zorama at 2007-7-29 13:42:20 > top of Java-index,Java Essentials,Java Programming...
# 3

> Do I need to import a package for using it... as it

> is related with C.

It's not related to C* as such, just stolen from it. It's a method on PrintStream is all

(*: might be from a predecessor of 'C', like B or BCPL - I'm not sure. Someone else will probably correct me)

georgemca at 2007-7-29 13:42:20 > top of Java-index,Java Essentials,Java Programming...
# 4

> > Do I need to import a package for using it... as it

> > is related with C.

>

> It's not related to C* as such, just stolen from it.

> It's a method on PrintStream is all

>

> (*: might be from a predecessor of 'C', like B or

> BCPL - I'm not sure. Someone else will probably

> correct me)

According Wikipedia, it originated from C:

http://en.wikipedia.org/wiki/Printf

prometheuzza at 2007-7-29 13:42:20 > top of Java-index,Java Essentials,Java Programming...
# 5

> > > Do I need to import a package for using it... as

> it

> > > is related with C.

> >

> > It's not related to C* as such, just stolen from

> it.

> > It's a method on PrintStream is all

> >

> > (*: might be from a predecessor of 'C', like B or

> > BCPL - I'm not sure. Someone else will probably

> > correct me)

>

> According Wikipedia, it originated from C:

> http://en.wikipedia.org/wiki/Printf

Cool! I am fully "Jimbos Big Bag of Trivia"-compliant :-)

Cheers

georgemca at 2007-7-29 13:42:20 > top of Java-index,Java Essentials,Java Programming...
# 6

Thanks for the information.

Zorama at 2007-7-29 13:42:20 > top of Java-index,Java Essentials,Java Programming...