/*
* 功能:将sourcecode文件夹中的java、vue、js、css文本内容写入newcode文件夹下的getcode.txt文本里。
*
* */
package cd.itcast.test;
import java.io.*;
public class getCode {
public static void main(String[] args) throws IOException, InterruptedException {
//获取sourcecode和getcode的File对象
File file = new File("C:\\Users\\Administrator\\Desktop\\job-v2-推荐 (1)");
File file1 = new File("C:\\Users\\Administrator\\Desktop\\newcode\\getcode.txt");
//重命名java、vue、js、css文件为txt文件
methodReName(file);
//重新获取sourcecode的File对象
File files = new File("C:\\Users\\Administrator\\Desktop\\job-v2-推荐 (1)");
//写入getcode.txt文本
methodWrite(files,file1);
}
public static void methodReName(File file) {
for (File listFile : file.listFiles()) {
if (listFile.isDirectory()) {
methodReName(listFile);
continue;
}
String name = listFile.getName();
if (name.endsWith("java")) {
listFile.renameTo(new File(listFile.getParent() + "/" + name.substring(0, name.indexOf("java")) + "txt"));
}
if (name.endsWith("vue")) {
listFile.renameTo(new File(listFile.getParent() + "/" + name.substring(0, name.indexOf("vue")) + "txt"));
}
if (name.endsWith("js")) {
listFile.renameTo(new File(listFile.getParent() + "/" + name.substring(0, name.indexOf("js")) + "txt"));
}
if (name.endsWith("css")) {
listFile.renameTo(new File(listFile.getParent() + "/" + name.substring(0, name.indexOf("css")) + "txt"));
}
}
}
public static void methodWrite(File file,File file1) throws IOException {
for (File listFile : file.listFiles()) {
if (listFile.isDirectory()) {
methodWrite(listFile,file1);
continue;
}
String name = listFile.getName();
if (name.endsWith("txt")) {
FileInputStream fileInputStream = new FileInputStream(listFile);
FileOutputStream fileOutputStream = new FileOutputStream(file1,true);
fileOutputStream.write(fileInputStream.readAllBytes());
fileOutputStream.close();
}
}
}
}
将源代码写入到一个txt文件中
最新推荐文章于 2023-04-09 19:37:23 发布