Writing to a Text File : Buffered Reader Writer « File Input Output « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » File Input Output » Buffered Reader WriterScreenshots 
Writing to a Text File
 


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class WriteFile {
  public static void main(String[] args) {
    Product[] movies = new Product[10];

    movies[0new Product("L"194614.95);
    movies[1new Product("T"196512.95);
    movies[2new Product("Y"197416.95);
    movies[3new Product("T"197511.95);
    movies[4new Product("S"197717.95);
    movies[5new Product("B"198716.95);
    movies[6new Product("G"198914.95);
    movies[7new Product("A"199519.95);
    movies[8new Product("G"199714.95);
    movies[9new Product("R"200119.95);

    PrintWriter out = openWriter("movies.txt");
    for (Product m : movies)
      writeMovie(m, out);
    out.close();
  }

  private static PrintWriter openWriter(String name) {
    try {
      File file = new File(name);
      PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file))true);
      return out;
    catch (IOException e) {
      System.out.println("I/O Error");
      System.exit(0);
    }
    return null;
  }

  private static void writeMovie(Product m, PrintWriter out) {
    String line = m.title;
    line += "\t" + Integer.toString(m.year);
    line += "\t" + Double.toString(m.price);
    out.println(line);
  }


}
class Product {
  public String title;

  public int year;

  public double price;

  public Product(String title, int year, double price) {
    this.title = title;
    this.year = year;
    this.price = price;
  }
}

 
Related examples in the same category
1. Use a BufferedReader to read characters from the console.
2. Read a string from console using a BufferedReader.
3. Creates a tiny text editor with BufferedReader
4. how to read lines from a file and print these to the standard output stream
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.