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 PortChange {
public static int time=0;
public static void main(String[] args){
String fileName="E:\\zip";
String fileType=".xml";
iteratorFile(fileName,fileType,"","");
System.out.println("一共更新了"+time+"个文件");
}
public static void iteratorFile(String fileName,String fileType,String from,String to){
File file=new File(fileName);
File[] files=file.listFiles();
for(int i=0;i<files.length;i++){
if(files[i].isDirectory()){
iteratorFile(files[i].getAbsolutePath().replace('\\','/'),fileType,from,to);
}else if(files[i].isFile()){
String file_name=files[i].getAbsolutePath().replace('\\','/');
updateFile(new File(file_name),fileType,from,to);
}
}
}
public static void updateFile(File file,String fileType,String from,String to){
if(file.getName().length()>4&&file.getName().substring(file.getName().length()-4).equals(fileType)){
try {
int index=file.getAbsolutePath().lastIndexOf('\\')+1;
String fileName=file.getName();
File file1=new File(file.getAbsolutePath().substring(0,index)+fileName+"_bakj");
System.out.println(file.getAbsolutePath());
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(file)));
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file1)));
String line=null;
while((line=br.readLine())!=null){
// bw.write(line.replace("charset=gb2312", "charset=gbk").replace("charset=GB2312","charset=gbk")+"\n");
// bw.write(line.replace("<Connector port=\"8080\"", "<Connector port=\"8888\""));
bw.write(line.replace("<Connector port=\""+from+"\"", "<Connector port=\""+to+"\""));
}
time++;
bw.flush();
bw.close();
br.close();
file.delete();
file1.renameTo(new File(file.getAbsolutePath().substring(0,index)+fileName));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
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 PortChange {
public static int time=0;
public static void main(String[] args){
String fileName="E:\\zip";
String fileType=".xml";
iteratorFile(fileName,fileType,"","");
System.out.println("一共更新了"+time+"个文件");
}
public static void iteratorFile(String fileName,String fileType,String from,String to){
File file=new File(fileName);
File[] files=file.listFiles();
for(int i=0;i<files.length;i++){
if(files[i].isDirectory()){
iteratorFile(files[i].getAbsolutePath().replace('\\','/'),fileType,from,to);
}else if(files[i].isFile()){
String file_name=files[i].getAbsolutePath().replace('\\','/');
updateFile(new File(file_name),fileType,from,to);
}
}
}
public static void updateFile(File file,String fileType,String from,String to){
if(file.getName().length()>4&&file.getName().substring(file.getName().length()-4).equals(fileType)){
try {
int index=file.getAbsolutePath().lastIndexOf('\\')+1;
String fileName=file.getName();
File file1=new File(file.getAbsolutePath().substring(0,index)+fileName+"_bakj");
System.out.println(file.getAbsolutePath());
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(file)));
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file1)));
String line=null;
while((line=br.readLine())!=null){
// bw.write(line.replace("charset=gb2312", "charset=gbk").replace("charset=GB2312","charset=gbk")+"\n");
// bw.write(line.replace("<Connector port=\"8080\"", "<Connector port=\"8888\""));
bw.write(line.replace("<Connector port=\""+from+"\"", "<Connector port=\""+to+"\""));
}
time++;
bw.flush();
bw.close();
br.close();
file.delete();
file1.renameTo(new File(file.getAbsolutePath().substring(0,index)+fileName));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}