package net;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
/**
*
* 将某个路径的文本文件批量复制并更改其中的一些内容
*
*/
public class ChangeFile {
private static int len ;
private static String resultPath;
private static boolean change = false;
public static void main(String[] args){
String path = "";
String result = "";
change(path,result);
}
public static void change(String path,String result){
File foder = new File(path);
if(!foder.exists()){
System.out.println("目标目录不存在????");
return;
}
len = path.length();
resultPath = result;
md(resultPath);
tree(foder);
}
//根据路径创建文件夹
private static void md(String path){
File folder = new File(path);
if(!folder.exists()){
folder.mkdir();
}
}
//根据路径创建文件
private static File mf(String path){
File file = new File(path);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
//遍历文件夹
private static void tree(File file){
if(file.isDirectory()){
String p = file.getAbsolutePath();
int l = p.length();
if(l>len){//创建新目录
String np = p.substring(len);
md(resultPath+np);
}
File[] fileList = file.listFiles();
for(File f:fileList){
tree(f);
}
}else if(file.isFile()){//复制文件
change = false;
String fp = file.getAbsolutePath();
if(fp.endsWith(".java")){
change = true;
}
String nf = fp.substring(len);
File sfile = mf(resultPath+nf);
try {
BufferedReader r = new BufferedReader(new InputStreamReader(
new FileInputStream(file)));
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(sfile)));
String line = null;
while((line=r.readLine())!=null){
String en = changeStr(line);
w.write(en+"\n");
}
r.close();
w.flush();
w.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//每一行的替换方法
private static String changeStr(String str){
String end = str;
if(change){
//具体规则
end = str;
}
return end;
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
/**
*
* 将某个路径的文本文件批量复制并更改其中的一些内容
*
*/
public class ChangeFile {
private static int len ;
private static String resultPath;
private static boolean change = false;
public static void main(String[] args){
String path = "";
String result = "";
change(path,result);
}
public static void change(String path,String result){
File foder = new File(path);
if(!foder.exists()){
System.out.println("目标目录不存在????");
return;
}
len = path.length();
resultPath = result;
md(resultPath);
tree(foder);
}
//根据路径创建文件夹
private static void md(String path){
File folder = new File(path);
if(!folder.exists()){
folder.mkdir();
}
}
//根据路径创建文件
private static File mf(String path){
File file = new File(path);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
//遍历文件夹
private static void tree(File file){
if(file.isDirectory()){
String p = file.getAbsolutePath();
int l = p.length();
if(l>len){//创建新目录
String np = p.substring(len);
md(resultPath+np);
}
File[] fileList = file.listFiles();
for(File f:fileList){
tree(f);
}
}else if(file.isFile()){//复制文件
change = false;
String fp = file.getAbsolutePath();
if(fp.endsWith(".java")){
change = true;
}
String nf = fp.substring(len);
File sfile = mf(resultPath+nf);
try {
BufferedReader r = new BufferedReader(new InputStreamReader(
new FileInputStream(file)));
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(sfile)));
String line = null;
while((line=r.readLine())!=null){
String en = changeStr(line);
w.write(en+"\n");
}
r.close();
w.flush();
w.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//每一行的替换方法
private static String changeStr(String str){
String end = str;
if(change){
//具体规则
end = str;
}
return end;
}
}