一直以来学习中从来没有操作过中文文件,今天有个事必须要从一个文件中读取中文字符然后输出,才发现束手无策,求助下各位大侠。先贴下我的范例代码public static void main(String args[])
{
byte buffer[] = new byte[2056];
try
{
FileInputStream fileint = new FileInputStream("dchs.txt");
int bytes = fileint.read(buffer,0,2056);
String str = new String(buffer,0,0,bytes);
System.out.print(str);
}//try
catch(Exception e)
{
String err = e.toString();
System.out.println(err);
}//cathch
}//public static void main(String args[])我用 FileInputStream 这么操作 输出的中文总是乱码,英文字符正常。各位大侠给指教下。
{
byte buffer[] = new byte[2056];
try
{
FileInputStream fileint = new FileInputStream("dchs.txt");
int bytes = fileint.read(buffer,0,2056);
String str = new String(buffer,0,0,bytes);
System.out.print(str);
}//try
catch(Exception e)
{
String err = e.toString();
System.out.println(err);
}//cathch
}//public static void main(String args[])我用 FileInputStream 这么操作 输出的中文总是乱码,英文字符正常。各位大侠给指教下。
解决方案 »
- java的类加载器有哪些,它们的顺序是什么?
- java如何判断键盘输入双键并读取?
- 一到java的编程思维,希望各位可以解决一下
- jpeg转成bmp图像的java代码怎么写
- 一个关于Runtime.getRuntime().exec()的问题(under linux)
- 请问怎么实现JAVA调用视频头的录象功能?
- 学习JSP需要哪方面知识?(分不多了,明天给分!回答有分)
- 我想在任意的目录下都能使用javac abc,java abc这样的省略路径形式,怎么设置环境变量?
- 救命!一定重谢!!!
- 请问java是跨平台的,它用什么软件编译?
- java程序运行后会产生什么
- java中如何将字符型变量String转化成int型变量?
{
byte buffer[] = new byte[2056];
try
{
FileInputStream fileint = new FileInputStream("易中天拼三国.txt");
int i=0;
while(fileint.read(buffer)!=-1){
String str=new String(buffer);
System.out.println(str);
}
}//try
catch(Exception e)
{
e.printStackTrace();
String err = e.toString();
System.out.println(err);
}//cathch
}
}
File file=new File("C:\\dchs.txt");
char[] ch=new char[(int)file.length()];
int len=0;
FileReader fr;
try {
fr = new FileReader(file);
len=fr.read(ch);
System.out.println(new String(ch,0,len));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}