@
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
/**
* 文本文件工具箱
* @author LPY
*/
public class FileUtils {
/**
* 获取文件文本内容
* @param fileUrl 文件路径
* @return
*/
public static String getFileText(String fileUrl) {
fileUrl = removeBackSlant(fileUrl); //去除url结尾的所以反斜杠
if(fileUrl==null || fileUrl.length()==0){
String message = "文件路径为空!!";
throw new RuntimeException(message);
}
File file = new File(fileUrl);
String fileContentString = null;
if(!file.isFile()){
String msg = "不是一个标准的文件";
System.err.println(msg);
return null;
}
FileInputStream is = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
is = new FileInputStream(file);
byte[] buffers = new byte[512];
int count = 0;
while((count = is.read(buffers))>0){
bos.write(buffers, 0, count);
}
fileContentString = new String(bos.toByteArray(),"UTF-8");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(is != null){
try {
is.close();
is = null;
} catch (IOException e) {
e.printStackTrace();
}
}
if(bos != null){
try {
bos.close();
bos = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
return fileContentString;
}
/**
* 创建文本文件
* @param text
* @param catalogUrl
*/
public static void text2File(String text,String catalogUrl){
catalogUrl = removeBackSlant(catalogUrl); //去除url结尾的所以反斜杠
if(catalogUrl==null || catalogUrl.length()==0){
String message = "文件路径为空!!";
throw new RuntimeException(message);
}
File file = new File(catalogUrl);
if(!file.exists()){
try {
file.createNewFile();
if(!file.exists()){
String msg = "不是一个标准的文件";
System.err.println(msg);
throw new RuntimeException(msg);
}
} catch (IOException e) {
String msg = "创建文件失败!";
System.err.println(msg);
e.printStackTrace();
}
}
FileWriter fw = null;
BufferedWriter out = null;
try {
fw = new FileWriter(file);
out = new BufferedWriter(fw);
out.write(text, 0, text.length());
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fw != null){
try {
fw.close();
fw = null;
} catch (IOException e) {
e.printStackTrace();
}
}
if(out != null){
try {
out.close();
out = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 对文件文本内容选择替换
* @param fileUrl 文件路径
* @param oldChar 要替换的内容数组
* @param newChar 替换的新内容数组
*/
public static void replaceFileText(String fileUrl, String[] oldChars, String[] newChars) {
fileUrl = removeBackSlant(fileUrl); //去除url结尾的所以反斜杠
if(fileUrl==null || fileUrl.length()==0){
String message = "文件路径为空!!";
throw new RuntimeException(message);
}
if(oldChars==null){
String message = "要替换的内容数组为null";
throw new RuntimeException(message);
}
if(newChars==null){
String message = "替换的新内容数组为null";
throw new RuntimeException(message);
}
if(oldChars.length!=newChars.length){
String message = "旧内容数组与新内容数组长度不一致";
throw new RuntimeException(message);
}
String fileContentString = getFileText(fileUrl); //得到文件文本内容
for(int i=0;i<oldChars.length;i++){
String oldChar = oldChars[i];
String newChar = newChars[i];
fileContentString = fileContentString.replace(oldChar, newChar);
}
text2File(fileContentString,fileUrl);
}
/**
* 提取文本文件内容并可替换内容后到新文件
* @param fileUrl
* @param catalogUrl
* @param oldChars
* @param newChars
*/
public static void fileText2NewFile(String fileUrl,String catalogUrl , String[] oldChars, String[] newChars){
fileUrl = removeBackSlant(fileUrl); //去除url结尾的所以反斜杠
catalogUrl = removeBackSlant(catalogUrl);
if(fileUrl==null || fileUrl.length()==0){
String message = "文件路径为空!!";
throw new RuntimeException(message);
}
if(catalogUrl==null || catalogUrl.length()==0){
String message = "文件路径为空!!";
throw new RuntimeException(message);
}
if(oldChars==null){
String message = "要替换的内容数组为null";
throw new RuntimeException(message);
}
if(newChars==null){
String message = "替换的新内容数组为null";
throw new RuntimeException(message);
}
if(oldChars.length!=newChars.length){
String message = "旧内容数组与新内容数组长度不一致";
throw new RuntimeException(message);
}
String fileContentString = getFileText(fileUrl); //得到文件文本内容
for(int i=0;i<oldChars.length;i++){
String oldChar = oldChars[i];
String newChar = newChars[i];
fileContentString = fileContentString.replace(oldChar, newChar);
}
text2File(fileContentString,catalogUrl);
}
/**
* 去除字符串后的反斜杠"\"
* 会将正斜杠换为两个反斜杠,并且去除结尾的反斜杠"\"
* @param url
* @return
*/
public static String removeBackSlant(String url){
String _url = url.replaceAll("/", "\\");
boolean endsWith = _url.endsWith("\\");
if(endsWith){
int lastIndexOf = _url.lastIndexOf("\\");
String _new = _url.substring(0, lastIndexOf);
return removeBackSlant(_new);
} else {
return _url;
}
}
}
@