Designation standard output as File type  
Author Message
Alan





PostPosted: 2007-11-30 9:31:00 Top

java-programmer, Designation standard output as File type 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)
}
. . .
 
ram





PostPosted: 2007-11-30 9:40:00 Top

java-programmer >> Designation standard output as File type Alan <email***@***.com> writes:
>My question: To what do I set outFile (File type) to make the
>output go to standard output (display)?

If you use 籉ile?to refer to 籮ava.io.File?
there is no portable way to do this.

 
Andrew Thompson





PostPosted: 2007-11-30 9:42:00 Top

java-programmer >> Designation standard output as File type Alan wrote:
>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.

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

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200711/1

 
 
Alan





PostPosted: 2007-11-30 9:50:00 Top

java-programmer >> Designation standard output as File type 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





PostPosted: 2007-11-30 16:31:00 Top

java-programmer >> Designation standard output as File type On Thu, 29 Nov 2007 17:30:38 -0800 (PST), Alan
<email***@***.com> 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 );
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com