-
Exporting Data to a Text File
Hello,
How can I make it so that the output from this statement:
System.out.println("Hello is now in a text file")
gets copied into a text file on my desktop. Is there an easy way to
do this or is it rather difficult?
Thanks,
Mix
-
Re: Exporting Data to a Text File
You can do this from outside the program by redirecting your output:
java theClass >textfile.txt
If you aren't running from a command line, but from an IDE, then I don't
know how to do this but your IDE's documentation should tell you how, if
it's possible.
Or you can do it from inside the program by using the System.setOut() method
to redirect to a PrintStream, which you can associate with a
FileOutputStream that references your text file.
PC2
Mixelplixed <mixelplixed@hotmail.com> wrote in message
news:3a07d684$1@news.devx.com...
>
> Hello,
>
> How can I make it so that the output from this statement:
>
> System.out.println("Hello is now in a text file")
>
> gets copied into a text file on my desktop. Is there an easy way to
> do this or is it rather difficult?
>
> Thanks,
>
> Mix
>
>
>
-
Re: Exporting Data to a Text File
import java.io.*;
public class MyFirstFileWritingApp
{
// Main method
public static void main (String args[])
{
// Stream to write file
FileOutputStream fout;
try
{
// Open an output stream
fout = new FileOutputStream ("myfile.txt");
// Print a line of text
new PrintStream(fout).println ("hello world!");
// Close our output stream
fout.close();
}
// Catches any error conditions
catch (IOException e)
{
System.err.println ("Unable to write to file");
System.exit(-1);
}
}
}
--
Regards
John Timney
Microsoft MVP
(http://support.microsoft.com/support/mvp/program.asp)
Co-Author Professional JSP
ISBN: 1-861003-62-5
Mixelplixed <mixelplixed@hotmail.com> wrote in message
news:3a07d684$1@news.devx.com...
Hello,
How can I make it so that the output from this statement:
System.out.println("Hello is now in a text file")
gets copied into a text file on my desktop. Is there an easy way to
do this or is it rather difficult?
Thanks,
Mix
-
Re: Exporting Data to a Text File
That's exactly what I was looking for. I read about the Output Streams
and Print Streams but couldn't quite piece it all together.
Many Thanks,
Mix
"John Timney (MVP)" <xyztimneyj@btinternet.com> wrote:
>import java.io.*;
>
>public class MyFirstFileWritingApp
>{
> // Main method
> public static void main (String args[])
> {
> // Stream to write file
> FileOutputStream fout;
>
> try
> {
> // Open an output stream
> fout = new FileOutputStream ("myfile.txt");
>
> // Print a line of text
> new PrintStream(fout).println ("hello world!");
>
> // Close our output stream
> fout.close();
> }
> // Catches any error conditions
> catch (IOException e)
> {
> System.err.println ("Unable to write to file");
> System.exit(-1);
> }
> }
>}
>
>
>--
>Regards
>
>John Timney
>Microsoft MVP
>(http://support.microsoft.com/support/mvp/program.asp)
>Co-Author Professional JSP
>ISBN: 1-861003-62-5
>
>
>
>Mixelplixed <mixelplixed@hotmail.com> wrote in message
>news:3a07d684$1@news.devx.com...
>
>Hello,
>
> How can I make it so that the output from this statement:
>
>System.out.println("Hello is now in a text file")
>
> gets copied into a text file on my desktop. Is there an easy way to
>do this or is it rather difficult?
>
>Thanks,
>
>Mix
>
>
>
>
>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks