参考:https://blog.csdn.net/yangzhichao888/article/details/79529756

https://blog.csdn.net/wangzhi291/article/details/41485403

1.新建一个类,叫“UnicodeReader”,复制粘贴如下内容;


/**
version: 1.1 / 2007-01-25
- changed BOM recognition ordering (longer boms first) Original pseudocode : Thomas Weidenfeller
Implementation tweaked: Aki Nieminen http://www.unicode.org/unicode/faq/utf_bom.html
BOMs:
00 00 FE FF = UTF-32, big-endian
FF FE 00 00 = UTF-32, little-endian
EF BB BF = UTF-8,
FE FF = UTF-16, big-endian
FF FE = UTF-16, little-endian Win2k Notepad:
Unicode format = UTF-16LE
***/ import java.io.*; /**
* Generic unicode textreader, which will use BOM mark
* to identify the encoding to be used. If BOM is not found
* then use a given default or system encoding.
*/
public class UnicodeReader extends Reader {
PushbackInputStream internalIn;
InputStreamReader internalIn2 = null;
String defaultEnc; private static final int BOM_SIZE = 4; /**
*
* @param in inputstream to be read
* @param defaultEnc default encoding if stream does not have
* BOM marker. Give NULL to use system-level default.
*/
UnicodeReader(InputStream in, String defaultEnc) {
internalIn = new PushbackInputStream(in, BOM_SIZE);
this.defaultEnc = defaultEnc;
} public String getDefaultEncoding() {
return defaultEnc;
} /**
* Get stream encoding or NULL if stream is uninitialized.
* Call init() or read() method to initialize it.
*/
public String getEncoding() {
if (internalIn2 == null) return null;
return internalIn2.getEncoding();
} /**
* Read-ahead four bytes and check for BOM marks. Extra bytes are
* unread back to the stream, only BOM bytes are skipped.
*/
protected void init() throws IOException {
if (internalIn2 != null) return; String encoding;
byte bom[] = new byte[BOM_SIZE];
int n, unread;
n = internalIn.read(bom, 0, bom.length); if ( (bom[0] == (byte)0x00) && (bom[1] == (byte)0x00) &&
(bom[2] == (byte)0xFE) && (bom[3] == (byte)0xFF) ) {
encoding = "UTF-32BE";
unread = n - 4;
} else if ( (bom[0] == (byte)0xFF) && (bom[1] == (byte)0xFE) &&
(bom[2] == (byte)0x00) && (bom[3] == (byte)0x00) ) {
encoding = "UTF-32LE";
unread = n - 4;
} else if ( (bom[0] == (byte)0xEF) && (bom[1] == (byte)0xBB) &&
(bom[2] == (byte)0xBF) ) {
encoding = "UTF-8";
unread = n - 3;
} else if ( (bom[0] == (byte)0xFE) && (bom[1] == (byte)0xFF) ) {
encoding = "UTF-16BE";
unread = n - 2;
} else if ( (bom[0] == (byte)0xFF) && (bom[1] == (byte)0xFE) ) {
encoding = "UTF-16LE";
unread = n - 2;
} else {
// Unicode BOM mark not found, unread all bytes
encoding = defaultEnc;
unread = n;
}
//System.out.println("read=" + n + ", unread=" + unread); if (unread > 0) internalIn.unread(bom, (n - unread), unread); // Use given encoding
if (encoding == null) {
internalIn2 = new InputStreamReader(internalIn);
} else {
internalIn2 = new InputStreamReader(internalIn, encoding);
}
} public void close() throws IOException {
init();
internalIn2.close();
} public int read(char[] cbuf, int off, int len) throws IOException {
init();
return internalIn2.read(cbuf, off, len);
} }

写入文件时:

    	 PrintWriter out=null;
try { File filename = new File(savefile);
filename.createNewFile();
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename),"UTF-8")));
System.out.println("打开fwriter");
String []ss = content.split(",");
out.write("["+"\r\n");
out.write(s+","+"\r\n"); } catch (IOException ex)
{
ex.printStackTrace();
}
finally {
out.flush();
out.close();
System.out.println("关闭fwriter");
} }

java读utf8 的txt文件,第一个字符为空或问号问题的更多相关文章

  1. java读取UTF-8的txt文件发现开头的一个字符问题

    今天遇到一个奇葩问题,在读取一个TXT文件时,出现开头多了一个问号(?).如下图: 莫名奇妙的多了一个.最后通过网上资料,知道在Java中,class文件采用utf8的编码方式,JVM运行时采用utf ...

  2. Java代码输出到txt文件(申请专利贴源码的必备利器)

    最近公司在申请专利,编写不少文档,项目的代码量实在是过于庞大.如果一个一个的复制粘贴虽然能够完成,但是对于程序员而言实在没有这个必要.shell或者python就能解决这个问题.由于我个人对于shel ...

  3. matlab 中txt文件(含字符及数值)处理

    matlab 中txt文件(含字符及数值)处理 (2008-08-02 09:45:12) 转载▼ 标签: 杂谈 分类: matlab及C学习 Matlab文件操作及读txt文件ZZ 2008-07- ...

  4. Java笔记13:统计文件中每个字符出现的次数

    一.代码实现 import java.io.*; import java.util.*; /** 功能:统计文件中每个字符出现的次数 思路: 1.定义字符读取(缓冲)流 2.循环读取文件里的字符,用一 ...

  5. java读取数据写入txt文件并将读取txt文件写入另外一个表

    package com.xsw.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.F ...

  6. java读取记事本文件第一个字符遇到的一个坑

    记事本数据是这样的: Faq_faqTitle=常见问题_标题Faq_faqKeyword=关键字Faq_faqDescription=FAQ描述...... 文件编码:utf-8有签名 然后用jav ...

  7. Java——读取和写入txt文件

    package com.java.test.a; import java.io.BufferedReader; import java.io.BufferedWriter; import java.i ...

  8. Java中读取txt文件中中文字符时,出现乱码的解决办法

    这是我写的一个Java课程作业时,遇到的问题. 问题描述: 我要实现的就是将txt文件中的内容按一定格式读取出来后,存放在相应的数组. 我刚开始运行时发现,英文可以实现,但是中文字符就是各种乱码. 最 ...

  9. UTF-8格式txt文件读取字节前三位问题

    今天试着读取一份UTF-8格式的txt文件,内容如下 12345 但是每次读取之后转为String类型,输出字符串长度总是为6,并且第一位打印在控制台后不占任何空间. 经过debug查看字节码后发现, ...

随机推荐

  1. 尺取法 POJ 3320 Jessica's Reading Problem

    题目传送门 /* 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 */ #include <cstdio> #include <cmath> ...

  2. sed练习第一节

    ed语法和基本命令 employee.txt文件内容如下: 101,John Doe,CEO 102,Jason Smith,IT Manager 103,Raj Reddy,Sysadmin 104 ...

  3. 设置电脑IP

    1.首先在Win7桌面上找到“网络”入口,如下图:   进入Win7网络 2.进入网络之后我们再点击顶部的“网络共享中心”,如下图:   进入Win7网络共享中心 3.进入Win7网络共享中心之后,我 ...

  4. MyEclipse开启Jquery智能提示

    myeclipse 增加javascript提示和jquery提示等不用安装插件自带功能 (对着需要提示的项目右键,点击properties) 不行的话就得安装插件: http://www.spket ...

  5. javaScript关闭浏览器 (不弹出提示框)

    一段JavaScript脚本程序,负责关闭窗口,如果网页不是通过脚本程序打开的(window.open()),调用window.close()脚本关闭窗口前,必须先将window.opener对象置为 ...

  6. 关于使用myeclipse搭建tomcat环境运行web项目的方法

    这两天准备改同事的一个系统的自适应,然而我没想到的是我竟然在打开这个项目上就遇到了困难,真的是too young too simple,究其根本就是了解的太少了,于是为了我不忘记,用博客的方式把它记录 ...

  7. ReactJS-2-props vs state

    rops理解: 大多数组件都可以在创建的时候被不同的参数定制化,这些不同的参数就叫做props.props的流向是父组件到子组件. 子组件Comment,是一条评论组件,父组件CommentList, ...

  8. JS filters-ul li简单过滤

    功能要求:在input中输入字母,显示ul li中匹配的元素,隐藏不匹配的 <!DOCTYPE html> <html> <head> <meta chars ...

  9. spark性能优化(包括优化原理及基本方法)

    https://www.jianshu.com/p/b8841a8925fb spark性能优化 1.诊断内存的消耗 2. 高性能序列化类库 3. 优化数据结构 4. 对多次使用的rdd进行持久化或者 ...

  10. redis 可视化管理工具

    Redis Desktop Manager 下载地址:http://redisdesktop.com/download 支持: Windows 7+, Mac OS X 10.10+, Ubuntu ...