import java.io.*;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;
public class Main {
private static String FILE_PATH = "D:\\BaiduNetdiskDownload\\test";
private static String DEST_FILE_PATH = "D:\\BaiduNetdiskDownload\\3.md";
public static void main(String[] args) throws IOException {
ArrayList<String> fileNames = (ArrayList<String>) getFileNames(new File(FILE_PATH), new ArrayList<>());
BufferedReader br = null;
PrintWriter pw = null;
try {
pw = new PrintWriter(DEST_FILE_PATH);
for (String fileName : fileNames) {
br = new BufferedReader(new InputStreamReader(new FileInputStream(FILE_PATH + File.separator + fileName)));
String line;
while ((line = br.readLine()) != null) {
if (line.indexOf("####") > -1) {
line = line.replaceAll("####", "#####");
} else if (line.indexOf("###") > -1) {
line = line.replaceAll("###", "####");
} else if (line.indexOf("##") > -1) {
line = line.replaceAll("##", "###");
} else if (line.indexOf("#") > -1) {
line = line.replaceAll("#", "##");
}
pw.println(line);
}
br.close();
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
br.close();
pw.close();
}
System.out.println("Hello world!");
}
private static List<String> getFileNames(File file, List<String> fileNames) {
File[] files = file.listFiles();
for (File f : files) {
if (f.isDirectory()) {
getFileNames(f, fileNames);
} else {
fileNames.add(f.getName());
}
}
return fileNames;
}
private void test() throws IOException {
ArrayList<String> fileNames = (ArrayList<String>) getFileNames(new File(FILE_PATH), new ArrayList<>());
BufferedReader br = null;
PrintWriter pw = null;
FileChannel inputChannel = null;
FileChannel outputChannel = null;
try {
outputChannel = new FileOutputStream(DEST_FILE_PATH).getChannel();
long index = 0;
for (String fileName : fileNames) {
inputChannel = new FileInputStream(FILE_PATH + File.separator + fileName).getChannel();
outputChannel.transferFrom(inputChannel, index, inputChannel.size());
index += inputChannel.size();
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
inputChannel.close();
outputChannel.close();
}
System.out.println("Hello world!");
}
}
标签:文件,fileNames,拼接,FILE,new,PATH,line,null
From: https://www.cnblogs.com/figsprite/p/17589703.html