inputstream流乱码_Java FileInputStream读中文乱码问题解决方案

1、前提

以读取编码是GBK的文件为案例,文件内容只有中文和中文符号

2、原因

FileInputStream读中文乱码是因为一个中文对应两个字节存储(负数),也就是说,读取对应中文的字节数应该是偶数; 而英文对应一个字节存储。FileInputStream每次读取一个数组长度的字节时,读取的中文字节数可能是奇数,也就是只读到中文的一半字节,出现乱码。

3、解决方法

一次读取所有字节,此方法不靠谱,因为不确定总字节数。

在输出时进行判断,遍历数组判断负数的个数,如果是奇数,说明读取到中文的一半字节,对数组进行扩容再输出;否则正常输出

4、代码案例

package 第二题;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import java.util.Arrays;

public class MainTest {

public static void main(String[] args) throws UnsupportedEncodingException {

// 创建File对象

File file = new File("D:\\filetest\\file4.txt");

FileInputStream fileInputStream = null;

try {

// 新建一个FileInputStream对象

fileInputStream = new FileInputStream(file);

// 新建一个字节数组

byte[] buf = new byte[2];

// read(buf):此方法的返回值就是当前读取的字节个数,将数据读取到buf数组

// 将readLen变量也就是read方法的返回值,当此变量等于-1,则读到文件末尾

int readLen = -1;

//读取文件数据

while ((readLen = fileInputStream.read(buf)) != -1) {

int pos=0;//记录负数的个数

for(byte v:buf)

{

if(v<0)

{

pos++;

}

}

//负数个数为偶数,读取完整,没有读取到半个中文

if(pos%2==0)

{

// 将字节数组转换成字符串

String content = new String(buf, 0, readLen);

System.out.print(content);

}else {//负数个数为奇数,读取不完整,会乱码

//再读取下一位字节

int nextByteValue=fileInputStream.read();

int nextLen=readLen+1;

//字节数组扩容一位

buf= Arrays.copyOf(buf,nextLen);

buf[readLen]= (byte) nextByteValue;

String content=new String(buf,0,nextLen);

System.out.print(content);

//奇数,字节补全

//针对数组扩容一个字节单元

/* buf=Arrays.copyOf(buf, readLen+1);

int nextByteValue=fileInputStream.read();

buf[readLen]= (byte) nextByteValue;

String content = new String(buf, 0, readLen);

System.out.print(content);*/

}

}

} catch (FileNotFoundException e) {

// 输出堆栈信息

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

// 文件输入流关闭(释放资源)

fileInputStream.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持龙方网络。

Java中使用Zip压缩中文文件名可能会出现乱码问题,这是因为Zip文件格式默认使用的是ASCII编码,而中文字符需要使用其他编码方式进行处理。解决这个问题的方法有两种: 1. 使用Apache Commons Compress库 可以使用Apache Commons Compress库来压缩文件,这个库支持多种编码方式,可以解决中文文件名乱码问题。示例代码如下: ```java import org.apache.commons.compress.archivers.ArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import org.apache.commons.compress.utils.IOUtils; import java.io.*; public class ZipUtil { public static void zip(String sourcePath, String destPath) throws IOException { FileOutputStream fos = new FileOutputStream(destPath); ZipArchiveOutputStream zos = new ZipArchiveOutputStream(fos); File file = new File(sourcePath); String basePath = file.getParent(); compress(file, zos, basePath); zos.close(); fos.close(); } private static void compress(File file, ZipArchiveOutputStream zos, String basePath) throws IOException { if (file.isDirectory()) { File[] files = file.listFiles(); if (files.length == 0) { String path = file.getPath().substring(basePath.length() + 1) + "/"; ArchiveEntry entry = zos.createArchiveEntry(new File(file.getPath()), path); zos.putArchiveEntry(entry); zos.closeArchiveEntry(); } else { for (File f : files) { compress(f, zos, basePath); } } } else { String path = file.getPath().substring(basePath.length() + 1); ArchiveEntry entry = zos.createArchiveEntry(new File(file.getPath()), path); zos.putArchiveEntry(entry); InputStream is = new FileInputStream(file); IOUtils.copy(is, zos); is.close(); zos.closeArchiveEntry(); } } } ``` 2. 使用Java的ZipOutputStream类 Java中自带的ZipOutputStream类也可以用于压缩文件,但是需要手动设置编码方式,示例代码如下: ```java import java.io.*; import java.nio.charset.Charset; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZipUtil { private static final int BUFFER_SIZE = 2 * 1024; public static void zip(String sourcePath, String destPath) throws IOException { FileOutputStream fos = new FileOutputStream(destPath); ZipOutputStream zos = new ZipOutputStream(fos, Charset.forName("GBK")); File file = new File(sourcePath); String basePath = file.getParent(); compress(file, zos, basePath); zos.close(); fos.close(); } private static void compress(File file, ZipOutputStream zos, String basePath) throws IOException { if (file.isDirectory()) { File[] files = file.listFiles(); if (files.length == 0) { String path = file.getPath().substring(basePath.length() + 1) + "/"; ZipEntry entry = new ZipEntry(path); zos.putNextEntry(entry); zos.closeEntry(); } else { for (File f : files) { compress(f, zos, basePath); } } } else { String path = file.getPath().substring(basePath.length() + 1); ZipEntry entry = new ZipEntry(path); zos.putNextEntry(entry); InputStream is = new FileInputStream(file); byte[] buffer = new byte[BUFFER_SIZE]; int len; while ((len = is.read(buffer)) != -1) { zos.write(buffer, 0, len); } is.close(); zos.closeEntry(); } } } ``` 以上两种方法都可以解决中文文件名乱码问题,具体使用哪一种方法可以根据实际需要选择。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值