我正在用Google app Engine编写一个web应用程序。它允许人们基本上编辑html代码,这些代码以.html
文件的形式存储在blobstore中。
我使用fetchData返回文件中所有字符的byte[]
。我正在尝试打印到html,以便用户编辑html代码。一切都运行得很好!
这是我现在唯一的问题:
字节数组在转换回字符串时有一些问题。聪明的引语和几个字符看起来很时髦。(?或日文符号等)具体地说,我看到有几个字节的值为负值,这是导致问题的原因。
在字节数组中,智能引号返回为-108
和-109
。为什么会这样?我如何解码负字节以显示正确的字符编码?
发布于 2013-01-08 19:59:37
你可以试试这个。
String s = new String(bytearray);
发布于 2011-04-15 18:32:29
public class Main {
/**
* Example method for converting a byte to a String.
*/
public void convertByteToString() {
byte b = 65;
//Using the static toString method of the Byte class
System.out.println(Byte.toString(b));
//Using simple concatenation with an empty String
System.out.println(b + "");
//Creating a byte array and passing it to the String constructor
System.out.println(new String(new byte[] {b}));
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Main().convertByteToString();
}
}
输出
65
65
A
发布于 2013-07-09 12:15:29
public static String readFile(String fn) throws IOException
{
File f = new File(fn);
byte[] buffer = new byte[(int)f.length()];
FileInputStream is = new FileInputStream(fn);
is.read(buffer);
is.close();
return new String(buffer, "UTF-8"); // use desired encoding
}
https://stackoverflow.com/questions/5673059
复制相似问题