Exporting Data to a Text File


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Exporting Data to a Text File

  1. #1
    Mixelplixed Guest

    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




  2. #2
    Paul Clapham Guest

    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
    >
    >
    >




  3. #3
    John Timney (MVP) Guest

    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






  4. #4
    Mixelplixed Guest

    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center