java.io

Class Writer

Implemented Interfaces:
Appendable, Closeable, Flushable
Known Direct Subclasses:
BufferedWriter, CallbackWriter, CharArrayWriter, DotTerminatedMessageWriter, FileWriterWithEncoding, FilterWriter, JspWriter, LockableFileWriter, NullWriter, OutputStreamWriter, PipedWriter, PrintWriter, ResponseWriter, StringWriter

public abstract class Writer
extends Object
implements Appendable, Closeable, Flushable

Abstract class for writing to character streams. The only methods that a subclass must implement are write(char[], int, int), flush(), and close(). Most subclasses, however, will override some of the methods defined here in order to provide higher efficiency, additional functionality, or both.
Since:
JDK1.1
See Also:
Writer, BufferedWriter, CharArrayWriter, FilterWriter, OutputStreamWriter, FileWriter, PipedWriter, PrintWriter, StringWriter, Reader

Field Summary

protected Object
lock
The object used to synchronize operations on this stream.

Constructor Summary

Writer()
Create a new character-stream writer whose critical sections will synchronize on the writer itself.
Writer(Object lock)
Create a new character-stream writer whose critical sections will synchronize on the given object.

Method Summary

Writer
append(char c)
Appends the specified character to this writer.
Writer
append(CharSequence csq)
Appends the specified character sequence to this writer.
Writer
append(CharSequence csq, int start, int end)
Appends a subsequence of the specified character sequence to this writer.
abstract void
close()
Close the stream, flushing it first.
abstract void
flush()
Flush the stream.
void
write(cbuf[] )
Write an array of characters.
abstract void
write(cbuf[] , int off, int len)
Write a portion of an array of characters.
void
write(int c)
Write a single character.
void
write(String str)
Write a string.
void
write(String str, int off, int len)
Write a portion of a string.

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

lock

protected Object lock
The object used to synchronize operations on this stream. For efficiency, a character-stream object may use an object other than itself to protect critical sections. A subclass should therefore use the object in this field rather than this or a synchronized method.

Constructor Details

Writer

protected Writer()
Create a new character-stream writer whose critical sections will synchronize on the writer itself.

Writer

protected Writer(Object lock)
Create a new character-stream writer whose critical sections will synchronize on the given object.
Parameters:
lock - Object to synchronize on.

Method Details

append

public Writer append(char c)
            throws IOException
Appends the specified character to this writer.

An invocation of this method of the form out.append(c) behaves in exactly the same way as the invocation

     out.write(c) 
Specified by:
append in interface Appendable
Parameters:
c - The 16-bit character to append
Returns:
This writer
Throws:
IOException - If an I/O error occurs
Since:
1.5
Usages and Demos :

View More Examples of append(char c)
   1: import java.io.Reader;
   2: import java.io.Writer;
   3: 
   4:         ...
   5:             if (c == '"') {
   6:                 sb.append('\\');
   7:             }
   8:         ...
   9:             if (c == '\\') {
  10:                 sb.append('\\');
  11:             }
  12:         ...
  13:             if (c == '\n') {
  14:                 sb.append("\");\n").append("    out.write(\"");

View Full Code Here
   1: import java.io.IOException;
   2: import java.io.Writer;
   3: import java.util.Map;
   4:         ...
   5:     public void writeReport( IAnalysisModule module, AnalysisModuleData data,
   6:             Writer out )
   7:             throws IOException
   8:         ...
   9:         out.append( "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" ).
  10:             append( EOL ).append( EOL ).
  11:             append( "<coveragereport measure=\"" ).
  12:         ...
  13:         
  14:         out.append( INDENT ).append( "<classcoverage classsignature=\"" ).

View Full Code Here
   1: import java.io.IOException;
   2: import java.io.Writer;
   3: import java.util.LinkedHashSet;
   4:         ...
   5:       {
   6:         dest.append(ch);
   7:       }
   8:         ...
   9:       else if (ch == ' ') {
  10:         dest.append('+');
  11:       }
  12:         ...
  13:       else if (ch <= 0xff) {
  14:         dest.append('%');

View Full Code Here

append

public Writer append(CharSequence csq)
            throws IOException
Appends the specified character sequence to this writer.

An invocation of this method of the form out.append(csq) behaves in exactly the same way as the invocation

     out.write(csq.toString()) 

Depending on the specification of toString for the character sequence csq, the entire sequence may not be appended. For instance, invoking the toString method of a character buffer will return a subsequence whose content depends upon the buffer's position and limit.

Specified by:
append in interface Appendable
Parameters:
csq - The character sequence to append. If csq is null, then the four characters "null" are appended to this writer.
Returns:
This writer
Throws:
IOException - If an I/O error occurs
Since:
1.5
Usages and Demos :

View More Examples of append(CharSequence csq)
   1: import java.io.Reader;
   2: import java.io.Writer;
   3: 
   4:         ...
   5:             if (c == '"') {
   6:                 sb.append('\\');
   7:             }
   8:         ...
   9:             if (c == '\\') {
  10:                 sb.append('\\');
  11:             }
  12:         ...
  13:             if (c == '\n') {
  14:                 sb.append("\");\n").append("    out.write(\"");

View Full Code Here
   1: import java.io.IOException;
   2: import java.io.Writer;
   3: import java.util.Map;
   4:         ...
   5:     public void writeReport( IAnalysisModule module, AnalysisModuleData data,
   6:             Writer out )
   7:             throws IOException
   8:         ...
   9:         out.append( "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" ).
  10:             append( EOL ).append( EOL ).
  11:             append( "<coveragereport measure=\"" ).
  12:         ...
  13:         
  14:         out.append( INDENT ).append( "<classcoverage classsignature=\"" ).

View Full Code Here
   1: import java.io.IOException;
   2: import java.io.Writer;
   3: import java.util.LinkedHashSet;
   4:         ...
   5:       {
   6:         dest.append(ch);
   7:       }
   8:         ...
   9:       else if (ch == ' ') {
  10:         dest.append('+');
  11:       }
  12:         ...
  13:       else if (ch <= 0xff) {
  14:         dest.append('%');

View Full Code Here

append

public Writer append(CharSequence csq,
                     int start,
                     int end)
            throws IOException
Appends a subsequence of the specified character sequence to this writer. Appendable.

An invocation of this method of the form out.append(csq, start, end) when csq is not null behaves in exactly the same way as the invocation

     out.write(csq.subSequence(start, end).toString()) 
Specified by:
append in interface Appendable
Parameters:
csq - The character sequence from which a subsequence will be appended. If csq is null, then characters will be appended as if csq contained the four characters "null".
start - The index of the first character in the subsequence
end - The index of the character following the last character in the subsequence
Returns:
This writer
Throws:
IndexOutOfBoundsException - If start or end are negative, start is greater than end, or end is greater than csq.length()
IOException - If an I/O error occurs
Since:
1.5
Usages and Demos :

View More Examples of append(CharSequence csq,int start,int end)
   1: import java.io.OutputStream;
   2: import java.io.Writer;
   3: 
   4:         ...
   5: {
   6:     private class ForwardingWriter extends Writer {
   7:         ...
   8:         private final Writer _w;
   9:         ForwardingWriter(Writer w) {
  10:         ...
  11:                 throws IOException {
  12:             return _w.append(csq, start, end);

View Full Code Here

close

public abstract void close()
            throws IOException
Close the stream, flushing it first. Once a stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously-closed stream, however, has no effect.
Specified by:
close in interface Closeable
Throws:
IOException - If an I/O error occurs
Usages and Demos :

View More Examples of close()
   1:   {
   2:     Writer fw = null;
   3: 
   4:         ...
   5:       if ( fw != null )
   6:         try { fw.close(); } catch ( IOException e ) { e.printStackTrace(); }
   7:     }
   8:   }
   9: }

View Full Code Here
   1: 
   2:   private Writer out;
   3: 
   4:         ...
   5:     out.flush();
   6:     out.close();
   7:   }
   8:         ...
   9:     try {
  10:       this.close();
  11:     }

View Full Code Here
   1: import java.io.IOException;
   2: import java.io.Writer;
   3: 
   4:         ...
   5:     public static void writeFile(File file, String content) throws IOException {
   6:         Writer out = new FileWriter(file);
   7:         out.write(content);
   8:         ...
   9:         out.flush();
  10:         out.close();
  11:     }

View Full Code Here
   1:   public static void fromReader( Reader in, CLOB theClob ) throws SQLException {
   2:     Writer out = theClob.getCharacterOutputStream();
   3:     int chunk = theClob.getChunkSize();
   4:         ...
   5:       }
   6:     in.close();
   7:         ...
   8:     out.close();
   9:     }

View Full Code Here
   1: 
   2: import java.io.Writer;
   3: import java.io.IOException;
   4:         ...
   5: {
   6:     public static void attemptClose(Writer os)
   7:     {
   8:         ...
   9:       try
  10:     {if (os != null) os.close();}
  11:       catch (IOException e)

View Full Code Here

flush

public abstract void flush()
            throws IOException
Flush the stream. If the stream has saved any characters from the various write() methods in a buffer, write them immediately to their intended destination. Then, if that destination is another character or byte stream, flush it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams.

If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.

Specified by:
flush in interface Flushable
Throws:
IOException - If an I/O error occurs
Usages and Demos :

View More Examples of flush()
   1: 
   2:   private Writer out;
   3:   
   4:         ...
   5:   public TagStripper(Writer out) {
   6:     this.out = out; 
   7:         ...
   8:       out.write(text);
   9:       out.flush(); 
  10:     }

View Full Code Here
   1: 
   2:   private Writer out;
   3: 
   4:         ...
   5:   public void close() throws IOException {
   6:     out.flush();
   7:     out.close();
   8:   }
   9:   

View Full Code Here
   1: import java.io.IOException;
   2: import java.io.Writer;
   3: 
   4:         ...
   5:     public static void writeFile(File file, String content) throws IOException {
   6:         Writer out = new FileWriter(file);
   7:         out.write(content);
   8:         ...
   9:         out.flush();
  10:         out.close();

View Full Code Here
   1:             
   2:             Writer out = new OutputStreamWriter(System.out);
   3:             handler = new MyHandler(out);
   4:         ...
   5:             
   6:             out.flush();
   7:         }
   8:         catch (Exception e) 
   9:         {

View Full Code Here
   1:             
   2:             Writer out = new OutputStreamWriter(System.out);
   3:             handler = new MyHandlerShort(out);
   4:         ...
   5:             
   6:             out.flush();
   7:         }
   8:         catch (Exception e) 
   9:         {

View Full Code Here

write

public void write(cbuf[] )
            throws IOException
Write an array of characters.
Parameters:
Throws:
IOException - If an I/O error occurs
Usages and Demos :

View More Examples of write(cbuf[] )
   1: import java.io.IOException;
   2: import java.io.Writer;
   3: 
   4:         ...
   5: 
   6:   public void writeAsEncodedUnicode(Writer writer) 
   7:     throws XMLStreamException
   8:         ...
   9:     try {
  10:       writer.write("<?" + _target);
  11: 
  12:         ...
  13:       if (_data != null && ! "".equals(_data))
  14:         writer.write(" " + _data);

View Full Code Here
   1: 
   2: import java.io.Writer;
   3: import java.io.IOException;
   4:         ...
   5:     
   6:     public void exportGames(Game[] games, Writer writer) throws IOException {
   7:         for(int i=0; i<games.length; i++) {
   8:         ...
   9:             if(i > 0) {
  10:                 writer.write("\n");
  11:             }
  12:         ...
  13:             if(!games[i].getInitialPosition().equals(Position.initialPosition())) {
  14:                 writer.write("[FEN \"");

View Full Code Here
   1: import java.io.IOException;
   2: import java.io.Writer;
   3: import java.util.Iterator;
   4:         ...
   5:     
   6:     public void write(MultipageModel multipage, Writer out) throws IOException {
   7:         writeHeader(out);
   8:         ...
   9:         out.write("<"+ MultipageParser.MULTIPAGE_TAG + " " + MultipageParser.TITLE_TAG + "=\"");
  10:         ...
  11:         out.write(multipage.getTitle());

View Full Code Here
   1: 
   2: import java.io.Writer;
   3: import java.util.Locale;
   4:         ...
   5: public class TextDiffOutput implements DiffOutput {
   6:     private Writer writer;
   7:     private boolean markLines;
   8:         ...
   9:     public void endLine() throws Exception {
  10:         writer.write(contentLine.toString());
  11:         ...
  12:         writer.write('\n');

View Full Code Here
   1: import java.io.IOException;
   2: import java.io.Writer;
   3: import java.util.Iterator;
   4:         ...
   5:     
   6:     public void writeModel(Object m, Writer writer)
   7:             throws IOException {
   8:         ...
   9:         writeHeader(writer);
  10:         writer.write("<" + SketchParser.MODEL_TAG +">\n");
  11:         for(Iterator iter = model.symbols(); iter.hasNext();){
  12:         ...
  13:         }    
  14:         writer.write("</" + SketchParser.MODEL_TAG + ">\n");

View Full Code Here

write

public abstract void write(cbuf[] ,
                           int off,
                           int len)
            throws IOException
Write a portion of an array of characters.
Parameters:
off - Offset from which to start writing characters
len - Number of characters to write
Throws:
IOException - If an I/O error occurs
Usages and Demos :

View More Examples of write(cbuf[] ,int off,int len)
   1: 
   2:   protected Writer writer;
   3: 
   4:         ...
   5:     try {
   6:       this.writer = new BufferedWriter(new OutputStreamWriter(out));
   7:       XmlPullParserFactory factory = XmlPullParserFactory.newInstance(System.getProperty(XmlPullParserFactory.PROPERTY_NAME), null);
   8:         ...
   9:     flush();
  10:     writer.write(Base64.encodeBytes(bytes, off, len));
  11:   }

View Full Code Here
   1: import java.io.IOException;
   2: import java.io.Writer;
   3: import java.util.ArrayList;
   4:         ...
   5: 
   6:     public void drawJavascriptSources(Writer out) throws IOException {
   7:         Iterator it = this.getJavascriptSources().iterator();
   8:         ...
   9: 
  10:     public void drawCssSources(Writer out) throws IOException {
  11:         Iterator it = this.getCssSources().iterator();

View Full Code Here
   1:     
   2:     public void render(User user, Archive archive, EntryContainer entry, String urlPrefix, Writer out) throws IOException {
   3:         if (entry == null) return;
   4:         ...
   5:         out.write("    <title>" + subject + "</title>\n");
   6:         out.write("    <link>" + urlPrefix + BlogRenderer.getEntryURL(entry, info, true) + "</link>\n");
   7:         out.write("    <pubDate>" + getRFC822Date(entry.getURI().getEntryId()) + "</pubDate>\n");
   8:         PetName pn = user.getPetNameDB().getByLocation(entry.getURI().getKeyHash().toBase64());
   9:         String author = null;

View Full Code Here
   1: import java.io.IOException;
   2: import java.io.Writer;
   3: 
   4:         ...
   5: 
   6:     public void dump(Writer writer) throws IOException {
   7:         for (SaxBit saxbit : saxbits) {
   8:         ...
   9:         public void dump(Writer writer) throws IOException {
  10:             writer.write("[StartDTD] name=" + name + ",publicId=" + publicId + ",systemId=" + systemId + "\n");
  11:         }
  12:         ...
  13:         public void dump(Writer writer) throws IOException {
  14:             writer.write("[StartElement] namespaceURI=" + namespaceURI + ",localName=" + localName + ",qName=" + qName + "\n");

View Full Code Here
   1: import java.io.OutputStreamWriter;
   2: import java.io.Writer;
   3: 
   4:         ...
   5: 
   6:     static class IndentWriter extends Writer {
   7:         ...
   8:         protected Writer proxied;
   9:         protected int    indentLevel;
  10:         ...
  11:                 if (temp > SPACES_LEN) {
  12:                     proxied.write(SPACES, 0, SPACES_LEN);

View Full Code Here

write

public void write(int c)
            throws IOException
Write a single character. The character to be written is contained in the 16 low-order bits of the given integer value; the 16 high-order bits are ignored.

Subclasses that intend to support efficient single-character output should override this method.

Parameters:
c - int specifying a character to be written.
Throws:
IOException - If an I/O error occurs
Usages and Demos :

View More Examples of write(int c)
   1: import java.io.IOException;
   2: import java.io.Writer;
   3: 
   4:         ...
   5: 
   6:   public void writeAsEncodedUnicode(Writer writer) 
   7:     throws XMLStreamException
   8:         ...
   9:     try {
  10:       writer.write("<?" + _target);
  11: 
  12:         ...
  13:       if (_data != null && ! "".equals(_data))
  14:         writer.write(" " + _data);

View Full Code Here
   1: 
   2: import java.io.Writer;
   3: import java.io.IOException;
   4:         ...
   5:     
   6:     public void exportGames(Game[] games, Writer writer) throws IOException {
   7:         for(int i=0; i<games.length; i++) {
   8:         ...
   9:             if(i > 0) {
  10:                 writer.write("\n");
  11:             }
  12:         ...
  13:             if(!games[i].getInitialPosition().equals(Position.initialPosition())) {
  14:                 writer.write("[FEN \"");

View Full Code Here
   1: import java.io.IOException;
   2: import java.io.Writer;
   3: import java.util.Iterator;
   4:         ...
   5:     
   6:     public void write(MultipageModel multipage, Writer out) throws IOException {
   7:         writeHeader(out);
   8:         ...
   9:         out.write("<"+ MultipageParser.MULTIPAGE_TAG + " " + MultipageParser.TITLE_TAG + "=\"");
  10:         ...
  11:         out.write(multipage.getTitle());

View Full Code Here
   1: 
   2: import java.io.Writer;
   3: import java.util.Locale;
   4:         ...
   5: public class TextDiffOutput implements DiffOutput {
   6:     private Writer writer;
   7:     private boolean markLines;
   8:         ...
   9:     public void endLine() throws Exception {
  10:         writer.write(contentLine.toString());
  11:         ...
  12:         writer.write('\n');

View Full Code Here
   1: import java.io.IOException;
   2: import java.io.Writer;
   3: import java.util.Iterator;
   4:         ...
   5:     
   6:     public void writeModel(Object m, Writer writer)
   7:             throws IOException {
   8:         ...
   9:         writeHeader(writer);
  10:         writer.write("<" + SketchParser.MODEL_TAG +">\n");
  11:         for(Iterator iter = model.symbols(); iter.hasNext();){
  12:         ...
  13:         }    
  14:         writer.write("</" + SketchParser.MODEL_TAG + ">\n");

View Full Code Here

write

public void write(String str)
            throws IOException
Write a string.
Parameters:
str - String to be written
Throws:
IOException - If an I/O error occurs
Usages and Demos :

View More Examples of write(String str)
   1: import java.io.IOException;
   2: import java.io.Writer;
   3: 
   4:         ...
   5: 
   6:   public void writeAsEncodedUnicode(Writer writer) 
   7:     throws XMLStreamException
   8:         ...
   9:     try {
  10:       writer.write("<?" + _target);
  11: 
  12:         ...
  13:       if (_data != null && ! "".equals(_data))
  14:         writer.write(" " + _data);

View Full Code Here
   1: 
   2: import java.io.Writer;
   3: import java.io.IOException;
   4:         ...
   5:     
   6:     public void exportGames(Game[] games, Writer writer) throws IOException {
   7:         for(int i=0; i<games.length; i++) {
   8:         ...
   9:             if(i > 0) {
  10:                 writer.write("\n");
  11:             }
  12:         ...
  13:             if(!games[i].getInitialPosition().equals(Position.initialPosition())) {
  14:                 writer.write("[FEN \"");

View Full Code Here
   1: import java.io.IOException;
   2: import java.io.Writer;
   3: import java.util.Iterator;
   4:         ...
   5:     
   6:     public void write(MultipageModel multipage, Writer out) throws IOException {
   7:         writeHeader(out);
   8:         ...
   9:         out.write("<"+ MultipageParser.MULTIPAGE_TAG + " " + MultipageParser.TITLE_TAG + "=\"");
  10:         ...
  11:         out.write(multipage.getTitle());

View Full Code Here
   1: 
   2: import java.io.Writer;
   3: import java.util.Locale;
   4:         ...
   5: public class TextDiffOutput implements DiffOutput {
   6:     private Writer writer;
   7:     private boolean markLines;
   8:         ...
   9:     public void endLine() throws Exception {
  10:         writer.write(contentLine.toString());
  11:         ...
  12:         writer.write('\n');

View Full Code Here
   1: import java.io.IOException;
   2: import java.io.Writer;
   3: import java.util.Iterator;
   4:         ...
   5:     
   6:     public void writeModel(Object m, Writer writer)
   7:             throws IOException {
   8:         ...
   9:         writeHeader(writer);
  10:         writer.write("<" + SketchParser.MODEL_TAG +">\n");
  11:         for(Iterator iter = model.symbols(); iter.hasNext();){
  12:         ...
  13:         }    
  14:         writer.write("</" + SketchParser.MODEL_TAG + ">\n");

View Full Code Here

write

public void write(String str,
                  int off,
                  int len)
            throws IOException
Write a portion of a string.
Parameters:
str - A String
off - Offset from which to start writing characters
len - Number of characters to write
Throws:
IOException - If an I/O error occurs
Usages and Demos :

View More Examples of write(String str,int off,int len)
   1: 
   2:   protected Writer writer;
   3: 
   4:         ...
   5:     try {
   6:       this.writer = new BufferedWriter(new OutputStreamWriter(out));
   7:       XmlPullParserFactory factory = XmlPullParserFactory.newInstance(System.getProperty(XmlPullParserFactory.PROPERTY_NAME), null);
   8:         ...
   9:     flush();
  10:     writer.write(Base64.encodeBytes(bytes, off, len));
  11:   }

View Full Code Here
   1: import java.io.IOException;
   2: import java.io.Writer;
   3: import java.util.ArrayList;
   4:         ...
   5: 
   6:     public void drawJavascriptSources(Writer out) throws IOException {
   7:         Iterator it = this.getJavascriptSources().iterator();
   8:         ...
   9: 
  10:     public void drawCssSources(Writer out) throws IOException {
  11:         Iterator it = this.getCssSources().iterator();

View Full Code Here
   1:     
   2:     public void render(User user, Archive archive, EntryContainer entry, String urlPrefix, Writer out) throws IOException {
   3:         if (entry == null) return;
   4:         ...
   5:         out.write("    <title>" + subject + "</title>\n");
   6:         out.write("    <link>" + urlPrefix + BlogRenderer.getEntryURL(entry, info, true) + "</link>\n");
   7:         out.write("    <pubDate>" + getRFC822Date(entry.getURI().getEntryId()) + "</pubDate>\n");
   8:         PetName pn = user.getPetNameDB().getByLocation(entry.getURI().getKeyHash().toBase64());
   9:         String author = null;

View Full Code Here
   1: import java.io.IOException;
   2: import java.io.Writer;
   3: 
   4:         ...
   5: 
   6:     public void dump(Writer writer) throws IOException {
   7:         for (SaxBit saxbit : saxbits) {
   8:         ...
   9:         public void dump(Writer writer) throws IOException {
  10:             writer.write("[StartDTD] name=" + name + ",publicId=" + publicId + ",systemId=" + systemId + "\n");
  11:         }
  12:         ...
  13:         public void dump(Writer writer) throws IOException {
  14:             writer.write("[StartElement] namespaceURI=" + namespaceURI + ",localName=" + localName + ",qName=" + qName + "\n");

View Full Code Here
   1: import java.io.OutputStreamWriter;
   2: import java.io.Writer;
   3: 
   4:         ...
   5: 
   6:     static class IndentWriter extends Writer {
   7:         ...
   8:         protected Writer proxied;
   9:         protected int    indentLevel;
  10:         ...
  11:                 if (temp > SPACES_LEN) {
  12:                     proxied.write(SPACES, 0, SPACES_LEN);

View Full Code Here