Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

Designation standard output as File type

Thread view: 
Alan - 30 Nov 2007 01:30 GMT
I have a method (fragment below) that takes a File type "outFile"
as the output file.  However, if the user of this method specifies
"null" for that parameter, I want to make the output go to standard
output instead of a file.

  My question: To what do I set outFile (File type) to make the
output go to standard output (display)?  System.out is of a type
PrintStream.

       Thanks, Alan

public static void printData(File dir, File outFile)
{
  try
  {
     if (outFile == null)
     {
        outFile = ?????;  // Default is standard output (System.out)
     }
. . .
Stefan Ram - 30 Nov 2007 01:39 GMT
>My question: To what do I set outFile (File type) to make the
>output go to standard output (display)?

 If you use »File« to refer to »java.io.File«,
 there is no portable way to do this.
Andrew Thompson - 30 Nov 2007 01:42 GMT
>I have a method (fragment below) that takes a File type "outFile"
>as the output file.  ...System.out is of a type
>PrintStream.
...
>public static void printData(File dir, File outFile)

This is probably better specified as

public static void printData(InputStream is, OuputStream os)

Then it might be called in code (something) like this..

 // redirect the streams
 printData( new FileInputStream(dir), System.out );

As an aside.  Why is your initial 'File' parameter called
'dir'?  That suggests a directory to me.

Signature

Andrew Thompson
http://www.physci.org/

Alan - 30 Nov 2007 01:49 GMT
I am traversing subdirectories and processing certain types of
files found.  So, I test dir to see if it is a directory or a file
(both of type File).

Alan
Roedy Green - 30 Nov 2007 08:31 GMT
On Thu, 29 Nov 2007 17:30:38 -0800 (PST), Alan
<jalanthomas@verizon.net> wrote, quoted or indirectly quoted someone
who said :

>   I have a method (fragment below) that takes a File type "outFile"
>as the output file.  However, if the user of this method specifies
>"null" for that parameter, I want to make the output go to standard
>output instead of a file.

See if this works:

final PrintStream ps;

if ( f == null )
 {
 ps = System.out;
 }
else
{
ps = new PrintStream ( new File(f) );
}

final PrintWriter pw = new PrintWriter( ps );
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Mark Rafn - 30 Nov 2007 21:24 GMT
>   My question: To what do I set outFile (File type) to make the
>output go to standard output (display)?  System.out is of a type
>PrintStream.

Right.  The first line of javadoc for java.io.File makes this clear: "An
abstract representation of file and directory pathnames."

stdout is not a path name.  It's an output stream.

>public static void printData(File dir, File outFile)

You'll need to write a printData method that takes an OutputStream (or better,
a Writer).  There is no path for stdout, so there is no File object that
represents it.
--
Mark Rafn    dagon@dagon.net    <http://www.dagon.net/>


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.