JAVA IO读写文本及乱码处理

判断字符编码所需jar包:

    1.antlrall-2.7.4.jar

    2.cpdetector_1.0.10.jar

    3.jargs-1.0.jar

    4.jchardet-1.0.jar

 

 

package util;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;

import info.monitorenter.cpdetector.io.CodepageDetectorProxy;
import info.monitorenter.cpdetector.io.JChardetFacade;

public class FileUtils {

	/**
	 * 获取文件文本编码
	 * 
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	public static String codeString(String fileName) throws Exception {
		CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();
		detector.add(JChardetFacade.getInstance());

		Charset charset = null;

		File f = new File(fileName);

		try {
			charset = detector.detectCodepage(f.toURI().toURL());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return charset.toString();
	}

	/**
	 * 根据编码读取文本
	 * @param url
	 * @return
	 */
	public static String readFileText(String url) {
		String code = "UTF-8";
		File file = new File(url);
		StringBuilder text = new StringBuilder();
		if (file.isFile() && file.exists()) {
			try {
				code = codeString(url);
                //window电脑右键新建的文本文件读取出来默认是"windows-1252",这个更换成gb2312,否则也是乱码
				if(code.equals("windows-1252")) {
					code = "gb2312";
				}
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			FileInputStream fis = null;
			try {
				fis = new FileInputStream(file);
				BufferedInputStream bis = new BufferedInputStream(fis);
//				byte[] buffer = new byte[(int) file.length()];文件长度有可能越界,不使用这种方式
				byte[] buffer = new byte[1024];
				int cnt = 0;
                while ((cnt = bis.read(buffer)) != -1) {
                    //不建议用String +=,效率差的不是一点半点
			        text.append(new String(buffer, 0, cnt, code));
				}
				bis.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return text.toString();
		}else {
			return null;
		}
	}
	
	/**
	 * 写入本地
	 * @param fileName
	 * @param htmlData
	 */
	public static void writeToLocal(String fileName, String htmlData) {
		File file = new File("C:\\Users\\Administrator\\Desktop\\测试");
		if (!file.exists()) {
			file.mkdirs();
		}
		file = new File("C:\\Users\\Administrator\\Desktop\\测试\\" + fileName + ".txt");
		OutputStreamWriter writer = null;
		BufferedWriter bw = null;
		try {
			OutputStream os = new FileOutputStream(file);
			writer = new OutputStreamWriter(os, "UTF-8");
			bw = new BufferedWriter(writer);
			bw.write(htmlData);
			bw.flush();
			if (file.exists()) {
				file.delete();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				bw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值