¿Por qué java.nio.charset.Charsets compila el error? [cerrado]

import java.nio.charset.Charsets in my class UriCodec.java,but when i use javac(jdk6) compile this class error. for example: javac UriCodec.java

código de error:

import java.nio.charset.Charsets;
                       ^
UriCodec.java:140: Can not find symbol
symbol: Variable Charsets
position: class com.android.exchange.utility.UriCodec
        appendEncoded(builder, s, Charsets.UTF_8, false);
                                  ^
UriCodec.java:144: Can not find symbol
symbol: Variable Charsets
position: class com.android.exchange.utility.UriCodec
        appendEncoded(builder, s, Charsets.UTF_8, true);
                                  ^
UriCodec.java:203: Can not find symbol
symbol: Variable Charsets
position: class com.android.exchange.utility.UriCodec
        return decode(s, false, Charsets.UTF_8);
                                ^
UriCodec.java:214: Can not find symbol
symbol: Method toHexString(byte,boolean)
position: class java.lang.Byte
        sb.append(Byte.toHexString(b, true));
                      ^
5 error

mi clase:

import java.nio.charset.Charset;
import java.nio.charset.Charsets;
public abstract class UriCodec {


    public final void appendEncoded(StringBuilder builder, String s) {
        appendEncoded(builder, s, Charsets.UTF_8, false);
    }

    public final void appendPartiallyEncoded(StringBuilder builder, String s) {
        appendEncoded(builder, s, Charsets.UTF_8, true);
    }

    public static String decode(String s) {
        return decode(s, false, Charsets.UTF_8);
    }


   public final String encode(String s, Charset charset) {
        // Guess a bit larger for encoded form
        StringBuilder builder = new StringBuilder(s.length() + 16);
        appendEncoded(builder, s, charset, false);
        return builder.toString();
    }
}

preguntado el 30 de junio de 12 a las 21:06

Could you give us more information? Some example code where you use this library? -

¿Cuál es el mensaje de error que está recibiendo? -

In future, please don't say that you get an error without telling us cual es ese error. It's like going to the doctor and saying, "I'm not feeling very well, but I'm not going to tell you in what way, or allow you to examine me." -

replace with java.nio.charset.StandardCharsets; -

1 Respuestas

There's no such class as Charsets. Hay Charset (singular) but that's not the same thing...

I suspect you got a message like this:

UriCodec.java:1: error: cannot find symbol
import java.nio.charset.Charsets;
                       ^
  symbol:   class Charsets
  location: package java.nio.charset
1 error

When you get a compiler error, leerlo. If the compiler says it can't find a symbol, then there's usually a very good reason for it - so double-check that you've spelled everything correctly, with the correct casing.

Cambie su importación a

import java.nio.charset.Charset;

y todo debería estar bien.

... or if you meant a different Charsets class (such as the one in Guava) you should add an import for que clase en su lugar.

Respondido 15 Feb 18, 16:02

i dont think the answer is this simple. java.nio.charset.Charset does not contain members such as "UTF_8" as the example code shows. in fact you can see the use here in some android code: grepcode.com/file/repository.grepcode.com/java/ext/… - Diana

@Sam: I've edited the answer, just adding a bit at the bottom. - jon skeet

fair enough. I'm personally trying to work out where i can find and import these classes from now. - Diana

also, the NDEfRecord class seems to be a little inconsistent in that you specify at "type" by passing a string, but can get it back as a byte[] - internally converted using Charsets. So there's no real consistent/compatible way of using it nicefully (that's a new word btw) - Diana

Using more recent documentation, should be import java.nio.charset.StandardCharsets; docs.oracle.com/javase/7/docs/api/java/nio/charset/… - Nagev

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.