Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
I am learning GoF Java Design Patterns and I want to see some real life examples of them. Can you guys point to some good usage of these Design Patterns.(preferably in Java's core libraries). Thank you
I've worked in and around Java for nigh on a decade, but have managed to ever avoid doing serious work with files. Mostly I've written database driven applications, but occasionally, even those require some file io. Since I do it so rarely, I end up googling around for quite some time to figure out the exact incantation that Java requires to read a file into a byte[], char[], String or whatever...
I'd like to know the "recommended" way of reading and writing a file in clojure 1.3 . How to read the whole file How to read a file line by line How to write a new file How to add a line to an existing file Thanks.
I'm processing some Java source code using Java. I'm extracting the string literals and feed them to a function taking a String. The problem is that I need to pass the unescaped version of the String to the function (this means converting \n to a newline and \\ to a single \ and so on). Is there a function inside the Java API that does this? If not, can I obtain such functionality from some li...
I'm trying to send a POST request to my GAE app through JQuery AJAX but I get no response data back. I have a very simple servlet that simply echo the "msg" I pass in. Also overriding doOptions. @Override protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setHeader("Access-Control-Allow-Origin", "*"); resp.setHe...
I'm trying to upload a file via URLConnection, but I need to read/write it as a binary file without any encoding changes. So i've tried to read byte[] array from a FileInputStream, but now i have an issue. The PrintWriter object I use for outputing to the server does not allow me to do writer.write(content) (where content is of type byte[]). How can i fix this? Or is there another way to quick...
I have come across these two terms and my understanding of them seem to overlap with each other. Flush is used with buffers and sync is used to talk about persisting changes of file to disk. In C , fflush(stdin) makes sure that the buffer is cleared. And fsync to persist changes file to disk. If these concepts are not universally defined, would prefer a linux, java explanation. found a relat...
Why some methods that write bytes/chars to streams takes int instead of byte/char?? Someone told me in case of int instead of char: because char in java is just 2 bytes length, which is OK with most character symbols already in use, but for certain character symbols (chines or whatever), the character is being represented in more than 2 bytes, and hence we use int instead. How far this explan...
assume that I have the following code fragment operation1(); bw.close(); operation2(); When I call BufferedReader.close() from my code, I am assuming my JVM makes a system call that ensures that the buffer has been flushed and written to disk. I want to know if close() waits for the system call to complete its operation or does it proceed to operation2() without waiting for close() to finish...
Let me preface this by saying that I'm pretty new to Java. I have a file that contains a single line. The size of the file is about 200MB. I need to insert a newline character after every 309th character. I believe I have the code to do this properly, but I keep running into memory errors. I've tried increasing the heap space to no avail. Is there a less memory-intensive way of handling this?...
I want to embed a link in a JSF message, is this possible? When I try it, the rendered html of the h:messages tag escapes the html characters. I tried setting the escape attribute of the h:messages tag to false, but that didn't help.
Our app sends the contents of the derby.log file to our server whenever Apache Derby throws a SQLException in our app. In order to get detailed logs, we are setting the 'derby.infolog.append' property to true. However, we are noticing enormously large logs files since the logs also contain bootup output each time a connection is made to the database. NOTE: we are using Derby in embedded mode...
I have a text file with Chinese words written to a line. The line is surrounded with "\r\n", and written using fileOutputStream.write(string.getBytes()). I have no problems reading lines of English words, my buffered reader parses it with readLine() perfectly. However, it recognizes the Chinese sentence as multiple lines, thus screwing up my programme flow. Any solutions?
I am attempting to Store() the change made to my application's Properties. The .Properties file is located in 'resources' package, which is different from the package that contains my UI and model. I opened the package using: this.getClass().getClassLoader().getResourceAsStream("resources/settings.properties") Is there a functional equivalent of this that permits me to persist changes to th...
If I have a file, and I want to literally write '42' to it (the value, not the string), which for example is 2a in hex, how do I do it? I want to be able to use something like outfile.write(42) or outfile.write(2a) and not write the string to the file. (I realize this is a simple question but I can't find the answer of google, probably because I don't know the correct search terms)
FileInputStream reads all bytes of a file and FileOutputStream writes allbytes to a file which class do i use if i want to read all bytes of a file but line by line so that if fileA contains two lines line1 line2 then bytes of line1 and line2 are read seperately same goes for FileOutputStream
The java.io.Writer interface has two methods called append and write. What are the differences between these two? It even says that An invocation of this method of the form out.append(c) behaves in exactly the same way as the invocation out.write(c) so what is the reason for having two method name variants?
I want to know that when I use PrintWriter for writing to a file. It will write with ASCII code in the file or binary format? Thanks.
I'm on my way in developing a desktop application using netbeans(Java Dextop Application) and I need to implement my own file format which is specific to that application only. I'm quite uncertain as to how should I go about first.What code should I use so that my java application read that file and open it in a way as I want it to be.
I am writing a program that will output data to a .txt file, that can be read by a person using a program like NotePad. Perhaps not necessarily ASCII, but something that the user can understand. Which one of these do I use? ByteArrayOutputStream FileOutputStream FilterOutputStream ObjectOutputStream OutputStream PipedOutputStream I have this assignment that asks me to use one of OutputStr...
What's the difference between using a BufferedReader and a BufferedInputStream?
  /*
   * Copyright 1996-2005 Sun Microsystems, Inc.  All Rights Reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Sun designates this
   * particular file as subject to the "Classpath" exception as provided
   * by Sun in the LICENSE file that accompanied this code.
  *
  * This code is distributed in the hope that it will be useful, but WITHOUT
  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  * version 2 for more details (a copy is included in the LICENSE file that
  * accompanied this code).
  *
  * You should have received a copy of the GNU General Public License version
  * 2 along with this work; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  *
  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  * CA 95054 USA or visit www.sun.com if you need additional information or
  * have any questions.
  */
 
 package java.io;


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.

 
 
 public abstract class Writer implements AppendableCloseableFlushable {

    
Temporary buffer used to hold writes of strings and single characters
 
     private char[] writeBuffer;

    
Size of writeBuffer, must be >= 1
 
     private final int writeBufferSize = 1024;

    
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.
 
     protected Object lock;

    
Creates a new character-stream writer whose critical sections will synchronize on the writer itself.
 
     protected Writer() {
         this. = this;
     }

    
Creates a new character-stream writer whose critical sections will synchronize on the given object.

Parameters:
lock Object to synchronize on
 
     protected Writer(Object lock) {
         if (lock == null) {
             throw new NullPointerException();
         }
         this. = lock;
     }

    
Writes 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
    public void write(int cthrows IOException {
        synchronized () {
            if ( == null){
                 = new char[];
            }
            [0] = (charc;
            write(, 0, 1);
        }
    }

    
Writes an array of characters.

Parameters:
cbuf Array of characters to be written
Throws:
IOException If an I/O error occurs
    public void write(char cbuf[]) throws IOException {
        write(cbuf, 0, cbuf.length);
    }

    
Writes a portion of an array of characters.

Parameters:
cbuf Array of characters
off Offset from which to start writing characters
len Number of characters to write
Throws:
IOException If an I/O error occurs
    abstract public void write(char cbuf[], int offint lenthrows IOException;

    
Writes a string.

Parameters:
str String to be written
Throws:
IOException If an I/O error occurs
    public void write(String strthrows IOException {
        write(str, 0, str.length());
    }

    
Writes a portion of a string.

Parameters:
str A String
off Offset from which to start writing characters
len Number of characters to write
Throws:
java.lang.IndexOutOfBoundsException If off is negative, or len is negative, or off+len is negative or greater than the length of the given string
IOException If an I/O error occurs
    public void write(String strint offint lenthrows IOException {
        synchronized () {
            char cbuf[];
            if (len <= ) {
                if ( == null) {
                     = new char[];
                }
                cbuf = ;
            } else {    // Don't permanently allocate very large buffers.
                cbuf = new char[len];
            }
            str.getChars(off, (off + len), cbuf, 0);
            write(cbuf, 0, len);
        }
    }

    
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.

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
    public Writer append(CharSequence csqthrows IOException {
        if (csq == null)
            write("null");
        else
            write(csq.toString());
        return this;
    }

    
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()) 

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:
java.lang.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
    public Writer append(CharSequence csqint startint endthrows IOException {
        CharSequence cs = (csq == null ? "null" : csq);
        write(cs.subSequence(startend).toString());
        return this;
    }

    
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) 

Parameters:
c The 16-bit character to append
Returns:
This writer
Throws:
IOException If an I/O error occurs
Since:
1.5
    public Writer append(char cthrows IOException {
        write(c);
        return this;
    }

    
Flushes 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.

Throws:
IOException If an I/O error occurs
    abstract public void flush() throws IOException;

    
Closes the stream, flushing it first. Once the stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously closed stream has no effect.

Throws:
IOException If an I/O error occurs
    abstract public void close() throws IOException;
New to GrepCode? Check out our FAQ X