读取txt文件,中文乱码

使用BufferedReader进行读取和规定编码,中文乱码一般都是txt文本存的编码和项目读的编码不同造成的,所以要查看txt编码然后进行规定。如果不知道编码或者编码不固定,可以动态获取,但没试过,网上有人说可以有人说准确率低,具体百度 如何获取txt文件编码。

public static String readtxtContent(File file){
	        StringBuilder result = new StringBuilder();
	        try{
	            BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(file),"gb2312"));  
	            String s = null;
	            while((s = br.readLine())!=null){//使用readLine方法,一次读一行
	                result.append(System.lineSeparator()+s);
	            }
	            br.close();    
	        }catch(Exception e){
	            e.printStackTrace();
	        }
	        return result.toString();
	    }
发布了64 篇原创文章 · 获赞 103 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/P_Doraemon/article/details/83183811