java读取txt文件时候出现乱码
publicclassHomework{publicstaticvoidmain(String[]args)throwsIOException{try{System.ou...
public class Homework {
public static void main(String[] args)throws IOException{
try{
System.out.println("Please input your command:");
Scanner sc=new Scanner(System.in);
String command=sc.next();
String temp;
String[] text1=new String[5];
String[] text2=new String[5];
String information;
File fl=new File("D:\\StoreHouse.txt");
FileReader fr=new FileReader(fl);
BufferedReader br=new BufferedReader(fr);
StringBuffer SB=new StringBuffer(4096);
if(command.equals("show")){
while((temp=br.readLine())!=null){
SB.append(temp).append('\n');
System.out.println(SB);
}
br.close();
这是代码,在执行show命令时,D:\\StoreHouse.txt明明有中文,为什么显示的是一堆乱码,为什么,乱码如下:
Please input your command:
show
0001;�չ����;�������չ��;SR01;100
0001;�չ����;�������չ��;SR01;100
0002;�չ����;��ţ�չ��;QE01;10 展开
public static void main(String[] args)throws IOException{
try{
System.out.println("Please input your command:");
Scanner sc=new Scanner(System.in);
String command=sc.next();
String temp;
String[] text1=new String[5];
String[] text2=new String[5];
String information;
File fl=new File("D:\\StoreHouse.txt");
FileReader fr=new FileReader(fl);
BufferedReader br=new BufferedReader(fr);
StringBuffer SB=new StringBuffer(4096);
if(command.equals("show")){
while((temp=br.readLine())!=null){
SB.append(temp).append('\n');
System.out.println(SB);
}
br.close();
这是代码,在执行show命令时,D:\\StoreHouse.txt明明有中文,为什么显示的是一堆乱码,为什么,乱码如下:
Please input your command:
show
0001;�չ����;�������չ��;SR01;100
0001;�չ����;�������չ��;SR01;100
0002;�չ����;��ţ�չ��;QE01;10 展开
4个回答
展开全部
WINDOW默认是GBK 看你java文件编码是什么 编码格式不统一
public static void main(String[] args)throws IOException{
FileInputStream fis = new FileInputStream( "D:\\StoreHouse.txt");
InputStreamReader isr = new InputStreamReader(fis, "GBK");
BufferedReader br=new BufferedReader(isr);
StringBuffer SB=new StringBuffer(4096);
String temp = null;
while((temp=br.readLine())!=null){
SB.append(temp).append('\n');
System.out.println(SB);
}
br.close();
}
public static void main(String[] args)throws IOException{
FileInputStream fis = new FileInputStream( "D:\\StoreHouse.txt");
InputStreamReader isr = new InputStreamReader(fis, "GBK");
BufferedReader br=new BufferedReader(isr);
StringBuffer SB=new StringBuffer(4096);
String temp = null;
while((temp=br.readLine())!=null){
SB.append(temp).append('\n');
System.out.println(SB);
}
br.close();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
SB= new String(SB.getBytes("ISO-8859-1"),"UTF-8");//字符编码
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
字符编码的问题,你把字符编码都设置成utf-8
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询