package com.androidComplex;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import javax.swing.JOptionPane;
public class Modifier {
private String saveFilePath=null;
private Hashtable<String,Hashtable>fileInf=null;
private Hashtable tempHashtable=new Hashtable();
private int version_id=0;
private String tempVersionStr="";
private int ban_ben_count=3;
private int ban_ben_number=5000;
private String progectRootPath="D:"+File.separator+"workspaceAndroid"+File.separator+"kaikairelease";//工程路徑
private ObjectInputStream getSaveInf=null;
private ObjectOutputStream setSaveInf=null;
private BufferedReader br=null;
private BufferedWriter bw=null;
private FileReader fr=null;
private FileWriter fw=null;
private String rootPath=null;
public Modifier(){
}
public void set_BanbenNumber(int number){ //版本號
ban_ben_number=number;
}
public void setBanBenCount(int count){ //版本的數量
ban_ben_count=count;
}
private void initInf(){ //初始化版本信息
fileInf=new Hashtable<String,Hashtable>();
Hashtable tempHashtable=new Hashtable();
tempHashtable.put("PARTNERID",ban_ben_number); //須要修改的東西
fileInf.put(progectRootPath+File.separator+"src"+File.separator+"com"+File.separator+"peptalk"+File.separator+"client"+File.separator+"kaikai"+File.separator+"util"+File.separator+"INFO.java", tempHashtable);
}
public void runModifier(){
initInf();
if(fileInf!=null && fileInf.size()>0){
Enumeration<String> key=fileInf.keys();
String strKey=null;
Hashtable tempHashtable=null;
while(key.hasMoreElements()){
strKey=key.nextElement();
tempHashtable=fileInf.get(strKey);
if(tempHashtable.containsKey("PARTNERID")){
version_id=(Integer)tempHashtable.get("PARTNERID");
}
getModiferContent(strKey,tempHashtable);
}
}
JOptionPane.showMessageDialog(null,"成功發佈"+ban_ben_count+"個版本!","發佈版本成功!",JOptionPane.WARNING_MESSAGE) ;
}
private int temp_index=-1;
private void covereContent(String filePath,Vector content){
Runtime r=Runtime.getRuntime();
String progectPath="D:\\apk"+File.separator;
String temPackage="autobuild"+File.separator;
StringBuilder pathroot=new StringBuilder("cmd /c md");
for(int i=0;i<ban_ben_count;i++){
pathroot.append(" "+progectPath+temPackage+"版本"+(version_id+i));//生成臨時版本文件夾
}
try {
r.exec(pathroot.toString());
}
catch (IOException e1) {
e1.printStackTrace();
}
String tageFileName="kai-signed.apk";
String tageFilePath=progectRootPath+File.separator+"bin"+File.separator+tageFileName;
int tempVersion=version_id;
temp_index=content.indexOf(tempVersionStr);
for(int j=0;j<ban_ben_count;j++){
try {
fw=new FileWriter(filePath);
bw=new BufferedWriter(fw);
if(temp_index!=-1){
content.remove(temp_index);
version_id++;
tempVersionStr=tempVersionStr.substring(0, tempVersionStr.indexOf("=")+1)+" "+'"'+version_id+'"'+" ;";
content.insertElementAt(tempVersionStr, temp_index);
}
for(int i=0;i<content.size();i++){
bw.write((String)content.elementAt(i), 0, ((String)content.elementAt(i)).length());
bw.newLine();
}
bw.flush();
runComplex(r);
r.exec("cmd /c copy "+tageFilePath+" "+progectPath+temPackage+"版本"+(tempVersion+j));
}
catch (Exception e) {
e.printStackTrace();
}
finally{
try{
if(fw!=null){
fw.close();
fw=null;
}
if(bw!=null){
bw.close();
bw=null;
}
}
catch(Exception e){
}
}
}
}
private File runBatFile=null;
private void createRunFile(){//建立臨時運行文件
try{
runBatFile=new File(progectRootPath,"run.bat");
if(!runBatFile.exists())
runBatFile.createNewFile();
String runCmd="cd "+progectRootPath+'\n'+"D:"+'\n'+"ant";
fw=new FileWriter(runBatFile);
bw=new BufferedWriter(fw);
bw.write(runCmd,0,runCmd.length());
bw.newLine();
bw.flush();
bw.close();
fw.close();
}
catch(IOException e){}
finally{
try{
if(fw!=null){
fw.close();
fw=null;
}
if(bw!=null){
bw.close();
bw=null;
}
}
catch(IOException e){}
}
}
private void runComplex(Runtime r){//編譯方法
BufferedReader errBr=null;
BufferedReader br=null;
try{
if(runBatFile==null){ //建立臨時文件
createRunFile();
}
Process p=r.exec("cmd /c "+progectRootPath+File.separator+"run.bat");
errBr= new BufferedReader( new InputStreamReader(p.getErrorStream()));
br=new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuffer sb=new StringBuffer();
String inline;
System.out.println("hello");
if(errBr!=null){
while((inline=br.readLine())!=null){
sb.append(inline).append("\n");
}
}
else{
while((inline=br.readLine())!=null){
sb.append(inline).append("\n");
}
}
System.out.println(sb.toString());
sb=null;
}
catch(Exception e){
}
finally{
try{
if(errBr!=null){
errBr.close();
errBr=null;
}
if(br!=null){
br.close();
br=null;
}
}
catch(IOException e){}
}
}
private void getModiferContent(String filePath,Hashtable tempHashtable){////覆蓋單個文件內容
Vector vStr=new Vector();
try {
fr=new FileReader(filePath);
br=new BufferedReader(fr);
String str=null;
String subStr=null;
boolean isOk=false;
while((str=br.readLine())!=null){
subStr=str.trim();
if(subStr.startsWith("//")){
}
else if(subStr.startsWith("/*")){
if(subStr.endsWith("*/"))
isOk=false;
else
isOk=true;
}
else if(subStr.endsWith("*/")){
isOk=false;
}
else if(isOk){
}
else if(subStr.indexOf("=")!=-1){
if(subStr.indexOf("//")!=-1){
str=replaceStr(str.substring(0, str.indexOf("//")),tempHashtable);//+str.substring(str.indexOf("//"));
}
else{
str=replaceStr(str,tempHashtable);
}
vStr.addElement(str);
}
else{
vStr.addElement(str);
}
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
finally{
try{
if(fr!=null){
fr.close();
fr=null;
}
if(br!=null){
br.close();
br=null;
}
}
catch(Exception e){
}
}
covereContent(filePath,vStr);
vStr=null;
}
private String replaceStr(String str,Hashtable tempHashtable){ //尋找替換每行的數據
/********************替換每一行數據的 必須有規定 有空格 ***********************/
String[] arrayStr=str.split(" ");//獲得數組數據
String temp_key=null;//鍵值對
String temp_value=null;//值
if(arrayStr!=null&&arrayStr.length>3){
for(int i=1;i<arrayStr.length;i++){//遍歷"="號
if(arrayStr[i].equalsIgnoreCase("=")){
temp_key=arrayStr[i-1];
if(tempHashtable.containsKey(temp_key)){
StringBuilder bstr=new StringBuilder(" ");
for(int j=0;j<i+1;j++){
bstr.append(" "+arrayStr[j]);
}
if(temp_key.equalsIgnoreCase("PARTNERID")){
version_id=(Integer)tempHashtable.get(temp_key);
str=bstr.toString()+" "+'"'+version_id+'"'+" ;";
tempVersionStr=str;
}
else
str=bstr.toString()+tempHashtable.get(temp_key)+" ;";
arrayStr=null;
bstr=null;
}
break;
}
}
}
arrayStr=null;
return str;
}
public static void main(String[]args) {
Modifier test=new Modifier();
test.set_BanbenNumber(5000);
test.setBanBenCount(1);
test.runModifier();
}
}
ant腳本
<?xml version="1.0" encoding="UTF-8"?>
<project name="Hello" default="debug" basedir=".">
<property file="local.properties" />
<property file="build.properties" />
<property file="default.properties" />
<!-- they are usefull when .java files are compiled into .class files-->
<property name="sdk2.2-folder" value="C:\Program Files\android-sdk-windows-1.5_r2" />
<property name="sdk-folder" value="${sdk2.2-folder}/platforms/android-1.5" />
<property name="android-jar" value="${sdk-folder}/android.jar" />
<property name="external-libs" value="libs" />
<property name="src" value="src"/>
<property name="outdir-classes" value="bin/classes"/>
<!-- they are usefull when .class files are compiled into .dex files -->
<property name="sdk-tools" value="${sdk-folder}/tools" />
<property name="dx" value="${sdk-tools}/dx.bat" />
<property name="outdir-bin" value="bin" /> <!--can change when you want to change the .class files path-->
<property name="outdir-obfuscate-classes" value="${outdir-bin}/classes" />
<property name="outdir-obfuscate-classes-ospath" value="${basedir}/${outdir-obfuscate-classes}" />
<property name="dex-file" value="classes.dex" />
<property name="intermediate-dex" value="${outdir-bin}/${dex-file}" />
<property name="intermediate-dex-ospath" value="${basedir}/${intermediate-dex}" />
<!-- Put the project's resources into the output package file. -->
<property name="aapt" value="${sdk-tools}/aapt.exe" />
<property name="resource-dir" value="res" />
<property name="asset-dir" value="assets" />
<property name="resources-package" value="${outdir-bin}/${ant.project.name}" />
<!--single res-->
<property name="out-debug-package" value="${outdir-bin}/${ant.project.name}-debug.apk" />
<property name="out-debug-package-ospath" value="${basedir}/${out-debug-package}" />
<!-- package unsingle res-->
<property name="resources-package-ospath" value="${basedir}/${resources-package}" />
<property name="out-unsigned-package" value="${outdir-bin}/${ant.project.name}-unsigned.apk" />
<property name="out-unsigned-package-ospath" value="${basedir}/${out-unsigned-package}" />
<property name="android-tools" value="${sdk2.2-folder}/tools" />
<property name="apk-builder" value="${android-tools}/apkbuilder.bat" />
<property name="srcdir" value="src" />
<property name="srcdir-ospath" value="${basedir}/${srcdir}" />
<property name="external-libs-ospath" value="${basedir}/${external-libs}" />
<!--簽名-->
<property name="jarsigner" value="C:\Program Files\Java\jdk1.6.0_20\bin\jarsigner.exe" />
<property name="useragent" value="byAndroidWeb" />
<property name="out-signed-package" value="${useragent}\${ant.project.name}.apk" />
<property name="out-signed-package-ospath" value="${basedir}\${out-signed-package}" />
<!-- Install the package on the default emulator -->
<property name="adb" value="${android-tools}/adb.exe" />
<!-- Uinstall the package from the default emulator -->
<property name="application-package" value="com.byread.reader" />
<!--R.java in gen folder is copied to src folder -->
<property name="aidl" value="${android-tools}/aidl.exe" />
<!-- 初始化,創建目錄,複製文件 -->
<target name="init">
<copy todir="${src}">
<fileset dir="gen">
</fileset>
</copy>
</target>
<!--
<target name="clean" description="清理,刪除臨時目錄">
<delete dir="${dir.java.build}" />
<delete dir="${dir.dist}" />
<echo level="info">清理完畢</echo>
</target>
-->
<!--compile .java files into .class files-->
<target name="compile" depends="init">
<javac encoding="GBK"
target="1.5"
debug="true"
extdirs=""
srcdir="${src}"
destdir="${outdir-classes}"
bootclasspath="${android-jar}">
<classpath>
<fileset dir="${sdk2.2-folder}\add-ons\google_apis-3\libs" includes="*.jar"/>
<fileset dir="${external-libs}" includes="*.jar"/>
</classpath>
</javac>
</target>
<!-- compile .class files into .dex files-->
<target name="dex" depends="compile">
<echo>Converting compiled files and external libraries into ${outdir}/${dex-file}...</echo>
<apply executable="${dx}" failonerror="true" parallel="true">
<arg value="--dex" />
<arg value="--output=${intermediate-dex-ospath}" />
<arg path="${outdir-obfuscate-classes-ospath}" />
<fileset dir="${external-libs}" includes="*.jar"/>
</apply>
</target>
<!-- Put the project's resources into the output package file. -->
<!--第五步 打包資源文件(包括res、assets、androidmanifest.xml等)-->
<target name="package-res-and-assets">
<echo>Packaging resources and assets...</echo>
<exec executable="${aapt}" failonerror="true">
<arg value="package" />
<arg value="-f" />
<arg value="-M" />
<arg value="AndroidManifest.xml" />
<arg value="-S" />
<arg value="${resource-dir}" />
<arg value="-A" />
<arg value="${asset-dir}" />
<arg value="-I" />
<arg value="${android-jar}" />
<arg value="-F" />
<arg value="${resources-package}" />
</exec>
</target>
<target name="package-res-no-assets">
<echo>Packaging resources...</echo>
<exec executable="${aapt}" failonerror="true">
<arg value="package" />
<arg value="-f" />
<arg value="-M" />
<arg value="AndroidManifest.xml" />
<arg value="-S" />
<arg value="${resource-dir}" />
<!-- No assets directory -->
<arg value="-I" />
<arg value="${android-jar}" />
<arg value="-F" />
<arg value="${resources-package}" />
</exec>
</target>
<!-- Invoke the proper target depending on whether or not an assets directory is present. -->
<!-- TODO: find a nicer way to include the "-A ${asset-dir}" argument
only when the assets dir exists. -->
<target name="package-res">
<available file="${asset-dir}" type="dir" property="res-target" value="and-assets" />
<property name="res-target" value="no-assets" />
<antcall target="package-res-${res-target}" />
</target>
<!-- Package the application and sign it with a debug key.This is the default target when building. It is used for debug. -->
<target name="debug" depends="dex, package-res">
<echo>Packaging ${out-debug-package}, and signing it with a debug key...</echo>
<exec executable="${apk-builder}" failonerror="true">
<arg value="${out-debug-package-ospath}" />
<arg value="-z" />
<arg value="${resources-package-ospath}" />
<arg value="-f" />
<arg value="${intermediate-dex-ospath}" />
<arg value="-rf" />
<arg value="${srcdir-ospath}" />
<arg value="-rj" />
<arg value="${external-libs-ospath}" />
</exec>
</target>
<!-- Package the application without signing it. This allows for the application to be signed later with an official publishing key. -->
<target name="package" depends="dex, package-res">
<echo>Packaging ${out-unsigned-package} for release...</echo>
<exec executable="${apk-builder}" failonerror="true">
<arg value="${out-unsigned-package-ospath}" />
<arg value="-u" />
<arg value="-z" />
<arg value="${resources-package-ospath}" />
<arg value="-f" />
<arg value="${intermediate-dex-ospath}" />
<arg value="-rf" />
<arg value="${srcdir-ospath}" />
<arg value="-rj" />
<arg value="${external-libs-ospath}" />
</exec>
<echo>It will need to be signed with jarsigner before being published.</echo>
</target>
<!--java singner -->
<target name="jarsigner" depends="package">
<echo>Packaging ${out-unsigned-package} for release...</echo>
<exec executable="${jarsigner}" failonerror="true">
<arg value="-verbose" />
<arg value="-storepass" />
<arg value="byread002" />
<arg value="-keypass" />
<arg value="byread002" />
<arg value="-keystore" />
<arg value="bbyread.keystore" />
<arg value="-signedjar" />
<arg value="${out-signed-package-ospath}" />
<arg value="${out-unsigned-package-ospath}" />
<arg value="byread" />
</exec>
</target>
<target name="release" depends="jarsigner">
<echo>release for release...</echo>
</target>
<!-- Install the package on the default emulator -->
<target name="install" depends="debug">
<echo>Installing ${out-debug-package} onto default emulator...</echo>
<exec executable="${adb}" failonerror="true">
<arg value="install" />
<arg value="${out-debug-package}" />
</exec>
</target>
<target name="reinstall" depends="debug">
<echo>Installing ${out-debug-package} onto default emulator...</echo>
<exec executable="${adb}" failonerror="true">
<arg value="install" />
<arg value="-r" />
<arg value="${out-debug-package}" />
</exec>
</target>
<!-- Uinstall the package from the default emulator -->
<target name="uninstall">
<echo>Uninstalling ${application-package} from the default emulator...</echo>
<exec executable="${adb}" failonerror="true">
<arg value="uninstall" />
<arg value="${application-package}" />
</exec>
</target>
</project>
原始:
package com.androidComplex;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import javax.swing.JOptionPane;
public class Modifier {
private String saveFilePath=null;//保存文件的信息路徑,相對路徑
private Hashtable<String,Hashtable>fileInf=null; //文件信息存儲的容器
private Hashtable tempHashtable=new Hashtable();
//private Vector save_temp_value=new Vector();//保存臨時信息,保存版本的信息,若是須要多個版本則須要改變臨時變量
private int version_id=0;//臨時變量
private String tempVersionStr="";//須要保存的要
private int ban_ben_count=3;//版本的數量
private String progectRootPath="D:"+File.separator+"workspaceAndroid"+File.separator+"kaikairelease";//工程路徑
private ObjectInputStream getSaveInf=null;
private ObjectOutputStream setSaveInf=null;
private BufferedReader br=null;
private BufferedWriter bw=null;
private FileReader fr=null;
private FileWriter fw=null;
private String rootPath=null;
public Modifier(){
//rootPath=new File("").getAbsolutePath();
//saveFilePath=rootPath+File.separator+"inf"+File.separator+"saveFile_Path";
//System.out.println("路徑:"+saveFilePath);
//File file=new File(saveFilePath,"file_Inf.data");
/*if(file.exists()){//若是文件存在
fileInf=getSaveModifierInf();
}
else{ */
/* file=new File(saveFilePath);
if(!file.exists())
file.mkdirs();
try {
file=new File(saveFilePath,"file_Inf.data");
if(!file.exists())
file.createNewFile();
}
catch (IOException e) {
e.printStackTrace();
}*/
fileInf=new Hashtable<String,Hashtable>();
Hashtable tempHashtable=new Hashtable();
tempHashtable.put("PARTNERID",5000); //須要修改的東西
// tempHashtable.put("HOST_IP","kai.kai.8080");
fileInf.put(progectRootPath+File.separator+"src"+File.separator+"com"+File.separator+"peptalk"+File.separator+"client"+File.separator+"kaikai"+File.separator+"util"+File.separator+"INFO.java", tempHashtable);
//saveModifierInf();
//}
}
public Hashtable<String,Hashtable> getSaveModifierInf(){ //獲得保存有用信息
FileInputStream fInput=null;
BufferedInputStream bInput=null;
Hashtable<String,Hashtable>tempFileInf=null;
try {
fInput=new FileInputStream(saveFilePath+File.separator+"file_Inf.data");
bInput=new BufferedInputStream(fInput);
getSaveInf=new ObjectInputStream(bInput);
tempFileInf=(Hashtable<String,Hashtable>)getSaveInf.readObject();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
finally{
try{
if(fInput!=null){
fInput.close();
fInput=null;
}
if(bInput!=null){
bInput.close();
bInput=null;
}
if(getSaveInf!=null){
getSaveInf.close();
getSaveInf=null;
}
}
catch(Exception e){}
}
return tempFileInf;
}
public void saveModifierInf(){//保存用戶信息
FileOutputStream fouput=null;
BufferedOutputStream bouput=null;
ObjectOutputStream objectOutput=null;
try {
fouput=new FileOutputStream(saveFilePath+File.separator+"file_Inf.data");
bouput=new BufferedOutputStream(fouput);
objectOutput=new ObjectOutputStream(bouput);
if(fileInf==null){
fileInf=new Hashtable<String,Hashtable>();
}
objectOutput.writeObject(fileInf);
objectOutput.flush();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
System.out.println(e.getMessage());
}
finally{
try{
if(fouput!=null){
fouput.close();
fouput=null;
}
if(bouput!=null){
bouput.close();
bouput=null;
}
if(objectOutput!=null){
objectOutput.close();
objectOutput=null;
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
}
public void addPathInf(String filePath,Hashtable tempHashtable){ //文件路徑與要修改的詞彙綁定
if(fileInf==null)
fileInf=new Hashtable<String,Hashtable>();
if(!fileInf.containsKey(filePath)){
fileInf.put(filePath, tempHashtable);
tempHashtable.clear();
}
else{
}
}
public void addContentInf(String oldValue,String newValue){ // 舊詞語新詞的綁定
if(!tempHashtable.containsKey(oldValue))
tempHashtable.put(oldValue, newValue);
}
public void setBanBenCount(int count){ //版本的數量
ban_ben_count=count;
}
// public Shell shell=new Shell();;
public void runModifier(){//開始修改
if(fileInf!=null && fileInf.size()>0){//文件有要修改的內容
Enumeration<String> key=fileInf.keys();
String strKey=null;
Hashtable tempHashtable=null;
while(key.hasMoreElements()){
strKey=key.nextElement();//獲得存儲文件的路徑
tempHashtable=fileInf.get(strKey);//獲得要修改的詞聚集合
if(tempHashtable.containsKey("PARTNERID")){
version_id=(Integer)tempHashtable.get("PARTNERID");
System.out.println("版本號:"+version_id);
}
getModiferContent(strKey,tempHashtable);
}
/******************************遍歷保存信息************************************************/
key=fileInf.keys();
strKey=null;
tempHashtable=null;
while(key.hasMoreElements()){
strKey=key.nextElement();//獲得路徑
tempHashtable=fileInf.get(strKey);//獲得要修改的詞聚集合
if(tempHashtable.containsKey("PARTNERID")){
tempHashtable.put("PARTNERID", version_id);//(Integer)tempHashtable.get("PARTNERID")+1);
}
}
// saveModifierInf();
}
// JOptionPane.showMessageDialog(null,"成功發佈"+ban_ben_count+"個版本!","發佈版本成功!",JOptionPane.WARNING_MESSAGE) ;
}
private int temp_index=-1;
public void covereContent(String filePath,Vector content){//動態產生新的臨時須要編譯的動態源文件
Runtime r=Runtime.getRuntime(); //調用dos接口
// System.out.println("文件路徑:"+filePath);
String progectPath="D:\\apk"+File.separator;
String temPackage="autobuild"+File.separator;
StringBuilder pathroot=new StringBuilder("cmd /c md");
for(int i=0;i<ban_ben_count;i++){
pathroot.append(" "+progectPath+temPackage+"版本"+(version_id+i));//生成臨時版本文件夾
}
try {
r.exec(pathroot.toString());
}
catch (IOException e1) {
e1.printStackTrace();
}
String tageFileName="kai-debug.apk";//目標文件路徑
// String tageFilePath="D:\\Hello"+File.separator+"bin"+File.separator+tageFileName;
String tageFilePath=progectRootPath+File.separator+"bin"+File.separator+tageFileName;
int tempVersion=version_id;
temp_index=content.indexOf(tempVersionStr);//獲得要替換文件的內容座標號
System.out.println("第一次 序號:"+temp_index);
for(int j=0;j<ban_ben_count;j++){
try {
fw=new FileWriter(filePath);
bw=new BufferedWriter(fw);
if(temp_index!=-1){
content.remove(temp_index);
version_id++;
tempVersionStr=tempVersionStr.substring(0, tempVersionStr.indexOf("=")+1)+" "+'"'+version_id+'"'+" ;";
System.out.println("臨時修改後的文件:"+tempVersionStr);
content.insertElementAt(tempVersionStr, temp_index);
// temp_index=-1;
}
for(int i=0;i<content.size();i++){
bw.write((String)content.elementAt(i), 0, ((String)content.elementAt(i)).length());
bw.newLine();
}
bw.flush();
runComplex(r);
r.exec("cmd /c copy "+tageFilePath+" "+progectPath+temPackage+"版本"+(tempVersion+j));
// System.out.println("版本編號:"+(tempVersion+j));
//拷貝文件
}
catch (Exception e) {
e.printStackTrace();
}
finally{
try{
if(fw!=null){
fw.close();
fw=null;
}
if(bw!=null){
bw.close();
bw=null;
}
}
catch(Exception e){
}
}
}
}
File runBatFile=null;
public void createRunFile(){//建立臨時運行文件
try{
runBatFile=new File(progectRootPath,"run.bat");
if(!runBatFile.exists())
runBatFile.createNewFile();
String runCmd="cd "+progectRootPath+'\n'+"D:"+'\n'+"ant";
fw=new FileWriter(runBatFile);
bw=new BufferedWriter(fw);
bw.write(runCmd,0,runCmd.length());
bw.newLine();
bw.flush();
bw.close();
fw.close();
}
catch(IOException e){}
finally{
try{
if(fw!=null){
fw.close();
fw=null;
}
if(bw!=null){
bw.close();
bw=null;
}
}
catch(IOException e){}
}
}
public void runComplex(Runtime r){//編譯方法
//調用dos
BufferedReader errBr=null;
BufferedReader br=null;
try{
if(runBatFile==null){ //建立臨時文件
createRunFile();
}
Process p=r.exec("cmd /c "+progectRootPath+File.separator+"run.bat");
errBr= new BufferedReader( new InputStreamReader(p.getErrorStream()));
br=new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuffer sb=new StringBuffer();
String inline;
System.out.println("hello");
if(errBr!=null){
while((inline=br.readLine())!=null){
sb.append(inline).append("\n");
}
}
else{
while((inline=br.readLine())!=null){
sb.append(inline).append("\n");
}
}
System.out.println(sb.toString());
sb=null;
}
catch(Exception e){
}
finally{
try{
if(errBr!=null){
errBr.close();
errBr=null;
}
if(br!=null){
br.close();
br=null;
}
}
catch(IOException e){}
}
}
public void getModiferContent(String filePath,Hashtable tempHashtable){////覆蓋單個文件內容
Vector vStr=new Vector();
try {
fr=new FileReader(filePath);
br=new BufferedReader(fr);
String str=null;
String subStr=null;
boolean isOk=false;
while((str=br.readLine())!=null){
subStr=str.trim();
if(subStr.startsWith("//")){
//System.out.println(str);
}
else if(subStr.startsWith("/*")){
if(subStr.endsWith("*/"))
isOk=false;
else
isOk=true;
//System.out.println(str);
}
else if(subStr.endsWith("*/")){
isOk=false;
// System.out.println(str);
}
else if(isOk){
// System.out.println(str);
}
else if(subStr.indexOf("=")!=-1){
if(subStr.indexOf("//")!=-1){
str=replaceStr(str.substring(0, str.indexOf("//")),tempHashtable);//+str.substring(str.indexOf("//"));
}
else{
str=replaceStr(str,tempHashtable);
}
vStr.addElement(str);
}
else{
// vStr.indexOf(o);
vStr.addElement(str);
}
System.out.println(str);
//vStr.addElement(str);
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
finally{
try{
if(fr!=null){
fr.close();
fr=null;
}
if(br!=null){
br.close();
br=null;
}
}
catch(Exception e){
}
}
covereContent(filePath,vStr);
vStr=null;
}
public String replaceStr(String str,Hashtable tempHashtable){ //尋找替換每行的數據
/********************替換每一行數據的 必須有規定 有空格 ***********************/
String[] arrayStr=str.split(" ");//獲得數組數據
String temp_key=null;//鍵值對
String temp_value=null;//值
if(arrayStr!=null&&arrayStr.length>3){
for(int i=1;i<arrayStr.length;i++){//遍歷"="號
if(arrayStr[i].equalsIgnoreCase("=")){
temp_key=arrayStr[i-1];
if(tempHashtable.containsKey(temp_key)){
// System.out.println("關鍵值:"+tempHashtable.get(temp_key)+"是否:"+tempHashtable.containsKey(temp_key));
//arrayStr[i+1]=""+tempHashtable.get(temp_key);
//temp_value=""+tempHashtable.get(temp_key);
StringBuilder bstr=new StringBuilder(" ");
for(int j=0;j<i+1;j++){
bstr.append(" "+arrayStr[j]);
}
if(temp_key.equalsIgnoreCase("PARTNERID")){
version_id=(Integer)tempHashtable.get(temp_key);
str=bstr.toString()+" "+'"'+version_id+'"'+" ;";
tempVersionStr=str;
}
else
str=bstr.toString()+tempHashtable.get(temp_key)+" ;";
arrayStr=null;
bstr=null;
}
break;
}
}
}
arrayStr=null;
return str;
}
public static void main(String[]args) {
new Modifier().runModifier();
/* Runtime r=Runtime.getRuntime();
try {
r.exec("cmd /c md fest");
} catch (IOException e) {
System.out.println("hello");
} */
}
}
bat文件
cd D:\Hello D: ant