J2ME中读取Unicode和UTF-8编码文件的方法
2010-01-08 11:49
519 查看
一、读取Unicode
文件
/**
*
读取Unicode
编码文本文件
* @param resource String -
文件名
* @return String - Unicode
文本
*/
public static String read_Uni(String resource) {
byte word_uni[] = new byte[1024];
String strReturn = null;
InputStream is;
try {
is = instance.getClass().getResourceAsStream(resource);
is.skip(2); //
跳过两个字节的文件头
is.read(word_uni);
is.close();
StringBuffer stringbuffer = new StringBuffer("");
for (int j = 0; j < word_uni.length; ) {
int l = word_uni[j++];
int h = word_uni[j++];
char c = (char) ((l & 0xff) | ((h << 8) & 0xff00));
stringbuffer.append(c);
}
strReturn = stringbuffer.toString();
} catch (IOException ex) {
System.out.println(ex);
} finally {
is = null;
}
return strReturn;
}
二、读取UTF-8
文件
/**
*
读取UTF-8
编码文本文件
* @param resource String -
文件名
* @return String - UTF-8
文本
*/
public static String read_UTF(String resource) {
byte word_utf[] = new byte[1024];
String strReturn = null;
InputStream is;
try {
is = instance.getClass().getResourceAsStream(resource);
is.read(word_utf);
is.close();
strReturn = new String(word_utf, "UTF-8");
} catch (IOException ex) {
System.out.println(ex);
}
return strReturn;
}
备注:
InputStream in=this.getClass().getResourceAsStream("/help.txt")
;//将位于res目录下的help.txt中数字读出。
……
in.mark(0);
……
in.reset();
文件
/**
*
读取Unicode
编码文本文件
* @param resource String -
文件名
* @return String - Unicode
文本
*/
public static String read_Uni(String resource) {
byte word_uni[] = new byte[1024];
String strReturn = null;
InputStream is;
try {
is = instance.getClass().getResourceAsStream(resource);
is.skip(2); //
跳过两个字节的文件头
is.read(word_uni);
is.close();
StringBuffer stringbuffer = new StringBuffer("");
for (int j = 0; j < word_uni.length; ) {
int l = word_uni[j++];
int h = word_uni[j++];
char c = (char) ((l & 0xff) | ((h << 8) & 0xff00));
stringbuffer.append(c);
}
strReturn = stringbuffer.toString();
} catch (IOException ex) {
System.out.println(ex);
} finally {
is = null;
}
return strReturn;
}
二、读取UTF-8
文件
/**
*
读取UTF-8
编码文本文件
* @param resource String -
文件名
* @return String - UTF-8
文本
*/
public static String read_UTF(String resource) {
byte word_utf[] = new byte[1024];
String strReturn = null;
InputStream is;
try {
is = instance.getClass().getResourceAsStream(resource);
is.read(word_utf);
is.close();
strReturn = new String(word_utf, "UTF-8");
} catch (IOException ex) {
System.out.println(ex);
}
return strReturn;
}
备注:
InputStream in=this.getClass().getResourceAsStream("/help.txt")
;//将位于res目录下的help.txt中数字读出。
……
in.mark(0);
……
in.reset();
相关文章推荐
- J2ME中读取Unicode和UTF-8编码文件
- J2ME读取UTF-8编码文件方法
- J2ME读取UTF-8编码文件方法
- J2ME中读取Unicode和UTF-8编码文件
- J2ME中读取Unicode和UTF-8编码文件
- J2ME中读取Unicode和UTF-8编码文件
- J2ME中读取Unicode和UTF-8编码文件
- J2ME中读取Unicode和UTF-8编码文件
- J2ME中读取Unicode和UTF-8编码文件
- Python读取Unicode编码格式的文件(转换为UTF-8)
- Java读取Unicode文件(UTF-8等)时碰到的BOM首字符问题,及处理方法
- Java读取Unicode文件(UTF-8等)时碰到的BOM首字符问题,及处理方法
- 中文编码问题:raw_input输入、文件读取、变量比较等str、unicode、utf-8转换问题
- Java读取Unicode文件(UTF-8等)时碰到的BOM首字符问题,及处理方法
- Java读取Unicode文件(UTF-8等)时碰到的BOM首字符问题,及处理方法
- [Python爬虫] 中文编码问题:raw_input输入、文件读取、变量比较等str、unicode、utf-8转换问题
- Java读取Unicode文件(UTF-8等)时碰到的BOM首字符问题,及处理方法
- 在J2ME实现UTF-8转换为Unicode编码的方法
- [Python] 中文编码问题:raw_input输入、文件读取、变量比较等str、unicode、utf-8转换问题
- Java读取UTF-8格式txt文件第一行出现乱码——问号“?”及解决;Java读带有BOM的UTF-8文件乱码原因及解决方法