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,315 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,058 people online right now. Registration is fast and FREE... Join Now!





Using FileWriter to write text to a file

The easiest way to write to a file, without any weird characters showing.

Submitted By: tygerberg
Actions:
Rating:
Views: 22,933

Language: Java

Last Modified: October 2, 2007

Snippet


  1. import java.io.*;
  2. public class Firstfile
  3. {
  4.     public static void main (String[] args)throws IOException
  5.     {
  6.         FileWriter first = new FileWriter(new File("first.txt"));
  7.        
  8. /*"FileWriter first = new FileWriter(new File("first.txt"));"
  9. FileWriter first = new FileWriter indicates that a new FileWriter is in use.
  10. (new File("first.txt")); creates the file called first, in quotations.*/
  11.        
  12.  
  13.         int s, x, a;
  14.         s = 10;         
  15.         x = 10;       
  16.         a = s + x;     
  17.  
  18. //Small calculation to show what can be done.
  19.  
  20. //It's very simple, using only xyz.write("");
  21.  
  22.         first.write("First file in java");    //The following lines writes
  23.         first.write("s + x = "+ a);         //to the file.
  24.         first.close();                           //Closes the file
  25.  
  26.  
  27.     } // main method
  28. } // Firstfile class
  29.  

Copy & Paste


Comments


Nelao 2008-06-16 07:15:36

i am a young female who just got attracted by the java ptogramming and as i am now learning this, java is geting complecated evey day, now i looking for help with my java project. my project is about keeping record of a customer, and a vedio in the video shop. and save this to a cvs file, and read this again. How do I list all the customers listed in the file when trying to retrieve a customer?


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