Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

Java Source Code


Welcome to Dream.In.Code
Become a Java Expert!

Join 300,313 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,062 people online right now. Registration is fast and FREE... Join Now!





File Output

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

Submitted By: BlackJesus
Actions:
Rating:
Views: 20,046

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!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month