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
In Java, I have a String and I want to encode it as a byte array (in UTF8, or some other encoding). Alternately, I have a byte array (in some known encoding) and I want to convert it into a Java String. How do I do these conversions?
The obvious answer is to use Charset.defaultCharset() but we recently found out that this might not be the right answer. I was told that the result is different from real default charset used by java.io classes in several occasions. Looks like Java keeps 2 sets of default charset. Does anyone have any insights on this issue? We were able to reproduce one fail case. It's kind of user error but ...
We are trying to download source of webpages, however we cannot see some specific characters -like ü,ö,ş,ç- propoerly due to character encoding. We tried the following code in order to convert encoding of the string ("text" variable): byte[] xyz = text.getBytes(); text = new String(xyz,"windows-1254"); We observed that if encoding is utf-8, we still cannot see pages correctly. What should w...
I've been doing some socket programming to transmit information across the wire. I've run into a problem with DataOutputStream.writeUTF(). It seems to allow strings of up to 64k but I have a few situations where I can run over this. Are there any good alternatives that support larger strings or do I need to roll my own?
Before when I use file in my fileWritter it worked, but now since I'm using getResourceAsStream instead of file how can I make it work? FileWriter fstream = new FileWriter(new File("filename"), true); Now when I pass InputStream is = getResourceAsStream("filename"); FileWriter fstream = new FileWriter(is, true); I had to change it because when I create runable jar with mave assembly plu...
I need to encode a message from request and write it into a file. Currently I am using the URLEncoder.encode() method for encoding. But it is not giving the expected result for special characters in French and Dutch. I have tried using URLEncoder.encode("msg", "UTF-8") also. Example: Original message: Pour gérer votre GSM After encoding: Pour g?rer votre GSM Can any one tell me which metho...
I am having this encoding issue in java, one string I actually need to handle is the response from running "systeminfo" command under windows commandline, and I need to present the result in a html document. The problem is if I run my application on French operating system, the garbled characters are shown in the html, no matter how I tried to convert the encodeing settings. From the log, I ca...
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)
I´m trying write strings in diffrent languages to a rtf file. I hav tried a few different things. I use japanese here as an example but it´s the same for other languages i have tried. public void writeToFile(){ String strJapanese = "日本語"; DataOutputStream outStream; File file = new File("C:\\file.rtf"); try{ outStream = new DataOutputStream(new FileOutputStream(file)...
I want to read a webpage A in ISO-8859-1 charset, according to the browser, and return the content in UTF-8 as a content of the webpage B. This is: I want to show the content of the page A in the same charset that I use to show the rest of the page B, that is UTF-8. How do I do this in java/groovy? thanks in advance
I am running into issues when displaying special characters on the Windows console. I have written the following code: public static void main(String[] args) throws IOException { File newFile = new File("sampleInput.txt"); File newOutFile = new File("sampleOutput.txt"); FileReader read = new FileReader(newFile); FileWriter write = new FileWriter(newOutFile); PushbackReade...
What's the difference between using a BufferedReader and a BufferedInputStream?
A servlet response wrapper is being used in a Servlet Filter. The idea is that the response is manipulated, with a 'nonce' value being injected into forms, as part of defence against CSRF attacks. The web app is using UTF-8 everywhere. When the Servlet Filter is absent, no problems. When the filter is added, encoding issues occur. (It seems as if the response is reverting to 8859-1.) The guts...
i want how to get the content from websites with utf8 format,, i have writing the following code is try { String webnames = "http://pathivu.com"; URL url = new URL(webnames); URLConnection urlc = url.openConnection(); //BufferedInputStream buffer = new BufferedInputStream(urlc.getInputStream()); BufferedReader buffer = new BufferedReader(new InputStr...
I am sorry if it has been asked before. I am trying to process a text file using Java. The text file is exported from MS SQLServer. When I open it in PSPad (sort of text editor in which I can view any file in hex format), it tells me that my text file is in UTF-16LE. Since I am getting it from someone else, it is quite possible. Now my Java program is not able to deal with that format. So I w...
We have an Java application running on Weblogic server that picks up XML messages from a JMS or MQ queue and writes it into another JMS queue. The application doesn't modify the XML content in any way. We use BEA's XMLObject to read and write the messages into queues. The XML messages contain the encoding type declarations as UTF-8. We have an issue when the XML contains characters that are o...
I am using FileWriter and I have noticed strange behavior. I buffer my collection myself and every x rows I use IOUtils.writelines(myList,"\n", writer ); It doesnt write to the file. I continue to call it with more lines and only after it is very full it writes to the file. Does it use a buffer? I cant find it in its documentation.
I'm trying to build a Chinese flashcards program in Java to help myself learn Chinese. I'm using intelliJ IDEA 10. The basic process is that my program will read a file saved on the local machine to generate the flashcards. The file is written using the File class in java. When opened in notepad, it displays all characters properly. When I run it in the IDE I am able to display Chinese charac...
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...
i want to read a file one character at a time and write the contents of first file to another file one character at a time. i have asked this question earlier also but didnt get a satisfactory answer..... i am able to read the file and print it out to std o/p.but cant write the same read character to a file.
  /*
   * Copyright 1996-2006 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;
 
An OutputStreamWriter is a bridge from character streams to byte streams: Characters written to it are encoded into bytes using a specified java.nio.charset.Charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.

Each invocation of a write() method causes the encoding converter to be invoked on the given character(s). The resulting bytes are accumulated in a buffer before being written to the underlying output stream. The size of this buffer may be specified, but by default it is large enough for most purposes. Note that the characters passed to the write() methods are not buffered.

For top efficiency, consider wrapping an OutputStreamWriter within a BufferedWriter so as to avoid frequent converter invocations. For example:

 Writer out
   = new BufferedWriter(new OutputStreamWriter(System.out));
 

A surrogate pair is a character represented by a sequence of two char values: A high surrogate in the range '\uD800' to '\uDBFF' followed by a low surrogate in the range '\uDC00' to '\uDFFF'.

A malformed surrogate element is a high surrogate that is not followed by a low surrogate or a low surrogate that is not preceded by a high surrogate.

This class always replaces malformed surrogate elements and unmappable character sequences with the charset's default substitution sequence. The java.nio.charset.CharsetEncoder class should be used when more control over the encoding process is required.

Author(s):
Mark Reinhold
Since:
JDK1.1
See also:
BufferedWriter
OutputStream
java.nio.charset.Charset
 
 
 public class OutputStreamWriter extends Writer {
 
     private final StreamEncoder se;

    
Creates an OutputStreamWriter that uses the named charset.

Parameters:
out An OutputStream
charsetName The name of a supported charset
Throws:
UnsupportedEncodingException If the named encoding is not supported
 
     public OutputStreamWriter(OutputStream outString charsetName)
         throws UnsupportedEncodingException
     {
         super(out);
         if (charsetName == null)
             throw new NullPointerException("charsetName");
         = StreamEncoder.forOutputStreamWriter(outthischarsetName);
    }

    
Creates an OutputStreamWriter that uses the default character encoding.

Parameters:
out An OutputStream
    public OutputStreamWriter(OutputStream out) {
        super(out);
        try {
             = StreamEncoder.forOutputStreamWriter(outthis, (String)null);
        } catch (UnsupportedEncodingException e) {
            throw new Error(e);
        }
    }

    
Creates an OutputStreamWriter that uses the given charset.

Parameters:
out An OutputStream
cs A charset
Since:
1.4
Spec:
JSR-51
    public OutputStreamWriter(OutputStream outCharset cs) {
        super(out);
        if (cs == null)
            throw new NullPointerException("charset");
         = StreamEncoder.forOutputStreamWriter(outthiscs);
    }

    
Creates an OutputStreamWriter that uses the given charset encoder.

Parameters:
out An OutputStream
enc A charset encoder
Since:
1.4
Spec:
JSR-51
    public OutputStreamWriter(OutputStream outCharsetEncoder enc) {
        super(out);
        if (enc == null)
            throw new NullPointerException("charset encoder");
         = StreamEncoder.forOutputStreamWriter(outthisenc);
    }

    
Returns the name of the character encoding being used by this stream.

If the encoding has an historical name then that name is returned; otherwise the encoding's canonical name is returned.

If this instance was created with the OutputStreamWriter(java.io.OutputStream,java.lang.String) constructor then the returned name, being unique for the encoding, may differ from the name passed to the constructor. This method may return null if the stream has been closed.

Returns:
The historical name of this encoding, or possibly null if the stream has been closed
See also:
java.nio.charset.Charset
Revised:
1.4
Spec:
JSR-51
    public String getEncoding() {
        return .getEncoding();
    }

    
Flushes the output buffer to the underlying byte stream, without flushing the byte stream itself. This method is non-private only so that it may be invoked by PrintStream.
    void flushBuffer() throws IOException {
        .flushBuffer();
    }

    
Writes a single character.

Throws:
IOException If an I/O error occurs
    public void write(int cthrows IOException {
        .write(c);
    }

    
Writes a portion of an array of characters.

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

    
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:
IOException If an I/O error occurs
    public void write(String strint offint lenthrows IOException {
        .write(strofflen);
    }

    
Flushes the stream.

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