在读取文件的时候,为了避免乱码,我们需要按照文件的编码格式来读取文件,但是如何获取文件的编码格式,成了一大难题,查了资料终于搞定,现拿来与大家分享。
<span style="white-space:pre"> </span>/**
*
* @Description 获取文件编码格式
* @param fileName
* @return
* @throws IOException
*/
public static String getCharset(File fileName) throws IOException{
BufferedInputStream bin = new BufferedInputStream(new FileInputStream(fileName));
int p = (bin.read() << 8) + bin.read();
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";
}
if(bin!= null) bin.close();
return code;
}
读文件操作:
/**
*
* @Description 读文件 (路径下不存在,则创建文件)
* @param fileRoot 文件路径
* @param fileName 文件名
* @return
*/
public static String readFile(String fileRoot, String fileName) {
try {
if (!new File(fileRoot).isDirectory()) new File(fileRoot).mkdirs();
File file = new File(fileRoot, fileName);
return readFile(file);
}
catch (Exception e) {
return "";
}
}
/**
*
* @Description 读文件
* @param file 文件
* @return 文件内容
*/
private static String readFile(File file) {
try {
if (!file.exists()) file.createNewFile();
// BufferedReader br = new BufferedReader(new FileReader(file));
// 指定读取文件的编码格式,要和写入的格式一致,以免出现中文乱码,
// BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), getCharset(file)));
String line = br.readLine();
StringBuffer sb = new StringBuffer();
while (line != null) {
sb.append(line + "\n");
line = br.readLine();
}
br.close();
System.out.println(sb.toString());
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
写文件操作,按指定格式写入,这里是UTF-8:
/**
*
* @Description 写入文件
* @param fileRoot 文件路径
* @param fileName 文件名
* @param fileInfo 写入内容
*/
public static void writeFile(String fileRoot, String fileName, String fileInfo) {
if(!new File(fileRoot).isDirectory()){
new File(fileRoot).mkdirs();//创建此抽象路径名指定的目录,包括所有必需但不存在的父目录
}
//创建文件
File file = new File(fileRoot, fileName);
//写入文件
writeFile(file, fileInfo);
}
/**
*
* @Description 写入文件
* @param file 文件
* @param fileInfo 写入内容
*/
private static void writeFile(File file, String fileInfo) {
try {
if(!file.exists()){
file.createNewFile();
}
// BufferedWriter bw = new BufferedWriter(new FileWriter(file));
// 指定编码格式,以免读取时中文字符异常
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
bw.write(fileInfo);
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
创建文件:
/**
* 创建文件
* @Description
* @author zhangjinmiao
* @return
*/
public static boolean createFile(File fileName){
boolean flag = false;
if(!fileName.exists()){
try {
fileName.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
flag = true;
}
return flag;
}
删除文件:
/**
* 删除文件
* @Description
* @param sPath
* @return
*/
public static boolean deleteFile(String sPath) {
boolean flag = false;
File file = new File(sPath);
// 路径为文件且不为空则进行删除
if(file.isFile()&& file.exists()){
file.delete();
flag = true;
}
return flag;
}
追加内容到文件:
/**
*
* @Description 向文件中追加内容
* @param filePath 文件路径 F:/a/zz.txt
* @param content 追加的内容
*/
public static void contentToTxt(String filePath,String content){
String str = new String(); //原有txt内容
String s1 = new String();//内容更新
try {
File file = new File(filePath);
if(file.exists()){
System.out.println("文件存在");
}else {
System.out.println("文件不存在");
file.createNewFile();//创建文件
}
//开始读文件
// BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(file), getCharset(file)));
while ((str = input.readLine()) != null) {
s1 += str + "\n";
}
System.out.println("原有内容:\n"+s1);
input.close();
s1 += "\r\n"+ content;//新添内容换行加入
// BufferedWriter output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"UTF-8"));
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),getCharset(file)));
output.write(s1);
output.close();
} catch (Exception e) {
e.printStackTrace();
}
}