需要删除文件中最后出现的字符串,即 VirtualHost。我可以使用java,shell来做到这一点。文件结构是一个xml文件。我需要的是我需要将 VirtualHost 移到最后。所以我试图删除它然后追加。
Current Text:
</VirtualHost>
#Added for Patch
<LocationMatch ^/bea_wls_internal/>
RewriteOptions inherit
</LocationMatch>
Desired Text:
#Added for Patch
<LocationMatch ^/bea_wls_internal/>
RewriteOptions inherit
</LocationMatch>
</VirtualHost>
我试图这样做:
int c=1,i=1;
int loc[]=new int[10];
//String str="</VirtualHost>";
BufferedReader br= new BufferedReader (new FileReader ());
String line=br.readLine();
FileWriter fw=new FileWriter(file_location);
PrintWriter pw=new PrintWriter(fw);
List<String> lines = new ArrayList<String>();
while ((line != null)){
//System.out.println(line);
lines.add(line);
c++;
line=br.readLine();
}
br.close();
int size=lines.size();
System.out.println("size "+size);
while(c>i){
int filesize=lines.size();
System.out.println(filesize);
String str=lines.get(filesize-i);
System.out.println(str);
lines.remove(filesize-i);
if (!lines.equals("</VirtualHost>")){
System.out.println("inside if");
lines.add(str);
//break;
}
else if(lines.equals("</VirtualHost>")){
break;
}
i++;
}
for (String writeLine : lines)
pw.println(writeLine);
pw.append("</VirtualHost>");
pw.flush();
pw.close();
fw.close();
System.out.println(c);
}
}