String str="hello \"tomcat\""; out.println(str); String s = "黑龙江电视台<div align=\"center\"> " int location = s.indexOf("黑龙江电视台");
加转义字符\,如"\"abc\""就是你想得效果!
上面的朋友报错: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
程序代码如下: 请各位朋友帮忙 import java.io.*; import java.net.*;/** * This simple program uses the URL class and its openStream() method to * download the contents of a URL and copy them to a file or to the console. **/ public class GetURL { public static void main(String[] args) { InputStream in = null; OutputStream out = null; String str=null,s=null; int i=0,location=0; try { // Check the arguments if ((args.length != 1)&& (args.length != 2)) throw new IllegalArgumentException("Wrong number of args");
// Set up the streams URL url = new URL(args[0]); // Create the URL in = url.openStream(); // Open a stream to it if (args.length == 2) // Get an appropriate output stream out = new FileOutputStream(args[1]); else out = System.out;
// Now copy bytes from the URL to the output stream byte[] buffer = new byte[4096]; int bytes_read; while((bytes_read = in.read(buffer)) != -1)
System.out.print(str);
out.println(str);
String s = "黑龙江电视台<div align=\"center\"> "
int location = s.indexOf("黑龙江电视台");
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
请各位朋友帮忙
import java.io.*;
import java.net.*;/**
* This simple program uses the URL class and its openStream() method to
* download the contents of a URL and copy them to a file or to the console.
**/
public class GetURL {
public static void main(String[] args) {
InputStream in = null;
OutputStream out = null;
String str=null,s=null;
int i=0,location=0;
try {
// Check the arguments
if ((args.length != 1)&& (args.length != 2))
throw new IllegalArgumentException("Wrong number of args");
// Set up the streams
URL url = new URL(args[0]); // Create the URL
in = url.openStream(); // Open a stream to it
if (args.length == 2) // Get an appropriate output stream
out = new FileOutputStream(args[1]);
else out = System.out;
// Now copy bytes from the URL to the output stream
byte[] buffer = new byte[4096];
int bytes_read;
while((bytes_read = in.read(buffer)) != -1)
str=new String(buffer,0,bytes_read,"gb2312");
s="中央电视台1";
//s=new String(s.getBytes("8859_1"));
//System.out.println(s);
s=new String(s.getBytes("gb2312"));
//System.out.println(s);
if (str.indexOf(s)!=-1){
str=new String(buffer,0,i+30);
bytes_read=str.lastIndexOf("a",i+30);
System.out.println(i);
System.out.println(str);
str=str.substring(i,bytes_read);
System.out.println(bytes_read);
System.out.println(str);
str="\"tomcat\"中央电视台1";
System.out.println(str);
i=str.indexOf("中央电视台1");
System.out.println(i);
}
//out.write(buffer, 0, bytes_read);
}
// On exceptions, print error message and usage message.
catch (Exception e) {
System.err.println(e);
System.err.println("Usage: java GetURL <URL> [<filename>]");
}
finally { // Always close the streams, no matter what.
try { in.close(); out.close(); } catch (Exception e) {}
}
}
}运行:
java GetURL http://www.daqing.gov.cn/spdb/tv/cctv1.htm