有时间根据需要必须要求上传的txt文本是UTF-8格式的才可以操作,不会出现乱码。
然而我们不确定txt文本的原本编码到底是不是UTF-8的,需要进行处理。
java为txt文本转码代码如下:
InputStream isbr = new FileInputStream(“上传的txt文件”);
byte[] head = new byte[3];
isbr.read(head);
//判断是否为utf-8格式的是就不转码不是就转码。
if(head[0]==-17 && head[1]==-69 && head[2] ==-65){
BufferedReader br = new BufferedReader(new FileReader(“上传txt的文件”));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(“文件的落地位置”, “上传txt的文件”))));
char[] b = new char[1024 * 5];
int len=0;
while ((len = br.read(b)) != -1) {
bw.write(b, 0, len);
}
bw.flush();
br.close();
bw.close();
}else{
BufferedReader br = new BufferedReader(new FileReader(“上传txt的文件”));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(“文件落地位置”, “上传txt的文件”)),"utf-8"));
char[] b = new char[1024 * 5];
int len;
while ((len = br.read(b)) != -1) {
bw.write(b, 0, len);
}
bw.flush();
br.close();
bw.close();
}
生成的方法:
import org.apache.commons.io.FileUtils;
public File TXTHandler(File file) {
String code = "gb2312";
byte[] head = new byte[3];
try {
InputStream inputStream = new FileInputStream(file);
inputStream.read(head);
if (head[0] == -1 && head[1] == -2) {
code = "UTF-16";
} else if (head[0] == -2 && head[1] == -1) {
code = "Unicode";
} else if (head[0] == -17 && head[1] == -69 && head[2] == -65) {
code = "UTF-8";
}
inputStream.close();
System.out.println(code);
if (code.equals("UTF-8")) {
return file;
}
String str = FileUtils.readFileToString(file, code);
FileUtils.writeStringToFile(file, str, "UTF-8");
System.out.println("转码结束");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return file;
}