JAVA读取文件里面部分汉字内容乱码

JAVA读取文件里面部分汉字内容乱码

读取一个txt文件,到代码中打印出来,发票有部分汉字的内容是乱码的。我开始的方式是这样的, 如下,这是完全错误的,汉字是两个字节的,如果每次读固定个字节,可能会把汉字截断。就会出现部分乱码的情况。

package susq.path;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author susq
 * @since 2018-05-18-19:28
 */
public class WrongMethodReadTxt {
    public static void main(String[] args) throws IOException {
        ClassLoader classLoader = WrongMethodReadTxt.class.getClassLoader();
        String filePath = classLoader.getResource("").getPath() + "/expect1.txt";

        System.out.println(filePath);

        File file = new File(filePath);
        try (FileInputStream in = new FileInputStream(file)) {
            byte[] bytes = new byte[1024];
            StringBuffer sb = new StringBuffer();
            int len;
            while ((len = in.read(bytes)) != -1) {
                sb.append(new String(bytes, 0, len));
            }
            System.out.println(sb.toString());
        }
    }
}

如果存在汉字,就要按字符的方式读取:

package susq.path;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author susq
 * @since 2018-05-18-17:39
 */
public class SysPath {
    public static void main(String[] args) throws IOException {
        ClassLoader classLoader = SysPath.class.getClassLoader();
        String filePath = classLoader.getResource("").getPath() + "/expect1.txt";

        System.out.println(filePath);

        File file = new File(filePath);
        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            StringBuffer sb = new StringBuffer();
            while (br.ready()) {
                sb.append(br.readLine());
            }
            System.out.println(sb);
        }
    }
}

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Java中解压和压缩文件内容时,如果遇到汉字乱码问题,可以使用正确的字符编码来解决。 在解压文件时,可以使用`ZipFile`类和`ZipEntry`类来遍历压缩包中的每个文件,并通过`InputStream`读取文件内容。在读取内容之前,我们可以使用`ZipEntry`的`getEncoding`方法获取文件名的编码格式。如果编码格式不为null,则表示文件名使用了特定的编码格式,我们可以使用该编码格式来读取文件内容。 如果文件名的编码格式为null,那么默认使用UTF-8编码来读取文件内容。 例如,以下是使用Java解压文件内容并解决汉字乱码问题的示例代码: ```java import java.io.*; import java.nio.charset.StandardCharsets; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class UnzipExample { public static void main(String[] args) { try { String zipFilePath = "path/to/your/zipFile.zip"; ZipFile zipFile = new ZipFile(zipFilePath); Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); String fileName; if (entry.getEncoding() != null) { fileName = new String(entry.getName().getBytes(StandardCharsets.ISO_8859_1), entry.getEncoding()); } else { fileName = entry.getName(); } System.out.println(fileName); InputStream inputStream = zipFile.getInputStream(entry); // 读取文件内容... inputStream.close(); } zipFile.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 通过在代码中正确地处理文件名的编码格式,我们可以避免解压压缩文件内容出现汉字乱码的问题。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值