你的charset名字不对吧你看一下charset类试试 US-ASCII
解决方案 »
- 字符串问题
- SWT Shell
- 2个问题
- eclipse+myeclipse导入swt原生库的问题
- 大吓指教java
- 不太难的问题,我有思路,但没做出来.50分啊~
- java抽取Word中公式并转化成png格式图片
- 我写二个模块.一个是用java写的套接字client/server端,和另一个用VC写的套接字client/server端通讯,请问能否可以实现通讯?套接字用berkel
- 用JBuilder生成exe文件,运行出现问题
- eclipse和DOM-4j解析xml文件时候出现java.lang.Exception: No runnable methods
- python效率和java相差这么多吗?求大神解释
- 如何设置 java -classpath 参数为目录下所有jar
char aChar;
int len = theString.length();
StringBuffer outBuffer = new StringBuffer(len);
for (int x = 0; x < len;) {
aChar = theString.charAt(x++);
if (aChar == '\\') {
aChar = theString.charAt(x++);
if (aChar == 'u') {
// Read the xxxx
int value = 0;
for (int i = 0; i < 4; i++) {
aChar = theString.charAt(x++);
switch (aChar) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
value = (value << 4) + aChar - '0';
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
value = (value << 4) + 10 + aChar - 'a';
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
value = (value << 4) + 10 + aChar - 'A';
break;
default:
throw new IllegalArgumentException(
"Malformed \\uxxxx encoding.");
}
}
outBuffer.append((char) value);
} else {
if (aChar == 't')
aChar = '\t';
else if (aChar == 'r')
aChar = '\r';
else if (aChar == 'n')
aChar = '\n';
else if (aChar == 'f')
aChar = '\f';
outBuffer.append(aChar);
}
} else
outBuffer.append(aChar);
}
return outBuffer.toString();
}
我的文件内容如下,保存的时候选择的Unicode编码。
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
中文
import java.io.*;
class Main16
{
public static void main(String[] args)throws IOException{
FileInputStream fis = new FileInputStream("read.txt");
System.setIn(fis);
byte[] buf = new byte[12];
int length = 0;
while((length=System.in.read(buf))>0){
System.out.print(new String(buf,0,length));
}
}
}
跟上面说的问题一样,文件内容如下
“用FileInputStream读取文件
第二行”,
buf足够大的时候没有问题,buf = new byte[10]的时候:buf = new byte[12]的时候,运行如下: