Java 读取ANSI文件中文乱码问题解决方式[转]
第一步:首先判断源文件的编码格式:
File file = new File(path);
InputStream in= new java.io.FileInputStream(file);
byte[] b = new byte[3];
in.read(b);
in.close();
if (b[0] == -17 && b[1] == -69 && b[2] == -65)
System.out.println(file.getName() + ":编码为UTF-8");
else
System.out.println(file.getName() + ":可能是GBK,也可能是其他编码");
第二步:用指定的编码格式读取文件流
- private static String forTest(String file) throws IOException {
- File f = new File(file);
- InputStreamReader read = new InputStreamReader(new FileInputStream(f),
- "编码格式");
- BufferedReader reader = new BufferedReader(read);
- String line;
- String s = "";
- while ((line = reader.readLine()) != null) {
- s = s+line;
- }
- return s;
- }
总结:就是采用如下的方式:
File file = new File("D:\\Myeclipse\\Ini\\config.properties");
FileInputStream in = new FileInputStream(file);
byte[] b = new byte[3];
in.read(b);
String code = "GBK";
if (b[0] == -17 && b[1] == -69 && b[2] == -65){
code = "UTF-8";
} InputStreamReader inputStreamReader = new InputStreamReader(in,code);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = bufferedReader.readLine();
String data = "";
while(str != null){
data = data + str+"\r\n";
str = bufferedReader.readLine();
}
in.close();
bufferedReader.close();
System.out.println(code);
System.out.println(data);
Java 读取ANSI文件中文乱码问题解决方式[转]的更多相关文章
- .net读取xml文件中文乱码问题解决
读取XML的编码方式Encoding.UTF8 要和<?xml version="1.0" encoding="utf-8"?>的encoding ...
- 【其他】【navicat】【1】navicat导入txt文件中文乱码问题解决
正文: TXT文件默认编码为ANSI,另存为编码为UTF-8的文本文件即可 备注: 1,一般需要导入的数据都是一张excel表,需要将excel表另存为“文本文件(制表符分隔)(*.txt)”保存类型 ...
- 解決BufferedReader读取UTF-8文件中文乱码
解決BufferedReader读取UTF-8文件中文乱码 File rst01 = new File(context.getRealPath("/")+" ...
- java读取XML文件的四种方式
java读取XML文件的四种方式 Xml代码 <?xml version="1.0" encoding="GB2312"?> <RESULT& ...
- Python读取 csv文件中文乱码处理
需求:按行解析读取csv文件存入关系型数据库——主要是中文字体解析:遇到的问题:直接解析出来的数据为list形式,而且编码格式为unicode;解决问题:前提了解: 中文编码的规则 —— GB2312 ...
- Java 读取 .properties 文件的几种方式
Java 读取 .properties 配置文件的几种方式 Java 开发中,需要将一些易变的配置参数放置再 XML 配置文件或者 properties 配置文件中.然而 XML 配置文件需要通过 ...
- Java使用ResourceBundle类读取properties文件中文乱码的解决方案
Java使用java.util.ResourceBundle类的方式来读取properties文件时不支持中文,要想支持中文必须将文件设置为ISO-8859-1编码格式,这对于开发工具默认为UTF-8 ...
- java压缩zip文件中文乱码问题(转——作者:riching)
本人遇到了同样的问题,用了以下方案,奇迹般的解决了.我很纳闷为什么,经理说:好读书,不求甚解,不要问为什么... 用java来打包文件生成压缩文件,有两个地方会出现乱码 1.内容的中文乱码问题,这个问 ...
- java压缩zip文件中文乱码问题
用java来打包文件生成压缩文件,有两个地方会出现乱码 1.内容的中文乱码问题,这个问题网上很多人给出了解决方法,两种:修改sun的源码:使用开源的类库org.apache.tools.zip.Zip ...
随机推荐
- 用十条命令在一分钟内检查 Linux 服务器性能
原文地址: http://www.oschina.net/news/69132/linux-performance 如果你的Linux服务器突然负载暴增,告警短信快发爆你的手机,如何在最短时间内找出L ...
- Selenium2+python自动化73-定位的坑:class属性有空格
前言 有些class属性中间有空格,如果直接复制过来定位是会报错的InvalidSelectorException: Message: The given selector u-label f-dn ...
- python 进程池pool简单使用
平常会经常用到多进程,可以用进程池pool来进行自动控制进程,下面介绍一下pool的简单使用. 需要主动是,在Windows上要想使用进程模块,就必须把有关进程的代码写if __name__ == ‘ ...
- Visio画流程图风格设置
第一步:选取设计下选用“简单” 第二步:设置颜色为“铅笔” 第三步:设置效果为“辐射” 第四步:效果
- WPF应用程序exe接收参数
using System;using System.ServiceProcess; namespace GoShopService{ public partial class Service1 ...
- 为什么用svg放弃了iconfont?
svg替代iconfont的好处(无论是基于Vue.Jquery),都推荐svg http://www.woshipm.com/pd/463866.html svg图标库,svg图标在线制作 http ...
- efcore数据库自动生成
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. p ...
- 018-Go将磁盘目录实现简单的静态Web服务
package main import( "net/http" ) func main(){ http.Handle("/", http.FileServer( ...
- C# System.Collections.Queue
using System; using System.Collections; public class SamplesQueue { public static void Main() { // C ...
- 快捷切换hosts的小工具:SwitchHosts!
Windows 绿色版本下载:http://oldj.github.io/SwitchHosts/. 日常开发工作中,我们可能经常需要切换各种 hosts 绑定,比如在本地开发时可能需要一个开发环境的 ...