我没说明白么?就是作一个程序。它读取一个html页面。然后把里面的
<img src="image/szzs.gif" width="778" height="97">改成
<img src="image/szzs.gif" width="778" height="97"/>
我是想得到程序的源代码。
<img src="image/szzs.gif" width="778" height="97">改成
<img src="image/szzs.gif" width="778" height="97"/>
我是想得到程序的源代码。
import java.io.FileOutputStream;public class TestConvert
{
public static void main(String[] args)
{
byte[] data = null;
try
{
FileInputStream fis = new FileInputStream("a.html");
data = new byte[fis.available()];
fis.read(data);
fis.close();
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
} String s = new String(data);
String oldString = "<img src=\"image/szzs.gif\" width=\"778\" height=\"97\">";
String newString = "<img src=\"image/szzs.gif\" width=\"778\" height=\"97\"/>"; StringBuffer buf = new StringBuffer();
int start = 0;
int end = s.indexOf(oldString);
while (end != -1)
{
buf.append(s.substring(start, end));
buf.append(newString);
start = end + oldString.length();
end = s.indexOf(oldString, start);
}
buf.append(s.substring(start, s.length())); String result = buf.toString(); try
{
FileOutputStream fos = new FileOutputStream("b.html");
fos.write(result.getBytes());
fos.close();
}
catch (Exception e)
{
e.printStackTrace();
System.exit(2);
}
}
}
int pos1 = sb.indexOf( "<img" );
int pos2 = sb.indexOf( ">", pos1 );
sb.insert( pos2, '/' );
我希望把里面的img标签后面都加上 ”/“,而不是对特定的标签才这样处理。
请大家帮忙