替换多个文件中的部分内容Demo
转载请注明出处!
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
/**
* 替换文件夹中多个文件中的部分内容
*
*/
public class ReplaceMultipleFileParts {
public static void main(String[] args) throws Exception {
// 目标文件夹
File directory = new File("C:/Users/lyj/Desktop/新建文件夹");
// 获取系统的换行符
String lineSeparator = System.getProperty("line.separator", "/n");
String filePath = "";
// 读取文件夹下所有文件
int fileNum = 0, folderNum = 0;
if (directory.exists()) {
LinkedList<File> list = new LinkedList<File>();
File[] files = directory.listFiles();
for (File tempFile : files) {
if (tempFile.isDirectory()) {
System.out.println("文件夹:" + tempFile.getAbsolutePath());
list.add(tempFile);
fileNum++;
} else {
// 获取没一个文件
filePath = tempFile.getAbsolutePath();
System.out.println("文件:" + filePath);
process(filePath, tempFile.getName(), lineSeparator);
folderNum++;
}
}
} else {
System.out.println("文件不存在!");
}
System.out.println("文件夹共有:" + folderNum + ",文件共有:" + fileNum);
}
/**
* 处理过程
*
* @param filePath
* @param fileName
* @param lineSeparator
* @throws IOException
*/
public static void process(String filePath, String fileName,
String lineSeparator) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream(filePath), "UTF-8"));
try {
// 文件读取
String str = readFile(in, lineSeparator);
// 内容替换
str = replaceFileContents(str);
// 输出新的文件
writeNewFile(fileName, str);
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
}
/**
* 读取文件夹下所有文件
*
* @param path
*/
public static void traverseFolder(String path) {
int fileNum = 0, folderNum = 0;
File file = new File(path);
if (file.exists()) {
LinkedList<File> list = new LinkedList<File>();
File[] files = file.listFiles();
for (File tempFile : files) {
if (tempFile.isDirectory()) {
System.out.println("文件夹:" + tempFile.getAbsolutePath());
list.add(tempFile);
fileNum++;
} else {
System.out.println("文件:" + tempFile.getAbsolutePath());
folderNum++;
}
}
} else {
System.out.println("文件不存在!");
}
System.out.println("文件夹共有:" + folderNum + ",文件共有:" + fileNum);
}
/**
* 文件读取
*
* @param in
* @param lineSeparator
* @return
* @throws IOException
*/
public static String readFile(BufferedReader in, String lineSeparator)
throws IOException {
String line = null;
StringBuffer lineBuffer = new StringBuffer();
while ((line = in.readLine()) != null) {
lineBuffer.append(line + lineSeparator);
}
// System.out.println(lineBuffer.toString());
String str = lineBuffer.toString();
return str;
}
/**
* 替换内容
*
* @param str
* @return
*/
public static String replaceFileContents(String str) {
str = str.replaceAll(regex, replacement);
return str;
}
/**
* 输出文件
*
* @param fileName
* @param str
* @throws IOException
*/
public static void writeNewFile(String fileName, String str)
throws IOException {
// 输出文件夹的路径
File txt = new File("C:/Users/lyj/Desktop/新建文件夹2/" + fileName);
if (!txt.exists()) {
txt.createNewFile();
}
byte bytes[] = new byte[1024];
bytes = str.getBytes(); // 新加的
int b = bytes.length; // 改
System.out.println(b);
FileOutputStream fos = new FileOutputStream(txt);
fos.write(bytes, 0, b);
fos.close();
}
}