-
2020-12-19 19:37:26
读:
File file=new File(this.filePath);
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(file),"utf-8"));
String line=null;
while((line=br.readLine())!=null){
if(!line.trim().equals("")){
if(checkDuplicateRow(line.trim()))
tempList.add(line.trim());
}
}
写:
File filename = new File("xxx");
filename.createNewFile();
FileWriter fw = new FileWriter(filename);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename),"GBk")));
out.write(hexToString(search_result));
out.flush();
out.close();
更多相关内容 -
用java修改文件的编码
2017-07-23 19:31:34用java修改文件的编码 -
java 更改文件编码
2021-03-13 04:31:20直接上代码import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FilenameFilter;import java.io....直接上代码
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class TransCoding {
//需要转编码的目录
private static String path = "C:\\Users\\Administrator\\Desktop\\123\\HelpUDcAppBG";
//定义另存目录。若不赋值(但此属性的值必须为字符串,如”“,不能为null)
//即当前目录下文件转变编码后会覆盖,包括子目录及其文件
private static String directory = "C:\\Users\\Administrator\\Desktop\\12";
//定义要转的文件类型。若不赋值(但此属性的值必须为字符串,如”“,不能为null)
//即转变当前目录下所有类型文件,包括子目录及其文件。
private static String typeName = "";
//要转的文件当前编码,默认GBK。因为一般硬盘文件默认ANSI,在中国即是GBK编码。
private static String codeBefore = "GB2312";
//要转的文件转后编码,默认UTF-8。
//需要注意的是如果被编码的文件是全英文的,没有汉字,那么即使下面指定UTF-8,
//有些计算机也不能生成UTF-8文件,可能与windows的转码有关,但全英文不影响性能。
private static String codeAfter = "UTF-8";
public static void main(String[] args){
File dir = new File(path);
File dirPath = new File(directory);
change(dir,dirPath,typeName);
}
public static void change(File dir,File dirPath,final String typeName){
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter =null;
//获得当前目录下的所有符合条件的对象,包括目录。
File[] files = dir.listFiles(new FilenameFilter(){
public boolean accept(File dir,String name){
//如果是目录,直接返回true,意味着可以直接加入listFiles方法里的集合中。
if(new File(dir,name).isDirectory()){
return true;
};
return name.endsWith(typeName);
}
});
for (File file : files) {
//如果没有指定另存目录名,此为当前目录的绝对路径
String pathAbsolute = null;
//如果没有指定另存目录名,此为当前文件编码后的绝对路径
File fileModify = null;
//定义另存目录对象
File createDir = null;
//定义另存目录中的文件对象
File createFile = null;
//如果当前file对象是一个目录,再调用此方法,递归。
if(file.isDirectory()){
//获取此目录绝对路径
String path = file.getAbsolutePath();
//截掉当前目录
String path2 = path.substring(0,path.lastIndexOf("\\"));
//获取到上级目录
String path3 = path2.substring(path2.lastIndexOf("\\"));
//封装成对象方便传递。
File file2 =null;
if(dirPath.getName()==null||dirPath.getName().trim()==""){
change(file,dirPath,typeName);
}else{
file2 = new File(dirPath,path3);
change(file,file2,typeName);
}
}else{//不是目录,直接转码
try {
//读取目录下文件,并按指定编码读取。
bufferedReader = new BufferedReader(new InputStreamReader(
new FileInputStream(file.getAbsolutePath()),codeBefore));
//如果另存目录为空,表示存放到当前目录。
if(dirPath.getName()==null||dirPath.getName()==""){
//绝对路径名 如 D:\action\1.txt
pathAbsolute = file.getAbsolutePath();
//截取后的路径 如D:\action\
String path1 = pathAbsolute.substring(0,
pathAbsolute.lastIndexOf(file.getName()));
//新路径 如 D:\action\11.txt
String pathModify = path1+"1"+file.getName();
fileModify = new File(pathModify);
bufferedWriter = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(fileModify),codeAfter));
}else{//否则,将转码后的文件写入指定的目录
String path = file.getAbsolutePath();
String fileName = file.getName();
//获取文件所在的绝对路径目录
String path2 = file.getAbsolutePath().substring(0,
path.lastIndexOf(fileName)-1);
//获取文件所在的上一级目录
String path3 = path2.substring(path2.lastIndexOf("\\"));
//创建另存目录
createDir = new File(dirPath,path3);
//这里是创建多级目录,防止条件转码时(例如后缀名为.java或.txt),
//符合条件的文件太深,造成目录创建失败,导致IO写入异常。
createDir.mkdirs();
createFile = new File(createDir,fileName);
bufferedWriter = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(createFile),codeAfter));
}
String line = null;
while((line=bufferedReader.readLine())!=null){
bufferedWriter.write(line);
bufferedWriter.newLine();
}
//我觉得这里写不写都一样,最终关闭流的时候也会刷新。
//如果写入while循环里会降低效率,每行都要刷新。
//因为那样写入磁盘的次数就增多了
bufferedWriter.flush();
} catch (Exception e) {
if(createDir!=null)
createDir.deleteOnExit();
if(createFile!=null)
createFile.deleteOnExit();
throw new RuntimeException("读写错误"+e);
}
finally{
if(bufferedReader!=null){
try {
bufferedReader.close();
} catch (IOException e) {
throw new RuntimeException("输入流关闭错误");
}
}
if(bufferedWriter!=null){
try {
bufferedWriter.close();
if(fileModify!=null){
//将原文件删除
file.delete();
//创建一个和原文件同名的File对象
File file3 = new File(pathAbsolute);
//将编码后的文件名改成原文件名
fileModify.renameTo(file3);
}
} catch (IOException e) {
throw new RuntimeException("输出流关闭错误");
}
}
}
}
}
}
}
-
java中怎么获取、设置文件编码格式?
2021-02-28 12:43:55System.out.println("修改文件编码之后getEncoding:" + osw.getEncoding()); osw.close(); System.out.println(codeString("e:\\test.txt")); } catch (FileNotFoundException e) { e.printStackTrace(); } catch ...public class OutputStreamWriterTest {
public static void main(String[] args) {
try {
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("e:\\test.txt"));
osw.write("学海无涯,维勤是岸!!!");
System.out.println("文件默认编码:" + osw.getEncoding());// 使用getEncoding()方法取得当前系统的默认字符编码
osw.close();
/*
* 如果在调用FileOutputStream的构造方法时没有加入true,那么新加入的字符串就会替换掉原来写入的字符串,
* 在调用构造方法时指定了字符的编码,新写入的字符,会使用新指定的编码
*/
osw = new OutputStreamWriter(new FileOutputStream("e:\\test.txt", true), "ISO8859_1");
osw.write("他山之石,可以攻玉!!!");
System.out.println("修改文件编码之后getEncoding:" + osw.getEncoding());
osw.close();
System.out.println(codeString("e:\\test.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
}
}
public static String codeString(String fileName) throws Exception {
BufferedInputStream bin = new BufferedInputStream(new FileInputStream(fileName));
int p = (bin.read() << 8) + bin.read();
String code = null;
// 其中的 0xefbb、0xfffe、0xfeff、0x5c75这些都是这个文件的前面两个字节的16进制数
switch (p) {
case 0xefbb:
code = "UTF-8";
break;
case 0xfffe:
code = "Unicode";
break;
case 0xfeff:
code = "UTF-16BE";
break;
case 0x5c75:
code = "ANSI|ASCII";
break;
default:
code = "GBK";
}
return code;
}
}
-
java获取文件编码
2022-02-12 10:24:54java编码 移位运算 获取文件编码目录
4.2. 判断前三个字节出错率还是蛮大的,还可以进一步读取文件的字段,进行特殊编码字符的判断来确定文件编码
1. 概述
在下面的描述中,将以"中文"两个字为例,经查表可以知道其GB2312编码是"d6d0 cec4",Unicode编码为"4e2d 6587",UTF编码就是"e4b8ad e69687"。注意,这两个字没有iso8859-1编码,但可以用iso8859-1编码来"表示"。
2. 编码基本知识
最早的编码是iso8859-1,和ascii编码相似。但为了方便表示各种各样的语言,逐渐出现了很多标准编码,重要的有如下几个。
2.1. iso8859-1
属于单字节编码,最多能表示的字符范围是0-255,应用于英文系列。比如,字母'a'的编码为0x61=97。很明显,iso8859-1编码表示的字符范围很窄,无法表示中文字符。但是,由于是单字节编码,和计算机最基础的表示单位一致,所以很多时候,仍旧使用iso8859-1编码来表示。而且在很多协议上,默认使用该编码。比如,虽然"中文"两个字不存在iso8859-1编码,以gb2312编码为例,应该是"d6d0 cec4"两个字符,使用iso8859-1编码的时候则将它拆开为4个字节来表示:"d6 d0 ce c4"(事实上,在进行存储的时候,也是以字节为单位处理的)。而如果是UTF编码,则是6个字节"e4 b8 ad e6 96 87"。很明显,这种表示方法还需要以另一种编码为基础。
2.2. GB2312/GBK
这就是汉子的国标码,专门用来表示汉字,是双字节编码,而英文字母和iso8859-1一致(兼容iso8859-1编码)。其中gbk编码能够用来同时表示繁体字和简体字,而gb2312只能表示简体字,gbk是兼容gb2312编码的。
2.3. unicode
这是最统一的编码,可以用来表示所有语言的字符,而且是定长双字节(也有四字节的)编码,包括英文字母在内。所以可以说它是不兼容iso8859-1编码的,也不兼容任何编码。不过,相对于iso8859-1编码来说,unicode编码只是在前面增加了一个0字节,比如字母'a'为"00 61"。
需要说明的是,定长编码便于计算机处理(注意GB2312/GBK不是定长编码),而unicode又可以用来表示所有字符,所以在很多软件内部是使用unicode编码来处理的,比如java。2.4. UTF
考虑到unicode编码不兼容iso8859-1编码,而且容易占用更多的空间:因为对于英文字母,unicode也需要两个字节来表示。所以unicode不便于传输和存储。因此而产生了utf编码,utf编码兼容iso8859-1编码,同时也可以用来表示所有语言的字符,不过,utf编码是不定长编码,每一个字符的长度从1-6个字节不等。另外,utf编码自带简单的校验功能。一般来讲,英文字母都是用一个字节表示,而汉字使用三个字节。
注意,虽然说utf是为了使用更少的空间而使用的,但那只是相对于unicode编码来说,如果已经知道是汉字,则使用GB2312/GBK无疑是最节省的。不过另一方面,值得说明的是,虽然utf编码对汉字使用3个字节,但即使对于汉字网页,utf编码也会比unicode编码节省,因为网页中包含了很多的英文字符。3.JAVA中移位运算>> , << , >>>
十进制12345的二进制表达式如下图:
3.1. 左移<<
左移一位相当于乘2,右移相当于除2。但是当左移18位后,首位是1,是负数。
当左移18位后,变成了负数,而当右移20位时又变成了整数,移位运算和乘2不是完全一样的。
那么问题来了,对于int类型进行移位运算,左移32位是不是在右边直接补32个0呢?结果是不是0呢?
答案肯定不是这样的。当位数超出int类型32位时,先进行求余数,移动的位数%32。<<36和<<4是一样的。
3.2. 右移
无符号右移运算符和右移运算符是一样的,不过无符号右移运算符在右移的时候是补0的,而右移运算符是补符号位的 在右移运算符中,右移后补0,是由于正数 12345 符号位为0 ,如果为1 则应补1
3.3. 无符号右移>>>
无符号右移运算符和右移运算符是一样的,不过无符号右移运算符在右移的时候是补0的
4. JAVA获取文件编码
ANSI: 无格式定义
Unicode: 前两个字节为FFFE Unicode文档以0xFFFE开头
Unicode big endian: 前两字节为FEFF
UTF-8: 前两字节为EFBB UTF-8以0xEFBBBF开头4.1. 通过文件的前三个字节来判断
public static String codeString(String fileName) throws Exception { BufferedInputStream bin = new BufferedInputStream(new FileInputStream(fileName)); int p = (bin.read() << 8) + bin.read(); bin.close(); String code = null; switch (p) { case 0xefbb: code = "UTF-8"; break; case 0xfffe: code = "Unicode"; break; case 0xfeff: code = "UTF-16BE"; break; default: code = "GBK"; } return code; }
4.2. 判断前三个字节出错率还是蛮大的,还可以进一步读取文件的字段,进行特殊编码字符的判断来确定文件编码
/** * 判断文本文件的字符集,文件开头三个字节表明编码格式。 * @param path * @return * @throws Exception * @throws Exception */ public static String charset(String path) { String charset = "GBK"; byte[] first3Bytes = new byte[3]; try { boolean checked = false; BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path)); bis.mark(0); // 读者注: bis.mark(0);修改为 bis.mark(100);我用过这段代码,需要修改上面标出的地方。 int read = bis.read(first3Bytes, 0, 3); if (read == -1) { bis.close(); return charset; // 文件编码为 ANSI } else if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) { charset = "UTF-16LE"; // 文件编码为 Unicode checked = true; } else if (first3Bytes[0] == (byte) 0xFE && first3Bytes[1] == (byte) 0xFF) { charset = "UTF-16BE"; // 文件编码为 Unicode big endian checked = true; } else if (first3Bytes[0] == (byte) 0xEF && first3Bytes[1] == (byte) 0xBB && first3Bytes[2] == (byte) 0xBF) { charset = "UTF-8"; // 文件编码为 UTF-8 checked = true; } bis.reset(); if (!checked) { while ((read = bis.read()) != -1) { if (read >= 0xF0) break; if (0x80 <= read && read <= 0xBF) // 单独出现BF以下的,也算是GBK break; if (0xC0 <= read && read <= 0xDF) { read = bis.read(); if (0x80 <= read && read <= 0xBF) // 双字节 (0xC0 - 0xDF) // (0x80 - 0xBF),也可能在GB编码内 continue; else break; } else if (0xE0 <= read && read <= 0xEF) { // 也有可能出错,但是几率较小 read = bis.read(); if (0x80 <= read && read <= 0xBF) { read = bis.read(); if (0x80 <= read && read <= 0xBF) { charset = "UTF-8"; break; } else break; } else break; } } } bis.close(); } catch (Exception e) { e.printStackTrace(); } System.out.println("--文件-> [" + path + "] 采用的字符集为: [" + charset + "]"); return charset; }
4.3. 通过工具库cpdetector来获取文件编码
public static String getFileCharset(String filePath) throws Exception { CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance(); /*ParsingDetector可用于检查HTML、XML等文件或字符流的编码, * 构造方法中的参数用于指示是否显示探测过程的详细信息,为false不显示。 */ detector.add(new ParsingDetector(false)); /*JChardetFacade封装了由Mozilla组织提供的JChardet,它可以完成大多数文件的编码测定。 * 所以,一般有了这个探测器就可满足大多数项目的要求,如果你还不放心,可以再多加几个探测器, * 比如下面的ASCIIDetector、UnicodeDetector等。 */ detector.add(JChardetFacade.getInstance()); detector.add(ASCIIDetector.getInstance()); detector.add(UnicodeDetector.getInstance()); Charset charset = null; File file = new File(filePath); try { //charset = detector.detectCodepage(file.toURI().toURL()); InputStream is = new BufferedInputStream(new FileInputStream(filePath)); charset = detector.detectCodepage(is, 8); } catch (Exception e) { e.printStackTrace(); throw e; } String charsetName = "GBK"; if (charset != null) { if (charset.name().equals("US-ASCII")) { charsetName = "ISO_8859_1"; } else if (charset.name().startsWith("UTF")) { charsetName = charset.name();// 例如:UTF-8,UTF-16BE. } } return charsetName; }
-
java批量修改指定文件夹下多级文件编码格式
2018-11-21 17:08:20经常碰到文件编码格式不一致导致一堆问题,想在linux上批量修改文件夹下的文件编码,操作太麻烦,花了一点时间写了个java程序来操作,用着非常方便,详情如下: 在FileEncodeTranslate类中,修改一下源文件夹路径,... -
读取创建CSV文件并自动解析文件编码方式
2018-05-08 16:22:08读取与创建CSV文件,根据第三方jar包自动解析文件编码方式,相关jar包与使用说明 -
Java 修改编码格式的几种方式
2017-05-10 20:05:001、工作空间 workspase Window→Preferences→General→Workspace→Text file encoding→other→UTF-8 2、项目编码格式 ...3、修改文件编码 右键文件名→Properties→Text file encoding→othe... -
修改文件字符编码
2012-05-27 10:25:09myeclipse或者eclipse中修改jsp、js等文件的编码方式 -
Java如何在创建文件时指定编码
2022-03-24 10:39:11前言:最近,学习了Java IO流的相关的知识,想通过读写文件的方式练习和巩固所学知识。在使用File类创建文件时,突然想到,我该如何指定文件使用的编码呢? 进而想到,应该如何查看一个文件的编码呢? 一、问题分析... -
IDEA修改某个文件的编码
2022-03-22 14:52:00背景:注意是想要修改某个单一文件的编码,而不是项目全局的编码。我想对某个文件改变编码。 一、问题解决 在IDEA右下角,可以看到显示的当前文件的编码类型,点击后,就可以修改为我们需要的类型。 由于我设置的... -
Java中properties文件编码问题
2022-02-08 11:54:141、properties文件显示乱码问题 原因是因为properties默认使用ASCII码,就算在文件中填写了中文,再...文件编码 eclipse:右键该文件->properties 这里不但设置了编码格式为UTF-8,旁边还有Transparent native-to-a -
统一修改 java 文件编码格式
2016-12-07 11:27:01Eclipse 修改文件编码 -
java将数据写入.txt文件 编码为GBK
2021-03-23 14:29:19文件编码格式:gbk //有则添加,无则新建.txt文件 File file = new File("D:\\note\\hello.txt"); FileOutputStream fos = new FileOutputStream(file,true); Writer w = new OutputStreamWriter(fos, "gbk"); w... -
Eclipse中java文件编码格式修改
2017-10-17 14:08:52在eclipse中可以影响文件编码格式的设定有好几处。先要知道当前情况下哪个设定起作用,再针对性的进行修改。 -
使用java进行文件编码转换
2014-07-17 22:56:11比如,原来文件本身的编码是GBK,现在要转换成UTF-8,如果直接在eclipse中把文件编码修改成UTF-8,恭喜你,是乱码,因为不能直接从GBK到UTF-8进行转换,这时就需要我们手动的来转换编码。下面是一个文件编码转换的... -
IDEA 文件编码修改
2019-06-20 17:59:41本文介绍如何在IntelliJ IDEA中修改文件编码,参考 IntelliJ IDEA 简体中文专题教程 ,英文好的同学可以查看 英文官网文档 文件编码修改 IntelliJ IDEA可以在菜单中的File -> Settings -> Editor -> File ... -
java判断txt文件的编码格式
2017-06-12 13:45:12//文件编码为 Unicode big endian checked = true; } else if (first3Bytes[0] == (byte) 0xEF && first3Bytes[1] == (byte) 0xBB && first3Bytes[2] == (byte) 0xBF) { charset = "UTF-8"; //文件编码为 UTF-... -
Java实现视频编码格式转换(转libx264编码格式)
2022-04-21 10:49:33导入依赖 <dependency> <groupId>ws.schild</groupId> <artifactId>jave-core</artifactId> <version>2.4.5</version> </dependency> <... -
eclipse 统一修改 java 文件编码格式
2016-12-07 11:35:23Eclipse 统一修改文件编码属性 -
用Java实现文本文件其他编码格式转UTF-8编码格式
2020-02-19 22:23:39一个一个的手动修改太麻烦了,就写了一个简单的java代码来实现批量转换一下。 主要完成的 import info.monitorenter.cpdetector.io.CodepageDetectorProxy; import info.monitorenter.cpdetector.io.JChardetF... -
Java修改文件MD5值-yellowcong
2017-07-15 16:17:10MD5是加密算法的一种,MD5对于每一个文件来说都是唯一的,在百度网盘、QQ的文件快传中,都是算文件的MD5在本地的服务器上有...但是有一个场景,就是我的文件在百度上是 违规的,我可以通过修改MD5值,然后上传问文件。 -
Intellij IDEA修改properties文件编码
2020-08-24 15:00:48Intellij Idea 修改 properties 文件编码 File -> Settings -> File Encodings -> Default encoding for properties file -> UTF-8。 勾选Transparent native-to-ascii conversion。 -
Java流处理之转换编码的转换流
2022-04-29 09:12:15♂️字符编码和字符集 字符编码 ♀️字符集 ⛹编码引出的问题 InputStreamReader类 构造方法 指定编码读取 OutputStreamWriter类 构造方法 ...⛷️转换文件编码案例 ♂️案例分析 ♂️案例实现 -
修改已创建的Java项目编码以及解决java文件中文乱码问题的解决方案
2019-04-25 16:59:171,工具中修改项目编码为UTF-8,此步会导致src中存在中文的java文件乱码; 2,使用程序将src目录下的文件以GBK(你没改成UTF-8前的编码)编码读出来,然后再以UTF-8的方式写入进去; 解决步骤: 1,选中项目,... -
eclipse 设置所有文件编码为UTF-8(最全)
2021-03-06 05:42:09eclipse 设置所有文件编码为UTF-8(最全)Created by Marydon on 2019-10-24 17:49如何修改eclipse工作空间的字符集以及项目的字符集?第一种方式:修改eclipse的...修改文件编码修改Java Class File的编码集Window--&... -
IntelliJ IDEA修改(文件)编码格式
2020-04-11 10:16:26本文介绍如何在IntelliJ IDEA中修改文件编码,参考IntelliJ IDEA 简体中文专题教程,英文好的同学可以查看英文官网文档 文件编码修改 IntelliJ IDEA可以在菜单中的File -> Settings -> Editor -> File ... -
java 5种方式读取配置文件 + 修改配置文件
2019-03-01 18:11:02方式一:采用ServletContext读取,读取配置文件的realpath,然后通过文件流读取出来。 因为是用ServletContext读取文件路径,所以配置文件可以放入在web-info的classes目录中,也可以在应用层级及web-info的目录中。... -
Zip解压-可设置压缩文件编码方式
2015-09-19 22:32:24jdk自带的ZipEntry类解压zip文件,中文文件会出现乱码,jar包是根据Apache的解压缩包进行改造的,也适合于Android使用