昨天读取txt文档的时候始终出现中文乱码,去好多网站看了,都TMD一坑,根本没有解决问题嘛!大多数博客上写的的格式就是一样的不晓得哪个超哪个,最后终于在http://blog.csdn.net/maxracer/article/details/5436580上找到解决办法,进行一些改进后,得到可行的方法

1.Read.java

import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.RandomAccessFile;import java.io.Reader;

public class Read {  /**     * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。     */

        public static void readFileByBytes(String fileName) {        File file = new File(fileName);        InputStream in = null;        try {                       // 一次读n个字节           int n=100;            byte[] tempbytes = new byte[n];            int byteread = 0;            in = new FileInputStream(fileName);            Read.showAvailableBytes(in);//查看剩余字节数            while ((byteread = in.read(tempbytes)) != -1) {                     System.out.print(new String(tempbytes, 0, byteread, "GB2312"));            }        } catch (Exception e1) {            e1.printStackTrace();        } finally {            if (in != null) {                try {                    in.close();                } catch (IOException e1) {                }            }        }    }

    /**     * 以字符为单位读取文件,常用于读文本,数字等类型的文件     */    public static void readFileByChars(String filePath) {        File file = new File(filePath);        Reader reader = null;        try {                        // 一次读一个字符            reader = new InputStreamReader(new FileInputStream(file),"GB2312");            int tempchar;            while ((tempchar = reader.read()) != -1) {                // 对于windows下,\r\n这两个字符在一起时,表示一个换行。                // 但如果这两个字符分开显示时,会换两次行。                // 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。                if (((char) tempchar) != '\r') {                    System.out.print((char) tempchar);                }            }            reader.close();        } catch (Exception e) {            e.printStackTrace();        }        try {                        // 一次读多个字符            char[] tempchars = new char[30];            int charread = 0;            reader = new InputStreamReader(new FileInputStream(filePath),"GB2312");            // 读入多个字符到字符数组中,charread为一次读取字符数            while ((charread = reader.read(tempchars)) != -1) {                // 同样屏蔽掉\r不显示                if ((charread == tempchars.length)                        && (tempchars[tempchars.length - 1] != '\r')) {                    System.out.print(tempchars);                } else {                    for (int i = 0; i < charread; i++) {                        if (tempchars[i] == '\r') {                            continue;                        } else {                            System.out.print(tempchars[i]);                        }                    }                }            }

        } catch (Exception e1) {            e1.printStackTrace();        } finally {            if (reader != null) {                try {                    reader.close();                } catch (IOException e1) {                }            }        }    }

    /**     * 以行为单位读取文件,常用于读面向行的格式化文件     */    public static void readFileByLines(String filePath) {        File file = new File(filePath);        BufferedReader reader = null;        try {            //以行为单位读取文件内容,一次读一整行            //reader = new BufferedReader(new FileReader(file));//换成下面两句            InputStreamReader isr=new InputStreamReader(new FileInputStream(file),"gb2312");            reader=new BufferedReader(isr);            String tempString = null;            int line = 1;            // 一次读入一行,直到读入null为文件结束            while ((tempString = reader.readLine()) != null) {                               //tempString=new String(tempString.getBytes("iso-8859-1"),"gb2312");//如果要这段代码,则上面设置的“gb2312”不要                System.out.println("line " + line + ": " + tempString);                line++;                            }            reader.close();        } catch (IOException e) {            e.printStackTrace();        } finally {            if (reader != null) {                try {                    reader.close();                } catch (IOException e1) {                }            }        }    }

    /**     * 随机读取文件内容     */    public static void readFileByRandomAccess(String filePath) {        RandomAccessFile randomFile = null;        try {            //随机读取一段文件内容            // 打开一个随机访问文件流,按只读方式            randomFile = new RandomAccessFile(filePath, "r");//filePath换成new File(filePath)也可以            // 文件长度,字节数            long fileLength = randomFile.length();            // 读文件的起始位置            int beginIndex = (fileLength > 4) ? 4 : 0;            // 将读文件的开始位置移到beginIndex位置。            randomFile.seek(beginIndex);            byte[] bytes = new byte[10];            int byteread = 0;            // 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。            // 将一次读取的字节数赋给byteread            while ((byteread = randomFile.read(bytes)) != -1) {                System.out.print(new String(bytes, 0, byteread,"GB2312"));            }        } catch (IOException e) {            e.printStackTrace();        } finally {            if (randomFile != null) {                try {                    randomFile.close();                } catch (IOException e1) {                }            }        }    }

    /**     * 显示输入流中还剩的字节数     */    private static void showAvailableBytes(InputStream in) {        try {            System.out.println("当前字节输入流中的字节数为:" + in.available());        } catch (IOException e) {            e.printStackTrace();        }    }

}

2.Start.java

public class Start {

   public static void main(String[] args) {      String filePath = "F:/txt/tsxk1.txt";        Read.readFileByBytes(filePath);        Read.readFileByChars(filePath);        Read.readFileByLines(filePath);        Read.readFileByRandomAccess(filePath);   }

}

读取txt文件没问题,其他文件有待改进。。。

txt文件读取(已解决中文乱码)相关推荐

  1. JAVA:如何读写txt文件,并解决中文乱码问题

    读写文件的编码格式需要保持一致,否则可能会出现中文乱码问题.以下使用UTF-8编码方式写入和读取文件. 1.写入txt文件 //--------------写入文本-------------//Str ...

  2. java 文件读取中文乱码_java文件读取时发生中文乱码怎么解决

    java文件读取时发生中文乱码怎么解决 发布时间:2020-06-23 09:33:47 来源:亿速云 阅读:59 作者:Leah java文件读取时发生中文乱码怎么解决?这篇文章运用了实例代码展示, ...

  3. JAVA之NIO按行读写大文件,完美解决中文乱码问题

    JAVA之NIO按行读写大文件,完美解决中文乱码问题 参考文章: (1)JAVA之NIO按行读写大文件,完美解决中文乱码问题 (2)https://www.cnblogs.com/jpfss/p/89 ...

  4. 如何解决Kettle读取txt文件时出现的中文乱码问题?

    使用Kettle的[文本文件输入]读取txt文件时,其中的两个中文字段无法正确识别,其预览效果如下: 出现中文乱码自然想到是文件编码的问题,该txt文件的编码方式为:UTF-8 将该文件的编码方式改为 ...

  5. Windows API CreateFile WriteFile 操作文件追加写入 解决中文乱码

    编译环境 WIn10 21H1 VS2017 字符集随意切换  解决乱码问题! 切换字符集后必需删除之前的文本文件! // CreateFile.cpp : 此文件包含 "main" ...

  6. java实现解压(zip和rar文件)实例-解决中文乱码问题

    1.实现说明: 在下面引入的包中可以看出: 1)压缩.解压zip是用到的apache 的zip包,需要在引入jar包(jar-ant.rar): 2)压缩.解压rar需要用到的是junrar,这里需要 ...

  7. idea Java 读取rtf文件内容,解决中文乱码的问题。

    百度找了很多java读取rtf文件内容的案例,都不行,不是读不出来就是乱码,要么就是多了很多编码异常的字符. 最后成功解决,得益于大佬的源码贡献,如下链接: 转载于:http://blog.sina. ...

  8. java nio 按行读取_JAVA之NIO按行读写大文件,完美解决中文乱码问题

    packagecom.chillax.imp; importjava.io.File; importjava.io.IOException; importjava.io.RandomAccessFil ...

  9. 【IntelliJ IDEA】从资源文件读取出来就中文乱码的解决方法

    在application.properties资源文件中设置两个自定义的属性以及属性值: com.sxd.name = "德玛西亚" com.sxd.want = "王者 ...

最新文章

  1. sqlserver、oracle数据库排序空值null问题解决办法
  2. Microsoft Power BI Desktop概念学习系列之Microsoft Power BI Desktop的下载和安装(图文详解)...
  3. SSM+mybatis单元测试
  4. PHP ajax 传递中文乱码,ajax+php传递中文乱码解决办法
  5. 突然!iPhone 12/12 Pro从苹果天猫旗舰店下架,不愿参加双11活动?
  6. 页面显示pdf_PDF怎样合并?在Mac上合并PDF文件的最佳方法
  7. CentOS下桥接模式连不上网
  8. 超搞笑之仙桃有没有毛?
  9. 【论文】本周论文推荐(迁移学习、阅读理解、对话系统、图神经网络、对抗生成网络等)...
  10. MySql命令行下导出、导入数据
  11. 股票指标php源码,股票软件通达信精品指标公式-MACD转折点指标源码
  12. 动态网络社区检测概述
  13. amd显卡风扇调节_amd显卡风扇速度设置linux版本
  14. 园区人工智能开启双创模式,“1+N”创新型组织发展成效初显...
  15. #每日一题 力扣第22题 黑白格子画
  16. 惠普暗影精灵命令中心linux,暗影精灵2成功装上Sierra 12.5正式版
  17. 内网渗透-端口转发隧道技术
  18. CIKM 2021 | DISENKGAT:知识图谱解耦表征学习
  19. 用php做一个网站,如何用PHP开发一个完整的网站
  20. php 防止恶意灌水,discuz!使用技巧(2)如何防止用户在论坛恶意灌水?_discuz!论坛...

热门文章

  1. centos7几种版本x86_64、aarch64、armhfp、i386、power9、ppc64、ppc64el简单说明
  2. Python学习笔记05----条件、循环及其他语句
  3. 女生,你也可以IT!
  4. 【人脸识别】调用百度AI开放平台人脸识别接口,获取图片结果
  5. 后台开发面试问题整理
  6. 嵌入式作业STM32采用串口DMA方式发送数据
  7. Windows 11最新版:任务管理器性能参数详解,什么是分页缓冲池和句柄
  8. 用Python实现带GUI 的exe
  9. Coda:从我们周围的结构中学习
  10. 一文了解黄金票据和白银票据