当前位置: 技术问答>java相关
文件阅读的中文问题
来源: 互联网 发布时间:2015-11-14
本文导语: 我想按行读取一个txt文件,文件中的中文读出来都是乱码,请问怎么解决,方法可行马上给分 public Vector read(String fileName) { Vector baseData = new Vector(); File file = new File(fileName); if (file != nul...
我想按行读取一个txt文件,文件中的中文读出来都是乱码,请问怎么解决,方法可行马上给分
public Vector read(String fileName)
{
Vector baseData = new Vector();
File file = new File(fileName);
if (file != null)
{
if (file.exists())
{
try
{
InputStream a = new FileInputStream(file);
DataInputStream b = new DataInputStream(a);
String c="";
while (c !=null)
{
c = b.readLine();
System.out.println(c);
}
}
catch(Exception e)
{
}
}
else
{
System.out.println("not exits");
}
}
return baseData;
}
public Vector read(String fileName)
{
Vector baseData = new Vector();
File file = new File(fileName);
if (file != null)
{
if (file.exists())
{
try
{
InputStream a = new FileInputStream(file);
DataInputStream b = new DataInputStream(a);
String c="";
while (c !=null)
{
c = b.readLine();
System.out.println(c);
}
}
catch(Exception e)
{
}
}
else
{
System.out.println("not exits");
}
}
return baseData;
}
|
不要用readline。你把数据按照数组读出来,然后用string对其进行重新编码,这样显示就没有问题了(前提是你的系统支持中文)
|
txt文件用FileReader类来做,
InputStream a = new FileInputStream(file);
DataInputStream b = new DataInputStream(a);
String c="";
改成:
FileReader a=new FileReader(file);
BufferedReader b=new BufferedReader(a);
String c="";
后面一样
InputStream a = new FileInputStream(file);
DataInputStream b = new DataInputStream(a);
String c="";
改成:
FileReader a=new FileReader(file);
BufferedReader b=new BufferedReader(a);
String c="";
后面一样