Designation standard output as File type

A

Alan

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)
}
.. . .
 
S

Stefan Ram

Alan said:
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.
 
A

Andrew Thompson

Alan said:
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
 
A

Alan

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
 
R

Roedy Green

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 );
 
M

Mark Rafn

Alan said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top