Writes all characters from a Reader to a file using the default character encoding. : 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 » WriterScreenshots 
Writes all characters from a Reader to a file using the default character encoding.
 
/*
 * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2006.
 *
 * Licensed under the Aduna BSD-style license.
 */
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

public class Main {
  
  /**
   * Writes all characters from a <tt>Reader</tt> to a file using the default
   * character encoding.
   *
   @param reader The <tt>Reader</tt> containing the data to write to the
   * file.
   @param file The file to write the data to.
   @return The total number of characters written.
   @throws IOException If an I/O error occured while trying to write the
   * data to the file.
   @see java.io.FileWriter
   */
  public static final long writeToFile(Reader reader, File file)
    throws IOException
  {
    FileWriter writer = new FileWriter(file);

    try {
      return transfer(reader, writer);
    }
    finally {
      writer.close();
    }
  }
  /**
   * Transfers all characters that can be read from <tt>in</tt> to
   * <tt>out</tt>.
   *
   @param in The Reader to read characters from.
   @param out The Writer to write characters to.
   @return The total number of characters transfered.
   */
  public static final long transfer(Reader in, Writer out)
    throws IOException
  {
    long totalChars = 0;
    int charsInBuf = 0;
    char[] buf = new char[4096];

    while ((charsInBuf = in.read(buf)) != -1) {
      out.write(buf, 0, charsInBuf);
      totalChars += charsInBuf;
    }

    return totalChars;
  }
}

   
  
Related examples in the same category
1. Null Writer
2. String Buffer Writer
3. Provides Closable semantics ordinarily missing in a java.io.CharArrayWriter
4. A writer for char strings
5. Write the entire contents of the supplied string to the given writer. This method always flushes and closes the writer when finished.
6. Reads characters available from the Reader and returns these characters as a String object.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.