Code Snippets

  

Java Source Code


Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 98,764 Java Programmers for FREE!. Ask your question and get quick answers from Dream.In.Code experts. There are 1,036 online right now! We're the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Java Expert

Register to Make This Box Go Away!



File Output

Shows you how to output to a .txt file using java.

Submitted By: BlackJesus
Actions:
Rating:
Views: 10,583

Language: Java

Last Modified: March 2, 2005
Instructions: change class to your class name. Also when you go to view the file you created, it will be in the classes folder in your project folder.

Snippet


  1. import java.io.*;
  2.  
  3. public class Txt
  4. {
  5.      public static FileOutputStream Output;
  6.      public static PrintStream file;
  7.      
  8.      public static void main(String[] args)
  9.      {
  10.           //must use a try/catch statement for it to work
  11.           try
  12.           {     
  13.                Output = new FileOutputStream("blah.txt");
  14.                file = new PrintStream(Output);
  15.           }
  16.           catch(Exception e)
  17.           {
  18.                System.out.println("Could not load file!");
  19.           }
  20.          
  21.           file.println("This is printed in the file!");
  22.          
  23.      }
  24. }

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month
-->