java 写入文件 指定字符集 设置字符集格式代码有什么用?

[更新]
·
·
分类:互联网
4076 阅读

java 写入文件 指定字符集

设置字符集格式代码有什么用?

设置字符集格式代码有什么用?

没有编程语言支持的汉字编码格式(或西文)不同,需要在编程时进行转换。
比如java默认是UTF8 C语言是ASCII,C#是unicode。
另外DOS界面支持的是ASCII

jsp咋启命令提示符?

首先创建一个读取文件的类,方法readFile读取文件,参数1:文件路径,参数2:字符集(gb2312,utf-8等)
源码:
package core.util
import
import
import
public class FileUtil {
public static String readFile(String filePath, String charset)
{
String s #34#34
try
{
File f new File(filePath)
if (f.exists())
{
FileInputStream bw new FileInputStream(f)
int len bw.available()
byte[] str new byte[len]
if ((str) -1)
{
s #34#34
}
else
{
s new String(str, charset)
}
()
}
}
catch (IOException e)
{
()
}
return s
}
}
在thispage.jsp中调用:
lt%
String show (#34show#34)
if (#34yes#34.equals(show))
{
String str (#34c:123.txt#34,#34gn2312#34)
(str)
}
%gt
ltscript language#34javascript#34gt
function doShow()
{
#34thispage.jsp?showyes#34
}
lt/scriptgt
ltinput typ#34button#34 namebutton value#34显示#34 onclick#34doShow()#34gt